The find command is an advanced tool for searching files or directories rigorously in your file system, taking a little longer time than its alternative tools like the locate command.
It’s due to its nature of searching a specified file by walking through each file for a match in your system instead of creating a database where all the file paths are indexed.
Apart from that, it supports file searching by its name, folder, users, groups, type, size, creation date, modification date, accessed time, owner, and permissions.
Tutorial Details
Description | Find |
Difficulty Level | Moderate |
Root or Sudo Privileges | No |
OS Compatibility | Ubuntu, Manjaro, Fedora, etc. |
Prerequisites | find |
Internet Required | No |
Syntax of the Find Command
The find command takes three arguments: one is the option; the second is the expression (what action to perform on the file); and the third is the path.
$ find [OPTION] [EXPRESSION] [PATH]
Finding Files Using the Name in the Present Directory
The following command, without using any flags, will print the file if it’s found in the present directory.
$ find file.txt
Output:
To print the relative path of the file, use the “-name
” flag with the filename.
$ find -name file.txt
Output:
To print the absolute path of the file, specify the complete path with the filename specified to the “-name
” flag.
$ find /home/linuxtldr/ -name file.txt
Output:
Finding Files Using the Name in Different Directories
To search the file in a different location (ex: “/home/
“), specify the path with your filename using the “-name
” flag.
$ find /home/ -name file.txt
Output:
Note that it will search for the specified file rigorously in each directory; if you have issues like “Permission denied”, use sudo with the find command.
$ sudo find /home/ -name file.txt
Output:
Finding Files Using the Name and Ignoring the Case
By default, the find command will search for the specified filename with case-sensitivity, meaning “file.txt
” and “File.txt
” are both different, as shown.
$ ls *.txt
$ find -name file.txt
Output:
Use the “-iname
” flag to ignore case-sensitivity while searching the specified file.
$ find -iname file.txt
Output:
Finding Files by Excluding Specific Directory
In my home directory, there is a text file (ex: “file.txt
“), and there is another text file inside the “~/Documents
” directory.
$ ls
$ ls Documents/
Output:
If you search for the text file using the find command, it will return all the matches.
$ find /home/linuxtldr/ -type f -name *.txt
Output:
However, you can limit the search by excluding the directory in which you have no interest using the “-not -path
” flag.
$ find /home/linuxtldr/ -type f -name *.txt -not -path "/home/linuxtldr/Documents/*"
Output:
The above command will not look for the text file in the “~/Documents
” directory.
Finding Directory Using the Name in the Present Directory
To search for the directory, you can specify its name to the find command to search in the present directory.
$ find Downloads
Output:
Use the “-name
” flag with the directory name to get the relative path of the directory.
$ find -name Downloads
Output:
Specify the complete path where you want to search for the directory with your directory name specified to the “-name
” flag to get an absolute path.
$ find /home/linuxtldr/ -name Downloads
Output:
However, if you have a file with the same name as the directory you were searching for, it might also appear in the search result as shown.
$ touch Documents/Downloads
$ find -name Downloads
Output:
To ignore the file in the result, print only the directory name, then use the “-type
” flag with the “d
” value (meaning directory).
$ find -type d -name Downloads
Output:
Finding HTML Files in the /var/www/html Directory
To search for “index.html
” in the “/var/www/html
” directory, specify the complete path with the “-type
” flag with value “f
” for file and the “-name
” flag specifying the filename.
$ find /var/www/html/ -type f -name index.html
Output:
Search for all files with the “.html
” extension in the “/var/www/html
” directory.
$ find /var/www/html/ -type f -name "*.html"
Output:
Finding Files with Specific Permissions
The following is a list of multiple text files, including one with the permission “777
“.
$ ls -l file*
Output:
To print only the specific text file with “777
” permissions, use “-type
” flag to assign the type of the file with the “-perm
” flag defining the permissions.
$ find -type f -perm 777
Output:
Use the following command to print all text files without “777
” permission in your present directory.
$ find -type f ! -perm 777 -name "*.txt"
The following command will print all the files that do not match the permissions “777
” in your present directory.
$ find -type f ! -perm 777
Finding SGID Files
The following command will list all the SGID files in the present directory.
$ find -perm /2000
Finding SUID Files
The following command will list all the SUID files in the present directory.
$ find -perm /4000
Finding Sticky Bit Files
The following command will list all the sticky bit files in the present directory.
$ find -perm /1000
Finding Files with Read-Only Permission
The following command will ignore all the files with write and executable permissions in the present directory, listing only read-only files.
$ find -type f ! -perm /ugo=wx
Output:
Finding Files with Writeable Permission
The following command will ignore all the files with read and executable permissions in the present directory, listing only writeable files.
$ find -type f ! -perm /ugo=rx
Output:
Finding Files with Executable Permission
The following command will ignore all the files with read and write permissions in the present directory, listing only executable files.
$ find -type f ! -perm /ugo=rw
Output:
Replacing All the Files with 777 Permissions to 644 Using Chmod
The following command will find all the files with permissions “777
” in the present directory and replace them with “644
” using the chmod command.
$ find -type f -perm 777 -print -exec chmod 644 {} \;
Replacing All the Files with 777 Permissions to 755 Using Chmod
The following command will find all the files with permissions “777
” in the present directory and replace them with “755
” permissions.
$ find -type f -perm 777 -print -exec chmod 755 {} \;
Find and Remove a Single File
The following command will search for the specified file (ex: “file.txt
“) in your present directory and remove it from your system using the rm command.
$ find -type f -name "file.txt" -exec rm -f {} \;
Find and Remove Multiple Files with the Same Extension
The following command will search for all text files in your present directory and remove them from your system.
$ find -type f -name "*.txt" -exec rm -f {} \;
Listing All the Empty Files
The following command will search for empty files (0 bytes) and list them on the screen.
$ find -type f -empty
Listing All the Hidden Files
The following command will list all the hidden files that start with “.
” in their filenames in your present directory.
$ find -type f -name ".*"
Finding Single or Multiple Files Owned by the Root
The following command will search for the text file (ex: “file.txt
“) in the “/
” directory owned by the root user.
$ find / -user root -name file.txt
Use the following command to list all the files and directories owned by the root user in the “/
” directory.
$ find / -user root
Finding Single or Multiple Files Owned by the User
The following command will search for the text file (ex: “file.txt
“) in the “/home/
” directory owned by the “linuxtldr
” user.
$ find /home/ -user linuxtldr -name file.txt
Use the following command to list all the files and directories owned by the “linuxtldr
” user in the “/home/
” directory.
$ find /home/ -user linuxtldr
Finding Single or Multiple Files Based on Group
The following command will search for the text file (ex: “file.txt
“) in the “/
” directory that belongs to the “www
” group.
$ find / -group www -name file.txt
The following command will list all the files or directories that belong to the “www
” group in the “/
” directory.
$ find / -group www
Find All the Files Modified 1 Hour Back
The following command will list all the files or directories modified 1 hour ago in the present directory.
$ find -mmin -60
Find All the Files Modified 10 Days Back
The following command will list all the files or directories modified 10 days ago in the present directory.
$ find -mtime 10
Find All the Files Modified in the Last 10 to 20 Days
The following command will list all the files or directories modified between the last 10 and 20 days in the present directory.
$ find -mtime +10 -mtime -20
Find All the Files Accessed 1 Hour Earlier
The following command will list all the files that were accessed 1 hour ago in the present directory.
$ find -amin -60
Find All the Files Accessed 10 Days Earlier
The following command will list all the files that were accessed 10 days ago in the present directory.
$ find -atime 10
Find All the Files Accessed in the Last 10 to 20 Days
The following command will list all the files that were accessed between the last 10 and 20 days in the present directory.
$ find -atime +10 -atime -20
Find All the Changed Files in the Last 1 Hour
The following command will list all the files that have been changed in the last 1 hour in the present directory.
$ find -cmin -60
Find All the Changed Files in the Last 24 Hours
The following command will list all the files that have been changed in the last 24 hours in the present directory.
$ find -cmin -1440
Find All the Changed Files in the Last 24 to 48 Hours
The following command will list all the files that were changed between the last 24 and 48 hours in the present directory.
$ find -cmin +1440 -cmin -2880
Find All the Files Created Yesterday
The command below locates all files modified yesterday in the “/path/to/search/directory
“:
$ find /path/to/search/directory -daystart -mtime 1
Alternatively, you can also use the following command to get more control over the results:
$ find /path/to/search/directory -type f -newermt $(date -d yesterday '+%Y-%m-%d') ! -newermt $(date '+%Y-%m-%d')
Whereas,
- “
/path/to/search/directory
” is the directory from which you wish to start the search. - “
-type f
” ensures that you are exclusively searching for standard/regular files. - “
-newermt $(date -d yesterday '+%Y-%m-%d')
” specifies that the modification time should be more recent than yesterday. - “
! -newermt $(date '+%Y-%m-%d')
” ensures that the modification time does not exceed today’s date.
To experiment, create a file with yesterday’s date using the following touch command:
2023-05-24 10:00
“, with yesterday’s date and time when performing this step.$ touch -a -m --date="2023-05-24 10:00" created_yesterday.txt
Then find the file mentioned above using the previously provided command.
Finding Files with a 10MB Size
The following command will search for all files over 10 MB in size in your present directory.
$ find -size 10M
Finding Files Between 10 and 100 MB in Size
The following command will search for all files between 10 and 100 MB in size in your present directory.
$ find -size +10M -size -100M
Find and Delete 1024MB Sized files
The following command will search for all files with a size of 1024 MB in your present directory and then delete them.
$ find -type f -size +100M -exec rm -f {} \;
Find and Delete Files with Specific Sizes and Types
The following command will search for all “.mp4
” files with a size of 50 MB in your present directory and then delete them.
$ find -type f -name *.mp3 -size +50M -exec rm {} \;
That was the last example.
I hope this descriptive list of examples is useful to you.
If you have any other examples that need to be added to this list, please let us know in the comment section.
Join The Conversation
Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.