As the name suggests, the find command is used to search files and directories in UNIX-like operating system. Find command can search files and directories on the basis of their name, size, type, permissions, access time, modify time, and a lot of other criteria. Some of the practical examples of the find command are shown below:

Syntax:

# find [start_at] conditions actions

According to the above syntax find command searches the files on the disk from a given starting location, start_at, looking for the files & directories that match given conditions and then taking the given actions, such as printing out the file names.

Example 1: How to find files on the basis of their names.

Syntax:

# find [location-where-To-Find] [options] [File Name]

Example:

# find / -iname interfaces

find iname command example

In the above command ‘-iname‘ option is used for case insensitive, which means it will search all the files that match interfaces.

Example 2: Find all the files & directories which have 777 permissions.

find perm command example

Example 3: Find all the regular files in /root and have permissions 644.

# find /root -type f perm 644

find files of type file

Similarly find the all the files which are directories:

Find all the files in /home folder which have the symbolic links:

Find all the socket files in / folder:

Example 4: Find files based on their size.

above command will find all the files in /home which have a size of more than 1GB.

Find all the files which have exact 1GB size.

Find all the files which have size less than 1GB in /home folder.

Example 5: Execute Command after find operations.

Syntax:

# find [where-to-search] [options] -exec [command] {} ;

Find all the files which have 644 permissions and do the listing using ls -l command:

# find / -perm 644 -exec ls -l {} ;

Find all the files have “.mp3 extensions and remove it”:

# find / -name "*.mp3"  -exec rm -rf {} ;

Note: We ca also use ok instead exec option but in case of ok ,it will ask the user first. If the user agrees, then command will execute, Otherwise just return false.

Example 6: Search files to a specific directories level using maxdepth & mindepth.

# find /root -maxdepth 2 -type f -perm 644

The above command will search all the regular files under / root folder and one level down. (i.e /root – level 1, and one sub-directory – level 2) which has 644 permissions.

Find all the regular files where maxdepth is 4 and mindepth is 2 and has permissions 644:

# find / -maxdepth 4 -mindepth 2 -type f -perm 644

Example 7: Search files and directories using their inode numbers.

# find -inum [inode-numbers]

Example 8: Find the files based on their modified & access time.

Find the file under the directory/home that was modified more than 60 days ago:

Find the files in /var/spool folder that have been accessed at least two days ago:

# find /var/spool -atime +1

Example 9: Find all the empty files & directories in / folder.

Example 10: Print and xargs options in find Command.

# find / -type f -perm 644 -print0 | xargs -0 ls -l

The above command will find all the regular files which have 644 permissions and print all the match files and gives input to xargs.