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]
If you encounter below error while running the find command:
find: command not found
you may try installing the below package as per choice of your distribution:
Distribution | Command |
---|---|
OS X | brew install findutils |
Debian | apt-get install findutils |
Ubuntu | apt-get install findutils |
Alpine | apk add findutils |
Arch Linux | pacman -S findutils |
Kali Linux | apt-get install findutils |
CentOS | yum install findutils |
Fedora | dnf install findutils |
Raspbian | apt-get install findutils |
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