How to use find command to search a file? Learn how to find a file in Linux terminal in all directories and how to find a file in Linux without knowing the path.
find command
‘find’ searches the directory tree rooted at each file name FILE by evaluating the EXPRESSION on each file it finds in the tree. This list of files to search is followed by a list of expressions describing the files we wish to search for.
The general syntax for the find command is find /path/ -type f -name file-to-search
find [options] [path...] [expression]
Where:
- options defines the treatment of the symbolic links and optimization method. In other words the type of file to search.
- path… defines the starting directory or directories where find will be searched.
- expression defines the options, search patterns, and actions for the files to be searched. For example it will define the file extension.
For Example:
find -L /Documents -name "*.xls"
- The option -L (options) tells the find command to follow symbolic links.
- The /Documents (path…) specifies the directory that will be searched.
- The (expression) -name “*.xls tells find to search files ending with .xls (excel files).
Find Files by Name
To find a file by its name, use the -name option along with the find command. For example, to search for a file named file123.xls in the /home/sourcedigit directory, you would use the following command:
find /home/sourcedigit -type f -name file123.xls
Note that the command option name is case sensitive by default. To execute the command in a case-insensitive search, change the -name option with -iname. For example,
find /home/sourcedigit -type f -iname file123.xls
Find Files by Extension
To find files by extension we will use the extension with the command. For example, to find all files ending with .tar.gz inside the /home/sourcedigit directory, you would type:
find /home/sourcedigit -type f -name '*.tar.gz'
Find Files by Modification Date
The most useful aspect of find command it to search for files based on their last modification, access, or change time. For example, we wiant to find a file which was modified few days ago, but you forgot the name of the file.
See the following example where we will search all files under the /home/sourcedigit directory that ends with .pdf and has been modified in the last five days:
find /home/sourcedigit -name "*.pdf" -mtime 5
Simlarly we can search based on the modification date using the -daystart option. See the following command where we are searching all files in the /home directory that were modified 30 or more days ago:
find /home/sourcedigit -mtime +30 -daystart
Similarly, one can easily serach the files by owner, particular user or group, use the -user and -group options. For example, to search for all files and directories owned by the user sourcedigit:
find / -user sourcedigit
Find and Delete Files
To delete all matching files, add the -delete option to the end of the match expression. For example, to delete all files ending with .pdf from the /home/sourcedigit:
find /vhome/sourcedigit -name `*.pdf` -delete
Be extra cautious using the delete option with the command.