Linux Commands

Linux Find Command Tutorial

Linux “find” command is most widely used in UNIX. It is a command-line utility used to search a list of files, directories and perform several functions on them. It provides numerous options to search, for example, you can find files by name, directory, file type, size, modification date, creation date, permissions, and also supports another possible way to search.

In this article, we convey how to use the “find” command to search files efficiently either through some expressions or patterns in an Ubuntu machine. If you are a new user of Linux, then you can master it by learning various conditions of the “find” command.

Find Command Syntax:

find [directory-path] [filename] [options]

Where directory-path contains the complete path of a directory, filename contains the name of a file which you want to search, whereas you have several options such as move, copy, delete.

1. Find file through Name

It is one of the easiest commands which assists you to find a specific file by name within a directory. By using this command, you can easily find all the files having the same name within the passed directory path if it is present in one of the folders.

Syntax:

$ find [directoryname] -name [filename]

Where name option is case-sensitive.

Example

In this example, we are going to find a file recursively such as “file1.txt” within the folder “dir1”.

$ find dir1 -name file1.txt

If you want to find a file by name which is not case-sensitive, you can enter the filename either in uppercase or lowercase. You can find files irrespective of the case by simply replacing the option “name” with “iname”.

Syntax

$ find [directoryname] -iname [filename]

Example:

In this example, we are going to find a file named “file1.txt” within the folder “dir1” but we entered the characters of the filename in upper and lower case.

$ find dir1 -iname FiLe1.txt

2. Finding a file through expression statement

You can also find files from the directory by using regular expressions. In this command, “-regex” tells us that we are going to pass a regular expression, then “.” match up no. of characters within the file. Subsequently, “*” matches the repetition of characters. At the last, you are going to pass the file extension.

Syntax

$ find -regex ".*\[fileextension]"

Example

In this example, we are going to find all files which have an extension of ‘.txt’ at the end.

$ find -regex ".*\.txt"

Find files by the regular expression is good as it allows you to search files with multiple extensions at once.

Syntax

$ find -regex ".*\[.fileextension]" || "[.fileextension] "

Example

In this example, we going to find all the files which have an extension of .txt and .sh.

$ find -regex ".*\.txt" || ".*\.sh"

3. Find modified files through n minutes

You can also search modified files within the last n minutes.

Syntax

$ find [directoryname] -mmin -n

Where N represents minutes

Example

In this example, we find all the files that are modified 50 minutes back.

$ find dir1 -mmin -50 dir1

4. Find files that are empty

You can also find all empty directories within the entered directory.

Syntax

$ find ./directoryname -empty

Example

In this example, we find empty folders reside in the documents folder.

$ find ./Documents -empty

5. Find modified files through n days

By using the “find” command, you can easily search modified files within the N no. of days.

Syntax:

$ find [directoryname] -mtime n

Where n represents the last modified days.

Example

The below-mentioned command finds all the files that are modified within a day.

$ find dir1 -mtime 0

6. Find files by entering specific permissions

You can also find files with specific entered permissions.

Syntax

$ find directory -perm [id]

Example

In this example, we are going to find files within the Documents folder and subfolders with 644 permission. 644 permission means that only the host who creates the file has the authority to read or write.

$ find ./Documents -perm 664

$

You can also find all files which are connected to a user.

Syntax

$ find . -user [username]

Example

$ find . -user linus_user

7. Find files by specifying the size

You can also find files which are smaller or greater than the entered size.

Syntax: Find files bigger than n size

$ find directory -size +n[bytes]

Where n is the size of the file, “+” operator is used to search files greater than n size of the file.

Syntax: Find files smaller than n size

$ find directory -size -n[bytes]

Where n is the size of the file, “-” operator is used to search files smaller than n size of the file.

There are various options of bytes like M is used for Megabytes. Similarly, G is used for Gigabytes, k is used for kilobytes.

Example

In this example, we are going to find files from the folder of dir1 whose size is greater than 15. Byte block is the default unit if you cannot specify any option next to the size of the file as shown in the picture below.

$ find ./dir1 -size +15

In this example, we are going to find files from the folder of dir1 whose size is smaller than 10 megabytes.

$ find ./dir1 -size -10M

8. Find files by specifying the type

You can also find files by specifying the type of file. The “find” command has various types like “f” is used to find files, “d” is used to find directories, etc.

Syntax

$ find . -type

Example

In this example, we are going to find directories that reside in the Document folder.

$ find . -type d

Then we are going to find all the files that reside in the document folder as well as in sub-folders.

$ find . -type f

9. Find files by using multiple conditions

You can also find files by combining different conditions by using a single command. In this example, we are going to find files whose size is greater than 1 kilobyte and also have an extension of .txt.

$ find . -size +1k -and -name "*.txt"

Conclusion

This article clearly demonstrates the effectiveness and efficiency of the “find” command to find the located files through name, permission or type, etc. Examples are also provided which helps you understand the better usage of each find command.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.