find Command Examples in Linux

The find command enables you to search a specific location for files and directories that adhere to some search criteria. It recursively searches the directory structure, including any subdirectories and their contents, beginning with the search location you enter. You can perform one or more actions on the files found.

The -type option enables you to specify the type of object you’re looking for, such as d for directory or f for file. The -name option is where you specify the name of the object you’re looking for. The following example searches a user’s home directory (and all subdirectories) for all files named 2021_report:

$ find /home/user -type f -name 2021_report

Syntax

The syntax of the find command is:

$ find [options] {search locations} {search criteria} [actions]

find VS. locate Commands

The locate command searches a database and retrieves information on files present on your system. However, failure to keep this database updated may produce outdated results. The find command, on the other hand, performs a live search of the file system and may concentrate on a specific location. The find command may take more time to complete a search than the locate command.

Options for files found

When the system finds a listing that meets your criteria, there are several actions you can perform on the results. Several of these options are outlined in the following table.

Option Used To
-print Displays the location of the files found.
-exec Executes the command that follows.
-ok Executes the command that follows interactively.
-delete Deletes files found.
-fprint Stores results in the target file.

find Command Examples

1. To find the file with name, within specified directory:

# find / -name file.txt
# find . -name file.txt       (within current directory)

2. To find the file of specified permissions:

# find / -perm 775
# find / -perm a+x
# find . -perm 775(within current directory)

3. To have at least one write bit set (specific permission):

# find / -perm /a+w
# find . -perm /a+w         (within current directory)

4. To find a file which does not have any executable bit set:

# find / ! -perm /111
# find / ! -perm /a+x
# find . ! -perm /a+x        (within current directory)

5. To search for a world readable file, at least one write bit and not executable by anybody:

# find . -perm -a+r -perm /a+w ! -perm /a+x

6. To avoid following the symbolic links:

# find -p / -name file

7. To follow the symbolic links:

# find -p / -name file

8. Not to follow symbolic links, except while processing the command line arguments:

# find -H / -name file

9. To find the file with specified depth in the directory structure:

# find -d 2 / -name file

10. To specify the descend level:

# find -maxdepth 2 / -name file

11. To specify the minimum depth level:

# find -mindepth 4 / -name file

12. To process each directory’s contents before the directory itself:

# find -depth / -name file 

13. To give the start day for time stamps of file:

# find -daystart -atime file

14. To show no error message will be issued (if file is deleted while processing):

# find -ignore_readdir_race / -name file

15. To turnoff the -ignore_readdir_race:

# find -noignore_readdir_race / -name file

16. To not to include directories from other mounts points:

# find -mount / -name file

17. To Changes the regular expression syntax understood:

# find -regextype type

18. To turn warning messages on or off:

# find -warn / -name file
# find -nowarn / -name file

19. Don’t descend directories on autofs filesystems:

# find -xautofs / -name file

20. Don’t descend directories on other filesystems:

# find -xdev / -name 
Related Post