Linux Commands

Most useful examples of the find command in Linux

In Linux find command is used to locate files according to the specified criteria by the user. Not only files, the find command also tracks down the directories. The files can be searched by file name, directory name, modification date, extension, and so on.

In this follow-up, we are going to discuss the most useful examples of the find command which are commonly used by the Linux community to find in Linux.

The most useful examples of the find command in Linux

The find command is used to find the files from the directories and subdirectories of Linux, the general syntax of the find command is:

$ find [options] [path] [expression]

The find command is used with the options which specify the command to perform some particular tasks, then we have to define the path from where we want to find the files, and finally, we have to replace the expression with the file name which we want to find out.

Example 1 : Finding the file by its name

To search files by name we can use the option “name”, for understanding, we will find out file myfile.txt in the home directory using the command:

$ find /home/hammad -name myfile.txt

Example 2 : Finding the file excluding the case sensitivity

If we have doubts about the case sensitivity of the name of a file name, we can replace the option “name” with “iname”:

$ find /home/hammad -iname myfile.txt

Example 3 : Finding the file through the extensions

The other way to find files is through their extensions, for example, we can find out the files of “.png” extensions by executing the command:

$ find /home/hammad -name*.png”

The above output displayed all the files in Linux with the extension of .png.

Example 4 : Using “type” option with find command

Similarly, there are different other options that can be used with the find command like the “type” option. It is used for different purposes, for example, to display all the directories we will use the “d” option:

$ find . -type d

In the executed command, we used the “.” which helps the find command to browse through the current directory.

Example 5 : Finding files using their size

We can find out the files by specifying their sizes:

$ find . -type f -size -1M

Example 6 : Finding files using the date

The other useful example of find is to find files by using their modification date like we want to find out the “.txt” files that are modified in the last 30 days, we use the command:

$ find / -name "*.txt" -mtime +30 -daystart

Example 7 : Finding read-only files

To find out the read-only files use the command:

$ find / -perm /u=r

Likewise, to find the executable files, replace the “r” with “x” in the above command:

$ find / -perm /u=x

Example 8 : Finding files with multiple extensions

We can also find multiple files of different extensions using a single command, for example, we find the files having extensions “.txt” and “.png” using the command:

$ find . -regex ".*\.\(txt\|png\)$"

Example 9 : Finding the hidden files

We can also find out all the hidden files of the directory using the find command:

$ find  ~ -type f name ".*"

Example 10 : Finding the empty files

We can find out all the files and directories which are empty using the find command:

$ find / -type f -empty

In the above command, we used the “f” flag that displayed the empty files, if we want to display the empty directories, use the “-d” flag.

Conclusion

The find command makes it convenient for us to save time and find the particular files instantly wherever it is in Linux, moreover, if we only know the extension name or its size, we can still find it using the find command. In this follow-up, we have discussed the most important usages of find command with examples in Linux. We use the find command to find out the files using the name, extensions, size, read-only, and execute-only files.

About the author

Hammad Zahid

I'm an Engineering graduate and my passion for IT has brought me to Linux. Now here I'm learning and sharing my knowledge with the world.