The Linux find command is a powerful tool that enables system administrators to locate and manage files and directories based on a wide range of search criteria. It can find directories and files by their name, their type, or extension, size, permissions, etc.

Besides locating files and directories, combining the find command with others enables you to take action on the results. Adding the -exec option enables sysadmins to run external commands and perform actions like copying, moving, deleting, or changing permissions of the files matching the specified criteria such as size, name, etc.

In this article, we will start by explaining the basic Linux find commands with examples. This will show you how to find files and directories. We will then show you how to use the -exec option to act on the files or directories based on their size, permissions, etc.

The general syntax for the find command is

find {path} {name -of-file or dir-to-search} {action-to-take}

Where,

  • path specifies the directory.
  • name-of file or dir-to-search : Name of the file or directory to look for
  • action-to-take: such as copy, delete, move, etc.

In this tutorial, we will explain how to locate files and directories matching specified patterns. We will also see how to perform actions on the files or directories that the find command locates. The guide is based on Ubuntu but is applicable to most Linux distributions and versions.

Find files and Directories

Find specific files by name or extension

To look for a specific file, run the following command from the root (/). The command contains the exact name for the file you are searching for.

find . -name file22.txt

Output

./test/file22.txt
./sales/file22.txt

Please note that the results include the path. This is important if you don’t know the directory where the file is located, or when it is in more than one place.

You can also search for the file in another directory while still in the current location. In this case, you need to provide the path for the directory where you want to search.

find ./test -name file22.txt

Looking for specific files in another directory

In our case, we will look for all those starting with the letters file in the test directory.

find ./test -name file*

Output

./test/file22.txt
./test/file30.doc
./test/file1.txt
./test/file5,doc

Search for files by extension

To find a file in Linux with a certain extension, add it to the command.

find . -name *.txt

Output

./test/subtest/subfil.txt
./test/file22.txt
./test/file1.txt
./home1/files32.txt
./home2/file22.txt
./qa/tags.txt

Find files and directories by name

Use the command below to look for files and directories starting with the letters qa. In our computer, we have the qatree.txt and qa.txt files as well as a directory by the name qa.

If we run the command;

find ./ -name "qa*"

It returns the following output

./test/qatree.pdf
./test/qa.txt
./home/qa

The command returns both the files and directories matching the search criteria. To find files or directories only, you need to specify this in the command.

Find files or directories only

For files only, use the type f switch.

Files only

find ./ -type f -name "qa*"

Output

./test/qatree.pdf
./test/qa.txt

Directories only

Add the type d option to locate directories only.

find ./ -type d -name "qa*"

Output

./home/qa

Case insensitive find command

All searches with -name switch are case sensitive and will not give results with capital letters. To get all cases, use the -iname option.

find ./ -iname "qa*"

Output

./test/qatree.pdf
./test/qa.txt
./test/QAtest.txt
./home/qa

Search for a file from multiple directories

To find the files in different directories, add their paths in the command. In our case, we will check in the test and numeric directories.

find ./test ./numeric -name file22.txt -type f

Output

./test/file22.txt
/root/numeric/file22.txt

Find multiple files with different extensions from all directories

You can use the find command to locate multiple files that share the different extensions such as *.doc, *.txt *.pdf, etc. This can be done separately, one extension at a time, or using just one command that includes all the desired extensions.

find . -type f ( -name "*.txt" -o -name "*.pdf" -o -name "*.doc" )

output

./test/subtest/subfil.txt
./test/qatree.pdf
./test/file22.txt
./test/qa.txt
./test/file30.doc
./books/acro.pdf
./data1/FILE22.txt
./docs/files32.txt

Find files containing certain text

Sometimes, you want to access a file containing certain text but cannot recall its file name or location. This command allows you to find all the files containing your target text.

To look for all the files containing the word hyperconvergence”, use;

find / -type f -exec grep -l -i "hyperconvergence" {} ;

Output

/root/numeric/hci
/root/HCIP

The –i option enables the command to ignore cases and will find the text whether capitalized or not i.e. hyperconvergence, Hyperconvergence , etc.

To look for the files in a specific directory, simply add them to the command

find ./numeric -type f -exec grep -l -i "hyperconvergence" {} ;

Output

./numeric/hci

Explore more grep command examples.

Find Files and Directories Based on Size

You can find all files or directories that are smaller, equal or greater than a certain size, within a certain range or empty. Use the appropriate size format depending on the type of files or directories you are searching for.

Size options include;

c – bytes

k – kilobytes

M – Megabytes

G – Gigabytes

Find files of a certain size – equal to 30MB

To Search find all 30MB files

find / -size 30M

Find files larger than a specified size

find -size +2M

Output

. /Downloads/ubuntu18043.iso 
./.cache/pip/http/6/e/3/8/3/6e38383f41850c3e5705d48116bc52f81b92a80500f414305057 7a9c

Find files less than 10MB in the current directory

find . -type f -size -10M

Find files with sizes between 100-200MB

When looking for files within a specific range such as between 100 and 200 MB

find / -size +100M -size -200M

Look for directories larger than 20kb

find / -type d -size +20k

Find empty files and directories.

Files

find ./ -type f -size 0

or

find ./ -type f –empty

Directories

find ./ -type d –empty

Find files by age or modification time

Find files older than n days

find /path/ -type f -name '*.txt' -mtime +8

The -mtime +8 will look for txt files that are older than 8 days.

By modification date

This will look for files modified within the last 17 hours

find . -mtime -17 -type f 

Looks for directories modified within the last 10 days

find . -mtime -10 -type d 

Find files based on access or modification

Find files based on date or time accessed. This allows you to see files that have or haven’t been accessed within a specified period.

To see files that have not been accessed within the last 10 days in the home directory.

find /home -atime +10

Files accessed exactly 10 days ago

find /home -atime 10

Accessed within the last 10 days

find /home -atime -10

Find files modified within the last n days

You can also look for the files in the /home directory modified within the last 10 days using the command;

find /home -mtime -10 

Find files modified within a specific period.

For example, all files modified between 6 and 15 days ago in the home directory.

find /home -type f -mtime +6 -mtime -15

Files and directories accessed within the last 10 minutes

To find the files accessed within the last 10 minutes, use the -amin option.

find . -amin -10 -type f

Output

./.bash_history
./.profileroot@ubuntu1804:~#

Directories accessed within the last 10 minutes

find . -amin -10 -type d

Find files matching specific permissions

Syntax

$ find -perm mode

Where mode is the permission which is either numeric such as 644, 655, 700, 777, etc, or letters such as u=x, a=r+x, etc.

You can specify the mode in the following three different ways.

  1. Without a prefix when you want to find files with the exact permissions specified.
  2. With “-“ for files with at least the specified permission. This returns files with the specified as well as additional higher permissions.
  3. Using “/” requires specifying the owner or group with the permission to the file.

Find files with permission 777

find -perm 777

Output

./file1

Find files with at least 766
find -perm -766

Output

./file2
./file1

The command looks for all files in which the

  • The file owner has read/write/execute permissions.
  • Group has read/write permissions
  • Others have read/write permission

As such, it returns two files that meet this criterion – file1 and file2. The files do not need to have the exact 766 permissions and can have additional ones as long but must have at least the specified.

Find files writable by the owner

We will now use the “/” to looks for files writable by either their owner, or group, or others.

find -perm /222
.
./file2
./file3
./file1

The above looks for files that are writable by either their owner or group.
This returns files that are writable by either but not necessarily both. To see files, where both have writable permissions, use the – prefix.

find -perm -220

Output

./file2
./file1

Find files owned by a user

Find all files owned by Jack

find /home -user jack

Output

/home/jack
/home/jack/examples.desktop
/home/jack/.gnupg
/home/jack/.gnupg/private-keys-v1.d
/home/jack/ans.cgi

Find specific files owned by a user

