Javatpoint Logo
Javatpoint Logo

Linux Find File

The find command in Linux is a robust tool that allows system administrators to manage and locate directories and files based on a huge range of search techniques.

It can search files and directories by the type, name, or permissions, size, extension, etc.

Besides locating directories and files, associating the find command along with others allows us for taking action over the outcomes. Including the -exec option allows system admins to execute external commands and implement actions like moving, deleting, copying, or modifying permissions of the file that are matching the described criteria like name, size, etc.

The find command searches for various directories and files inside the directory hierarchy which is based on the user-provided expression. It could implement user-specific operations on all the matched files. Also, we can add with various other tools like sed or grep.

Syntax-

The basic syntax for Linux find command is as follows:

Where,

  1. The path describes the directory.
  2. name of the file or directory to search: filename or directory to search for
  3. action to take: like move, delete, copy, etc.

Options in find command

There are various options available in find command. Some of the important options are discussed and explained as follows:

  1. -exec CMD: A file being found which matches the criteria of the find command and returns zero because of its status of exit for command execution successfully.
  2. -ok CMD: It implements similarly to the -exec option except that the user is induced first.
  3. -links N: It finds files along with the 'N' links.
  4. -inum N: It finds files along with the inode 'N' number.
  5. -newer file: It finds the file that was created/modified after 'file'.
  6. -name demo: It finds files that are described by 'demo'.
  7. -print: It shows the file's path name that is searched with the help of the rest of the file criteria.
  8. -perm octal: It finds files when the permission is 'octal'.
  9. -size +N/-N: It finds for 'N' blocks files. In this option, 'N' pursued by 'c' could be used for measuring size in characters. '-N' defines that the size < 'N' blocks and '+N' defines that size > 'N' blocks.
  10. -empty: It finds empty directories and files.
  11. -user name: It finds files that are owned by ID 'name' or user name.
  12. !expr: It returns true when 'expr' is false.
  13. \(expr \): It returns true when 'expr' is true. It is used to group the criteria associated with AND or OR.

Find directories and files

Find particular files by extension or name

To search for a particular file, we can execute the below command through the root (/). This command includes the matching name for the file we are looking for.


Linux Find File

Remember that the outcomes contain the path. It's important when we do not know the directory in which the file is positioned, or when it's in multiple places.

We can also find the file in other directories while still inside the current position. We need to facilitate the path for our directory in which we wish to search.


Linux Find File

Looking for particular files in other directories

In this case, we will find for each of those files that are beginning with the file letters inside the newdir directory.


Linux Find File

Find for files by their extension

In Linux, for finding a file along with a specific extension, include it to the command.


Linux Find File

Find directories and files by name

Apply the following command to search for directories and files beginning with the hello letters. In our system, we have hello1, hello2, hello3, and hello4 files.

So let's execute the command:


Linux Find File

The above command will return all the directories and files that are starting with the hello letters. We need to describe it in this command to search directories or files only.

Find only directory or files

For only files, use the f type switch.


Linux Find File

For only directories, use the d type option for locating only directories.


Linux Find File

Find case insensitive command

All finds with -name option is case sensitive and these will not provide outcomes with the capital letters. We can apply the -iname option for getting all cases:


Linux Find File

Find a file through more than one directory

For finding the files in distinct directories, we can include their path inside the command. In this case, we will find it inside the newdir directory.


Linux Find File

Find more than one file with distinct extensions from each directory

We can apply the find command for locating more than one file that distributes the distinct extensions like *.pdf, *.txt, *.doc, etc. It could be separately done, a single extension at a time, or with the help of a single command that contains each desired extension.


Linux Find File

Find files including certain text

We wish to access the files including certain types of text but can't recall its file location and name. This command permits us to find each file including our target text.

To search for each file including the hello word, we can use the following command:


Linux Find File

Option -i allows the command for ignoring cases and it will search that text whether it is capitalized or not i.e., hello, Hello, etc.

To search for a file inside a particular directory, we can simply include them in our command.


Linux Find File

Find directories and files based on size

We can find all the directories and files that are greater, equal, or smaller than a specific size, in a spec range, or empty. Apply the proper size format based on the directory or file type we are looking for.

Size options are:

M- Megabytes

G- Gigabytes

c- bytes

k- kilobytes

Find files of a specific size- equal to 20MB

To find each 20MB file, we can use the following command:

Find files bigger than a certain size

We can use the following command to find those files that are bigger than a certain size:


Linux Find File

Find files smaller than 10MB in our current directory

We can use the following command to find those files that are smaller than 10MB in our current directory:


Linux Find File

Find files that contain sizes between 1-3MB

If searching files in a certain range like between 1 and 3 MB, we can use the following command:


Linux Find File

Find directories bigger than 10kb

We can use the following command:


Linux Find File

Find empty directories and files

For files

We can use the following command to find an empty file:


Linux Find File

Or,

For directories

We can use the following command to find an empty directory:


Linux Find File

Find files by modification time or age

With this option, we can find those files that were created before n days:


Linux Find File

Where,

-mtime +9 will search those txt files that were created before 9 days.

Find a file by modification date

It looks search those files that are modified in the last n hours.


Linux Find File

We can also use the following command to search those directories that are modified in the last n days:


Linux Find File

Find files according to modification or access

Find files according to time or date accessed. It permits us to look at files that haven't or have been accessed at a particular time.

To look at files that haven't been accessed in the last 15 days in our home directory, we can use the following command:


Linux Find File

We can use the following command to see files that are accessed 15 days ago exactly:


Linux Find File

Also, we can use the following command to see files that are accessed in the last 15 days:


Linux Find File

Find files changed in the last n days

We can also consider files within the /home directory that changed in the last 1 days with the help of the following command:


Linux Find File

Find files changed in a particular time

For example, each file changed between 7 and 16 days ago within our home directory.


Linux Find File

Directories and files accessed in the last n minutes

For files:

To see files accessed in the last 15 minutes, we can use the option -amin. The command is as follows:


Linux Find File

For directories:

To see directories accessed in the last 15 minutes, we can use the option -amin. The command is as follows:


Linux Find File
Next TopicEcho command




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA