Linux provides the find
command in order to find files and directories. The find command is executed via the command line interface and it is also provided by the Unix and other related Unix-based operating systems. The find command can be used to search files and folders according to their names, creation date, modification date, ownership, permission, path, etc. Also, it provides useful features like executing commands for the match results.
find Command Syntax
The find command has the following syntax.
find PATH -name SEARCH_TERM OPTIONS
- PATH is the search path where the specified SEARCH_TERM will be searched.
- SEARCH_TERM is searched in the specified PATH.
- OPTIONS are used for different search types. The option can e used to search for a specific file name pattern or file extension or recursively etc.
find Command Help
The find command provides different help options where the --help
option is one of them. The –help option list very basic help and usage information about the find command.
$ find --help
To get more details about the find command the man page can be used. The man page of the find command provides a lot of information about the options, usage examples, etc.
$ man find
Search File with Name
The files can be searched according to their names by using the -name
option. The complete or partial name is provided as a parameter with the -name option. In the following example, we search for the file named “database” in the home directory of the user “ismail”.
$ find /home/ismail -name database
Alternatively, we can search in the current working directory and all child directories by using bash “.” operator.
$ find . -name database
Search File with Pattern
The find command provides the ability to search files with their names for specific patterns. The “*” glob operator can be used to search files with partial names. We can search files those names that contain “data” with the following command.
$ find / -name "*data*"
Search File with Extension
The find command can be used to search files according to their extensions. The glob operator is used to express the name of the file and the extension is specified explicitly. In the following example, we search files with the “*.txt” extension.
$ find / -name "*.txt*"
Find and Delete File
One of the useful features of the find command is the ability to delete founded files. The -exec
option is used to delete files by executing the “rm” command. In the following example, we delete all text or “*.txt” extension files.
$ find / -name "*.txt*" -exec rm {} \;
Search Empty Files and Directory
The Linux operating system may contain empty files and directories. The find command can be used to find empty files and directories easily by using the -empty
option. There is no need to provide a file or directory name.
$ find / -empty
Search Empty Files with Specific Extension
The -empty option can be used to search empty files with a specific extension. In the following example, we search empty *.txt files by using the -name option.
$ find / -name "*.txt" -empty
Search with Permission
Another useful feature of the find command is the ability to search files and directories according to their permissions. The Linux system use permission consisting of 3 numbers like “000”, “777” etc. The -perm
the option is used to specify the permission value where all matched files and directories will be listed.
$ find / -perm 777
Search SUID Enabled Files
The find command can be used to search SUID-enabled files. SUID is a permission that enables the normal user to run privileged commands on its own. The -perm
option is used to specify SUID with /u=s
parameters.
$ find / -perm /u=s
Even we can search and delete SUID-enabled bash scripts for the specified path and all of its children.
$ find /tmp -perm /u=s -exec rm {} \;
Search with User Name
In Linux, every file and directory has an owner. The find command can be used to search according to the owner information of the file or directory. The -user
option is used to specify the owner name of the file or directory. In the following example, we search files and directories owned by “ismail”.
$ find / -user "ismail"
Search with Size
Another interesting option for the find command is the ability to search for the size of the files. The -size
option can be used to specify search terms that specified the size of the files to match. In the following example, we search for files with 100M size.
$ find / -size 100M
In the following example, we search for the files whose sizes are higher than 100M.
$ find / -size +100M
In the following example, we search for the files whose sizes are lower than 100M.
$ find / -size -100M
In the following example, we will search files with sizes between 100M and 200M.
$ find / -size +100M -size -200M
Delete Matched Files
The find command provides the ability to execute commands using the matched file names. The -exec
option is used to run commands and scripts where the matched filename is expressed with the {}
. In the following example, we run rm
command for every matched file by the find command.
$ find / -t f -name *.tmp -exe rm {} \;
Windows find Command
The counterpart operating system Windows also provides the find
command but it is very weak with its features and does not provide the same functionality Linux find command. The Windows find command can be used for basic search operations by providing the search term and search path like below.
> find /V "ismail" C:\