Find all text files owned by Jack

find /home -user jack -iname "*.txt"

Output

/home/jack/docs/file32.txt

Find and list files and directories together with their permissions

find -name "*.conf" | ls -l

Output

total 120
drwxr-xr-x 2 root root 4096 Dec 31 13:19 backup2
drwxr-xr-x 2 root root 4096 Jan 4 11:57 Desktop
drwxr-xr-x 2 root root 4096 Dec 31 11:50 Documents
drwxr-xr-x 2 root root 4096 Dec 31 12:27 Downloads
-rw-r--r-- 1 root root 0 Jan 4 12:02 file22.doc
drwxr-xr-x 2 root root 4096 Jan 4 12:14 file23
-rw-r--r-- 1 root root 0 Jan 4 12:23 file23.txt

Find and act on the results

In this section, we will look at how you can act on the files that match the pattern specified in the find command.

Find files and change permissions

Find and change permissions of certain file types. In our case, we will work with PHP files with different permissions as shown below.

jack@ubuntu1804:~/ver$ ls -la

total 8
drwxrwxr-x 2 jack jack 4096 Jan 3 14:11 .
drwxr-xr-x 8 jack jack 4096 Jan 3 14:05 ..
-rwxr-xr-x 1 jack jack 0 Jan 3 14:06 ans.php
-rw-rw-r-- 1 jack jack 0 Jan 3 14:11 base.php
-rwxr-xr-x 1 jack jack 0 Jan 3 14:06 query.php
-rw-rw-r-- 1 jack jack 0 Jan 3 14:11 qust.php
-rwxr-xr-x 1 jack jack 0 Jan 3 14:06 text.php
-rw-rw-r-- 1 jack jack 0 Jan 3 14:11 vary.php

We will now look for all the PHP files (above) and replace their permissions with 755

find ver -name "*.php" -type f -exec chmod 755 {} ;

The command looks for PHP files in the ver directory and then sets their permission to 755 (rwxr-xr-x)

Results

jack@ubuntu1804:~/ver$ ls -la
total 8
drwxrwxr-x 2 jack jack 4096 Jan 3 14:11 .
drwxr-xr-x 8 jack jack 4096 Jan 3 14:05 ..
-rwxr-xr-x 1 jack jack 0 Jan 3 14:06 ans.php
-rwxr-xr-x 1 jack jack 0 Jan 3 14:11 base.php
-rwxr-xr-x 1 jack jack 0 Jan 3 14:06 query.php
-rwxr-xr-x 1 jack jack 0 Jan 3 14:11 qust.php
-rwxr-xr-x 1 jack jack 0 Jan 3 14:06 text.php
-rwxr-xr-x 1 jack jack 0 Jan 3 14:11 vary.php

Find and change file and directory permissions

Find files with 644 permissions and change them to have 655 permissions

find . -type f -perm 644 -exec chmod 655 {} ;

You can also look for directories with 644 permissions and replace this with 755.

find . -type d -perm 644

Output

./docs   

The docs folder has 644 permissions

drwxrwxr-x 2 jack jack 4096 Jan 3 12:45 docs

To set it to 755, we run

find . -type d -perm 644 -exec chmod 755 {} ;

Now we can check again to see what exactly 755

find . -type d -perm 755

Output
.
./docs

From above we can see the root and docs directories have the 755 permissions.

Ls –la command gives the following details

drwxr-xr-x 2 jack jack 4096 Jan 3 12:45 docs

Find and copy files or directories

Find and copy a specific file to a directory

The command below will find the file22.txt file and copy it to the ~/tmp/images directory.

find -iname file22.txt -exec cp {} ~/tmp/images ;

Find and copy one type of files to a directory

To find files such as images with jpg extension in the current directory and copy them to a different location like an images folder, use;

find -iname '*.jpg' -exec cp {} ~/tmp/images ;

This will find and copy all the jpg files to the ~/tmp/images folder.

Find and copy one file to many directories

Find and copy a single to multiple directories.

