How to Use The find Command in Linux

Tutorial Jan 16, 2021

The find command is one of the oldest and most powerful tools offered by Linux. It is available by default on almost all Linux distributions out of the box. The command is used for searching for files and folders in a directory hierarchy based on conditions we define, and optionally it allows us to perform actions on the search result. The find command is a vital tool for anyone trying to live in the Linux terminal world.

COMMAND SYNTAX

Before diving into the examples, let us get a little familiar with the overall syntax of the find command,

find [options] [paths] [expressions]

[options] control how symbolic links, debugging options, and optimization methods are handled.

[paths] is the directory/directories where find will perform the search on.

[expressions] is where we define search patterns and actions for the search results using expressions (tests, actions, operators etc..).

Now let's look at an example where we want to search for files ending with .css in the ~/public directory,

find -L ~/public -name "*.css"

Here,
-L is in the [options]part of the command
~/public is in the [paths] part of the command
-name "*.css" is in the [expressions] part of the command

The default behaviour of find is to never follow symbolic links. The -L flag will make the find command follow symbolic links.

TESTS

Tests contain a list of expressions that we will use in the [expressions] part of the find command. The -name we used in the last example command is a test. What tests do is, when find iterates through the files and directories, the tests we declare will return a true or false value, and that determines if that specific file or directory should be in the search result or not.

Some of the tests are -name, iname, -type, -perm, -empty, -executable, -readable, -writable, -size, -user, -group

1. Search For a Specific File in Current Directory

Performing find searches on the current directory can be done by using . (period) as the [path]. The -type test with a value of f will make sure we are looking for file(s). And the file name to search for will be given to the -name test in the [expressions] part of the command.

find . -type f -name recipies.txt

If the file name has spaces in it, escape the spaces using \ (backslash) or enclose the name in single or double quotes,

find . -type f -name my\ recipies.txt
find . -type f -name 'my recipies.txt'
find . -type f -name "mom's recipies.txt"

The -type test accepts these values,
f = plain files
d = directories
l = symbolic links
b = block devices
c = character devices
p = named pipes
s = sockets

2. Search Inside a Specific Directory

Performing find searches on a specific directory can be done by providing the full path to the directory in the [path] part of the command.

find /home/reiri/ -name recipies.txt

Case insensitive find searches can be done by using the -iname test instead of -name in the [expressions] part of the command.

find /home/reiri/ -iname butter
/home/reiri/Butter
/home/reiri/BUTTER
/home/reiri/butter

4. Find Directories

By giving the -type test a value of d, we can search for directories only. In the example shown below, we are searching for folders named scripts inside the ~/public/ folder.

find ~/public -type d -name scripts

5. Search Based on File Type

We can use some simple regex with the -name test for searching for any specific file type. In the example below, we are searching for all the css files inside the ~/public/ folder

find ~/public -type f -name "*.css"

6. Search For Hidden Files and Directories

This is similar to searching for files based on their file type. Here, the regex will be .*, and the -type test is omitted to search for files and directories starting with a . (period).

find ~/public -name ".*"

7. Search Based on File Permissions

For searching for files (-type f) and directories (-type d) based on their permissions, use the -perm test in the [expressions] part of the command. In the example shown below, we are searching for files with permission 0777 in the ~/public folder.

find ~/public -type f -perm 0777

And for searching for directories with the 0777 permission inside the ~/public folder,

find ~/public -type d -perm 0777

Further reading: Understanding Linux Permissions

8. Search For Empty Files And Directories

We can use the -empty test for searching for empty files (-type f) and directories (-type d). In the example shown below, we are searching for empty files inside the ~/public folder.

find ~/public -type f -empty

And for searching for empty directories inside the ~/public folder,

find ~/public -type d -empty

9. Search Based on User

For searching for files and directories based on who owns it, use the -user test. In the example shown below, we are searching for files and directories owned by userb inside the ~/public folder.

find ~/public -user userb

10. Search Based on Group

For searching for files and directories based on their group, use the -group test. In the example shown below, we are searching for files and directories belonging to the group friends inside the ~/public folder.

find ~/public -group friends

11. Search For Read Only Files

For searching for files and directories readable by the current user, use the -readable test. Omitting the -type test will give us a list containing files and directories.

find ~/public -type f -readable

12. Search For Executable File

For searching for files and directories that are executable by the current user, use the -executable test. Omitting the -type test will give us a list containing files and directories.

find ~/public -type f -executable

13. Search For Writeable Files

For searching for files and directories that are writeable by the current user, use the -writable test. Omitting the -type test will give us a list containing files and directories.

