Linux Commands

How to Search and Find Files Recursively in Linux

This brief tutorial explains how to search and find the files recursively in the Linux operating systems.

After reading this article, you will be able to find any file recursively using the different techniques including a single file search, multiple files search, find files by permissions, and more. This document is optimized for both new and experienced Linux users. All methods are valid for every Linux distribution.

All examples in this tutorial contain screenshots to make it easy for any Linux user to understand and reproduce them.

Finding Files Recursively in Linux

The find command does not need flags to search the files recursively in the current directory. You only need to define the main directory and the file name using the –name option. This command will search the file within the main directory and all subdirectories.

The syntax is simple, as shown in the following:

find <Directory> -name <FileName>

If you want to find the 27.jpg file within your home directory and subdirectories, run the following command as shown in the following screenshot:

find ~/ -name 27.jpg

As you can see, the file was found in the /home/linuxhint/Downloads/recur subdirectory.

An alternative to the previous command is the tree command shown in the following example where you search the same file named 27.jpg within the current directory:

tree -P 27.jpg

As you can see in the previous figure, the format is pretty different. It seems to be more user friendly or nicer as long as you don’t need to copy the full path to paste it.

The following screenshot shows how to use the find command to recursively search more than a file.

The syntax to search multiple files recursively is the following:

find . \( -name <FileName1> -o -name <FIleName2> " \)

Note that there is a –o flag before the second file name. You can add more than one file by adding more –oname flags. For example, to find 4 files with the same command, use the following syntax:

find . \( -name <FileName1> -o -name <FIleName2> -o -name <FIleName3>" \)

In the practical example described in the following image, use this command to find a file named 27.jpg and a file whose name begins with “DIAGRAM” but without specifying its extension. Instead, use a wildcard (*) to find any file named DIAGRAM independently of its type.

find . \( -name 27.jpg -o -name "DIAGRAM*" \)

As you can see in the previous image, both files were found recursively.

The next example describes how to find the files by extension using the find command. In the following figure, you can see how to recursively find all the .jpg files using the wildcard again. The syntax is pretty simple:

find ~/ -type f -name "*.<Extension>"

Thus, to find all the .jpg files recursively, run the following command:

find ~/ -type f -name "*.jpg"

As shown in the previous image, all the jpg files including their path are listed successfully. You can replace the .jpg extension for any extension that you want to search like .png, .txt, .c and more.

Now, let’s assume that you don’t want to find a file but a directory recursively. All you need to do is to use the same command that was shown in the first example of this tutorial then add the -type d option. The syntax as follows:

find ~/ -type d -name <DirectoryName>

In the following practical example, use the previous syntax to find the recur directory.

find ~/ -type d -name recur

As you see in the previous figure, the directory named “recur” was found successfully.

You also can find the files by size using the following syntax where <Directory> is the main directory containing the subdirectories and the <Size><Unit> is the size of the files that you can list with their full path.

sudo find <Directory> -size<size><Unit>

The following example describes how to find the 10 MB size files. You can replace the M defining units in MB with c for bytes, w for two two byte words, k for kibytes and G for gibibytes (note units are case sensitive).

To find the 10 mebibytes files, execute the following command:

sudo find /var -size 10M

All 10M files were properly listed with their paths.

The syntax to find the files based on their permissions is shown in the following:

find <Directory> ~perm <Permissions>

Let’s assume that you want to identify and list the files with read, write, and executing permissions (777). The command to run is the following:

find ~/ -perm 777

The last example of this tutorial shows how to find and list the files and directories by size.

du -hs ~/*

As shown, the files are listed by size with proper units. The 0 size directories and files are empty.

Conclusion

Linux versatility and flexibility allows to find the files (and other functions) recursively in many ways. They can easily be executed by all the Linux users independently of his knowledge level, from the new users to the system administrators. All techniques previously described are valid for all the Linux distributions and even to some Unix systems. According to their man pages, some flags may vary in some distributions, but most of them are universal. In case your Linux distribution does not match any of the previously explained commands, you can read the man page. It is highly recommended to the readers to practice the examples to incorporate this knowledge.

Thank you very much for reading this Linux tutorial. Keep following us for more Linux professional tips.

About the author

David Adams

David Adams is a System Admin and writer that is focused on open source technologies, security software, and computer systems.