35+ Advance Examples of the Find Command in Linux

Linux TLDR
Last Updated:
Reading time: 7 minutes

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.

Table of Contents

Tutorial Details

DescriptionFind
Difficulty LevelModerate
Root or Sudo PrivilegesNo
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisitesfind
Internet RequiredNo

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:

Locating file using find command

To print the relative path of the file, use the β€œ-name” flag with the filename.

$ find -name file.txt

Output:

Locating file and printing its relative path using find command

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:

Locating file and printing its absolute path using find command

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:

Locating file in different directory using find command

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:

Locating file in different directory using find command but with sudo privileges

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:

Locating file without option resulting case-sensitive results using find command

Use the β€œ-iname” flag to ignore case-sensitivity while searching the specified file.

$ find -iname file.txt 

Output:

Locating file with option resulting case-insensitive results using find command

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:

Showing the directory and file structure for example

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:

Locating all files with that are matching using to specified syntax find command

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:

Locating all files with that are matching using to specified syntax but in specific directory using find command

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:

Locating directory using find command

Use the β€œ-name” flag with the directory name to get the relative path of the directory.

$ find -name Downloads

Output:

Locating directory and listing its relative path using find command

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:

Locating directory and listing its absolute path using find command

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:

Locating file and directory sharing same name using find command

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:

Locating only directory ignoring all files in the result using find command

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:

Locating index html file in html directory using find command

Search for all files with the β€œ.html” extension in the β€œ/var/www/html” directory.

$ find /var/www/html/ -type f -name "*.html"

Output:

Locating all html files in html directory using find command

Finding Files with Specific Permissions

The following is a list of multiple text files, including one with the permission β€œ777β€œ.

$ ls -l file*

Output:

Listing text files with their permissions using ls command

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:

Locating files based on their assigned permissions using find command

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:

Locating read only files using find command

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:

Locating files only with writable permissions using find command

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:

Locating executable only files using find command

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:

πŸ“
Remember to replace the provided date and time, β€œ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.