10+ Ways to Use the find Command in Linux

Have you ever found yourself frantically searching for an important file, only to realize that you wasted many hours trying to look in every directory but the file is nowhere to be found? Well, this is a common problem that most Linux users face, especially new users with a huge number of disorganized files and directories. In this article, we explain how you can search for files and directories in different ways using the find command in Linux.

What is the find Command in Linux

The find command is one of the most important tools that help Linux users search for their files and folders with a variety of options. You can even execute shell commands on the search items returned (more on it later!). The syntax to search for files and directories using the find command is:

find <options> <location> <expression>

Here is how we will explain the above syntax:

  • <options> are the various options used to refine the search
  • <location> specifies the location in memory for the find command to look in
  • <expression> specifies the search item to look for

Some of the common options to pair with the find command are:

OptionsDescription
-nameThis option is used to specify the name of the file or folder.
-typeThis specifies the type of items to look for.
-execThe exec flag is used to execute shell commands on the output.
-sizeThis flag filters the items based on their memory occupancy size.
-permThis flag filters files and directories based on their permissions.
-userThis flag is used to search for files and directories owned by a specific user.

Best Ways to Use the find Command in Linux

The most common way to search for files and directories with the find command in Linux is with their respective name. If you do not know the exact file name, the find command being very versatile can be used to search for files even with the part of a name.

1. Find Specific Files with Matching Name

To search for files and directories with the find command, use the -iname flag. The benefit of searching with this flag is that it will search for the given name ignoring the case of the file name. The syntax to use the find command with the -iname flag is:

find <location> -iname <search_name>

For example, if you want to search for the file “test.txt” in the current directory, use this command:

find . -iname "test.txt"

Here, the ‘.’ represents the current directory in terms of the relative addressing mode for the file location. You can even use the absolute addressing mode for the file location instead if you find the relative mode difficult to understand.

searching for files matching with find in Linux

The “-iname” flag will ignore the case of the file names. To search for file names exactly matching the file name, simply replace the -iname flag with the -name flag:

find <location> -name <name_to_search>

2. Find Files by Name Pattern in Linux

Sometimes you need to work with an important file but can’t remember the complete name of the file. Even in this situation, the find command can be your savior. Simply use wildcards to match the pattern for your file name. Wildcards will then try to match the given pattern with all the available names. Suppose you need to look for a file “test-101.py” in the current directory, you can use the following command:

This will list out the different files beginning with the name ‘test’ in their file names as shown below:

find . -type f -iname "test*"

Here, type f is added to specify the search item type as files. Also, make sure to use the wildcard expression in a pair of double quotes to prevent any errors.

3. Find Either Files or Directories with Matching Names

Generally, the find command searches for all files and directories matching the name. To search for only files, use the “-type f” option with the basic syntax:

find <location> -type f -name <name_to_search>

For example, the following command will show all the files with the name “test”:

find . -type f -iname "test*"

To search for only directories use the “-type d” flag like:

find <location> -type d -iname <name_to_search>

For example, the following command will show all the directories with the name “test”:

find . -type d -iname "test*"

4. Find Files with Specific Extension

With the deadly combination of wildcards and the find command, you can even search for files based on their extensions. For example, if you need to search for a configuration file in the current directory, but don’t know the exact name, you can use the command:

find . -iname "*.conf"

5. Find Files and Directories based on Size

When combined with appropriate size descriptors, you can use the find command with the -size flag to find files and directories in Linux based on their size occupied in the memory. Additionally, various size descriptors can also be added to specify the file size limit to search.

The basic syntax to find files in Linux based on size is:

find <location> -type <search_item_type> -size <symbol><file_size><size_descriptor>

The above syntax is the same as the previous ones, except for the “-size <symbol><file_size><size_descriptor>” part. Here’s an explanation for this part:

  • For the <symbol> part you can specify either ‘+’ or ‘-‘ or even leave it blank. Here, ‘+’ means to look for files/directories exceeding the given size, and ‘-‘ specifies to find files/directories below the given size. If it is left blank, then the find command will look for the files matching exactly the size.
  • <file_size> specifies the size of the file/directory.
  • <size_descriptor> refers to the file size range you want to look for.

Various types of size descriptors you can use are:

Size DescriptorDescription
cSpecifies find command to look for size in bytes
kSpecifies find command to look for size in kilobytes
MSpecifies find command to look for size in megabytes
GSpecifies find command to look for size in gigabytes

The above syntax may seem very complicated for some users, but some examples will make it easier to understand.

  • If you need to view directories equal to 4 kB in the Documents directory, use this command:
find ~/Documents -type d -size 4k
  • Or if you are low on space and need to view files exceeding 100 MB in the Documents directory, use this command:
find ~/Documents -type f -size +100M
  • If you need to view files less than 10 MB in the Documents directory, use this command:
find ~/Documents -type f -size -10M
  • If you need to view empty files in the Documents directory, use this command:
find ~/Documents -type f -size 0

6. Find Files and Directories Matching Specific Permission

Permissions in Linux are of utmost importance in Linux to protect both files and directories from unintentional access. To know more, check out our comprehensive guide on Linux permissions.

To find files and directories based on the permission set, use this syntax:

find <location> -type <search_item_type> -perm <permission_set>

In the above syntax, you can use either the absolute mode of addressing or the relative mode for <permission_set>.

Suppose you need to find files with read-only permission for root, use this command:

find ~/Documents/test/ -type f -perm 400

Or if you want to find files with both read and write permissions for all users, use this command:

find ~/Documents/test/ -type f -perm a=r+w

7. Find Files and Directories owned by a User

As we explained previously, you can search for files based on permissions set for the file with the find command in Linux. Now we will show you how to view the files owned by a particular user. The basic syntax remains mostly the same except for the new -user flag which specifies the find command to search for files owned by that user:

 find <location> -type <search_item_type> -user <username>

For example, if you want to see the files owned by the ‘test’ user in the current directory, use this command:

find . -type f -user test

8. Using the find command with -exec flag

With the -exec flag, you can execute actual shell commands with the find command and thus boost its functionalities multiple times. The -exec flag executes the shell commands on every search result returned. The basic syntax to use the find command with the -exec flag is:

find <location> <options> -exec <shell_command> {} ;

In the above syntax, the {} are used to inject the search results found by the find command inside the shell command. An example will make it more clear.

Suppose, you want to copy a file named test.txt inside the current directory to the /tmp/docs/, use this command:

find . -iname renaming.sh -exec cp {} ./testing \;

In the above example, when the “renaming.sh” file is found, it is directly plugged in place of the “{}” where the normal copy procedure is followed. The last semi-colon (;) demarcates the termination of the command.

Let us now see how multiple files are handled by the -exec flag. Say you want to move all .pdf files in the current directory to a new directory named “ftesting” in the home directory, you can use this command:

find .-name "*.pdf" -exec mv {} ./testing + \;

If you want to rename using the mv command, check out our article on how to rename a file in Linux where we have discussed four different methods to rename files.

9. Find and Delete Files and Directories in Linux

Sometimes you may need to delete specific files/directories in Linux. You’d think that a complicated shell script needs to be written for it. However, with the power of the -exec command, you can easily delete files or directories filtered by the find commands based on the specified rules. The basic syntax to delete files using the find command is:

find <location> <options> -type <search_item_type> -exec rm -vf {} ;

Some users may find the above command difficult to grasp. To make things simpler, you can use the -delete flag instead of the entire -exec part to delete the items. The syntax to delete files using the -delete flag with the find command in Linux is:

find <location> <options> -type <search_item_type> -delete

Delete File and Directory with Exact Name

Deleting a file/directory works the same as using the find command to search for it. The syntax to delete a file using find is:

find <location> <options> -type <search_item_type> -name <file_to_delete> -delete

Say you want to delete the “test.txt” file in the current directory, you can use this command as:

find . -type f -name test.txt -delete

Delete Files with Partially Matching Name

If you delete a file and all you can remember is a part of the name, then you can use wildcards to find all sorts of matching names with the given file name. The syntax to delete files and directories with a partial name with the find command in Linux is:

find <location> <options> -type <search_item_type> -name <name_with_wildcard> -delete

For example, to delete all files beginning with the name “test”, use this command:

find . -type f -name "test*" -delete

Delete Files with same Extension

When you need to delete all files with the same extension in a directory, all you need to do is add the -delete flag at the end. The syntax to delete all files with the same extensions is:

find <location> -type f -name "*.<extension>" -delete

For example, if you want to delete all .pdf files in the current directory, you can use this command:

find . -type f -name "*.pdf" -delete

If you want to know about more ways to delete files and directories in Linux, check out articles on how to delete a file in Linux and how to delete a directory in Linux. We recommend you to always first run the find command without te delete flag. Once you are happy with the results, then you can apply the -delete flag to finally delete the items.

Delete Empty Files and Directories with find Command

Deleting empty files and directories is fairly simple. Simply add the -delete flag at the end for the command to find empty folders and files in Linux. The syntax to delete empty files and folders is:

find <location> -type <search_item_type> -size 0 -delete
Comments 0
Leave a Reply