find ~/public -type f -writable

14. Search Based on File Size

For searching for files based on their file size, we can use the -size test. In the example shown below, we are searching for files that are 1GB in size inside the ~/public folder.

find ~/public -size 1G

For searching for files larger than 1GB, prefix the value of -size with a +,

find ~/public -size +1G

And for searching for files smaller than 1GB, prefix the value of -size with a -,

find ~/public -size -1G

Available size units for -size,
b = 521 byte blocks (this is the default if no suffix is used)
c = Bytes
w = Two byte words
k = Kilobytes
M = Megabytes
G = Gigabytes

15. Search Based on Modification Date

For searching for files and directories based on their file modification date, we can use the -ctime test. In the example shown below, we are searching for files that were modified exactly 10 days ago inside the ~/public folder.

find ~/public -type f -ctime 10

For searching for files that were last modified more than 10 days ago, prefix the value of -ctime with a +,

find ~/public -type f -ctime +10

For searching for files that were modified within the last 10 days, prefix the value of -ctime with a -,

find ~/public -type f -ctime -10

16. Search Based on Modification Time

For searching for files and directories based on their file modification time, we can use the -cmin test. In the example shown below, we are searching for files that were modified exactly 60 minutes ago inside the ~/public folder.

find ~/public -type f -cmin 60

For searching for files that were last modified more than 60 minutes ago, prefix the value of -cmin with a +,

find ~/public -type f -cmin +60

For searching for files that were modified within the last 60 minutes, prefix the value of -cmin with a -,

find ~/public -type f -cmin -60

ACTIONS

Like tests, actions are a list of expressions that we can use in the [expressions] part of the find command. Actions make it possible for us to perform an action on the search results we get from find.

Some of the actions are, -delete, -exec, -ls, -ok, -print

1. Print The Search Results

For printing out the full pathname of the matching files and directories, we can use the -print action. In the example shown below, we are printing out all the files that belong to the user userb inside the ~/public folder.

find ~/public -type f -user userb -print

If we don't explicitly provide the find request with any action, the -print action is applied to it by default. Actions which inhibit the default -print are -delete, -exec, -execdir, -ok, -okdir, -fls, -fprint, -fprintf, -ls, -print and -printf.

2. Delete The Search Results

For deleting the search results we get from the find command, we can use the -delete action. In the example shown below, we are deleting all the files that belong to the user userb inside the ~/public folder.

find ~/public -type f -user userb -print -delete

3. Executing Commands on The Search Results

For executing our own command on each of the search results, we can use the -exec action. In the example shown below, we are deleting all the files that belong to the user userb inside the ~/public folder.

find ~/public -type f -user userb -print -exec rm '{}' \;

Here, rm '{}' \; is the command we are executing for each of the search results that the find command will produce. While the command iterates through the search results, during each iteration, the {} will be holding the filepath to that specific iteration's file. In the above example, we are enclosing the {} inside single quotes, it is to make sure that filepaths containing spaces are handled properly. And finally, the \; (backslash-escaped semicolon) denotes the end of the command.

Note: You can have multiple -exec actions in a find command, but only one instance of {} can exist in one -exec action.

4. Change Permission of Files

In the example shown below, we are searching for files with 777 permission and changing them to 644 inside the ~/public folder.

find ~/public -type f -perm 777 -print -exec chmod 644 '{}' \;

5. Change Permission of Directories

In the example shown below, we are searching for directories with 777 permission and changing them to 755 inside the ~/public folder.

find ~/public -type d -perm 777 -print -exec chmod 755 '{}' \;

OPERATORS

Like tests and actions, operators are a list of expressions that we can use in the [expressions] part of the find command. With operators, we are able to perform logical operations to fine-tune our search.

In the decreasing order of precedence, the operators are, ! (not), -and and -or.

Operation Explanation
! -expr true if -expr is false
-expr1 -and -expr2 -expr2 is not evaluated if -expr1 is false
-expr1 -or -expr2 -expr2 is not evaluated if -expr1 is true.

In the above table, -expr, -expr1 and -expr2 are dummy expression names.

Note: -and when specified implicitly (for example by two tests appearing without an explicit operator between them) or explicitly has higher precedence than -or. This means that find . -name afile -or -name bfile -print will never print afile.

Shown below is an example where we are searching for files larger than 1MB owned by user userb in the ~/public folder,

find ~/public -user userb -and -size +1M

And an example where we are searching for files which belong to either userx or usery.

find ~/public -user userx -or -user usery

Tags

Lime

Simple, like quantum physics.