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:
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:
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:
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:
Note that there is a –o flag before the second file name. You can add more than one file by adding more –o –name flags. For example, to find 4 files with the same command, use the following syntax:
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.
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:
Thus, to find all the .jpg files recursively, run the following command:
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:
In the following practical example, use the previous syntax to find the recur directory.
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.
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:
All 10M files were properly listed with their paths.
The syntax to find the files based on their permissions is shown in the following:
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:
The last example of this tutorial shows how to find and list the files and directories by size.
As shown, the files are listed by size with proper units. The 0 size directories and files are empty.
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.