Beginners Guide for 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
Host System and ArchitectureUbuntu 22.10 (x64)
OS Compatibility Ubuntu, Manjaro, Fedora, etc.
Prerequisitesfind
Internet RequiredNo
Discussed Tools in this Article

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
file.txt

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

$ find -name file.txt
./file.txt

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 
/home/linuxtldr/file.txt

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 
find: β€˜/home/jr’: Permission denied
/home/linuxtldr/file.txt

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 
/home/linuxtldr/file.txt

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
file.txt  File.txt
$ find -name file.txt 
./file.txt

Use the “-iname” flag to ignore case-sensitivity while searching the specified file.

$ find -iname file.txt 
./file.txt
./File.txt

Also Read: Beginners Guide for Whereis Command in Linux

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 
Desktop    Downloads  Music     Public  Templates
Documents  file.txt   Pictures  snap    Videos
$ ls Documents/
file.txt

If you search for the text file using the find command, it will return all the matches.

$ find /home/linuxtldr/ -type f -name *.txt
/home/linuxtldr/file.txt
/home/linuxtldr/Documents/file.txt

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/*"
/home/linuxtldr/file.txt

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
Downloads

Use the “-name” flag with the directory name to get the relative path of the directory.

$ find -name Downloads
./snap/snapd-desktop-integration/14/Downloads
./Downloads

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
/home/linuxtldr/snap/snapd-desktop-integration/14/Downloads
/home/linuxtldr/Downloads

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
./snap/snapd-desktop-integration/14/Downloads
./Downloads
./Documents/Downloads

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
./snap/snapd-desktop-integration/14/Downloads
./Downloads

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

Search for all files with the “.html” extension in the “/var/www/html” directory.

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

Also Read: Beginners Guide for Chmod Command in Linux

Finding Files with Specific Permissions

The following is a list of multiple text files, including one with the permission “777”.

$ ls -l file*
-rw-rw-r-- 1 linuxtldr linuxtldr 0 Nov 28 14:35 file1.txt
-rwxrwxrwx 1 linuxtldr linuxtldr 0 Nov 28 14:35 file2.txt
-rw-rw-r-- 1 linuxtldr linuxtldr 0 Nov 28 14:35 file3.txt

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
./file2.txt

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 

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

Also Read: Beginners Guide for Which Command in Linux

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

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

Also Read: Beginners Guide for ls Command in Linux

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 ".*"

Also Read: Beginners Guide for Chown Command in Linux

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

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.