find ~/tmp/dir1/ ~/tmp/dir2/ $HOME/3/ -maxdepth 0 -exec cp ~/numeric/hci {} ;

This will find the file hci file and copy it to the three directories of /tmp/dir1/ /tmp/dir2/ and $HOME/3/

Find and move files to a different directory

To move a known file from a directory to another. To move the universal.php file;

find ~/folder/ -type f -name universal.php -exec mv {} ~/numeric/ ;

Search and move files with a certain extension to a different folder

find ~/numeric/ -type f -name '*.php' -exec mv {} ~/folder/ ;

Find certain files and move to a specific different folder

find -type f -name uni*.* -exec mv {} ~/unifiles/ ;

The command looks for all the files with names starting with uni and having any extension. It then moves them to the directory /unifiles/

Find and move files based on age

Find and move files older than specified days to a different location such as the archive.

find /path/ -type f -name '*.pdf' -mtime +20 -exec mv {} /backup1/ ;

This will look for pdf files older than 20 days and move them to the backup1 directory.

Find and delete files and directories

The syntax for finding and removing files or directories in the current directory is

find . -type f -name "file to delete" -exec rm -f {} ; to delete files only or

find . -type d -name "dir-to-delete" -exec rm -rf {} ; to delete directories only

Find and delete specific files only

To find and delete files starting with til, use;

find . -type f -name "til*" -exec rm {} ; 

To find and delete directories starting with til

find . -type d -name "til*" -exec rm {} ;

Remove both files and directories

find . -name "til*" -exec rm -rf {} ; 

This will remove both files and directories starting with the letters til.

Delete by extension

Below is how you can locate and delete all txt files in the current directory. Replace the txt with another extension such as bak, pdf or any other that you want to remove.

find . -type f -name "*.txt" -exec rm -f {} ;

In case you want the system to prompt you to confirm before deleting each file, add the -i option as below.

find . -type f -name "*.txt" -exec rm -i {} ;

Output

rm: remove regular empty file './File22.txt'? y
rm: remove regular empty file './file22.txt'? y
rm: remove regular empty file './file23.txt'?

By default, the -rm will not remove the directories and you need to use the –r option to ensures a recursive removal. This ensures the deletion of empty directories and those containing files. The -f option forces the removal and is used for both the files and directories.

Find and delete files older than n days

Find and delete backup files older than 20 days from the current directory.

find . -type f -name '*.bak' -mtime +20 -exec rm {} ;

This will delete all .bak files older than 20 days.

Find and delete directories only

To delete a directory called dir22

find . -type d -name dir22 -exec rm -rf {} ;

Ensure that you match the directory name case or use the -iname option.

find . -type d -iname Dir22 -exec rm -rf {} ;

Removes both Dir22 and dir22

To confirm before deletion, use the -i option.

find . -type d -iname Dir22 -exec rm -rf -i {} ;

Output
rm: remove directory ‘./Dir22’? n
rm: remove directory ‘./dir22’? y

In our case, we typed n for directory Dir22 which will not be deleted and y for the dir22 which will now be removed.

Find and remove empty files

You can use any of the following commands to locate empty files and delete them automatically.

find ./ -type f -size 0 -exec rm -f {} ;

or
find ./ -type f -size 0 | xargs rm -f
or
find ./ -type f -size 0 –delete

Find and remove empty directories

To remove empty directories we will use the d option.

find ./ -type d -empty -exec rmdir {} ;

Another alternative is to use delete instead of remove.

find ./ -type d -empty -delete

Please note that deleting system or critical files from your computer can damage the operating system or applications or lead to loss of your important data.

To avoid accidental deletions, it is best practice to use a non-root user account. Also, ensure that you are deleting the right files that are not useful and that you have a backup of all your data files just in case.

What’s next?

Go ahead and try the above Linux find commands in your lab or NON PRODUCTION environment. See if you can use some to automate the file system cleanup with Crontab. And, to master Linux, check out this online course.