The “find” Command in Linux [10+ Practical Examples]

The find command is one of the most powerful commands in Linux. It enables users to perform search operations on their machines in a very time-efficient way. You can search for specific contents and list them with their pathway locations from a very large number of materials in a very short time. Hence, as a Linux enthusiast, you should learn in detail about the find command in Linux.

A. Description

The find command in Linux performs the task of searching & listing files and directories in a directory hierarchy in real-time.

B. Syntax

The syntax for the find command has been given below in detail.

find [OPTIONS] [PATH] [EXPRESSION]
  • [OPTIONS] — It maintains the handling of the symbolic links, debugging methods, and modification techniques.
  • [PATH] — It specifies where to start searching.
  • [EXPRESSION] — It specifies the operations to be executed.

C. Options

Many useful options are available for the find command. I have listed some of the most used ones here. However, if you want to know more about options for the find command you can always check the man page for the find command.

man find

Useful Options

  • -name PATTERN: Will search for the given PATTERN named contents.
  • -empty: Search for empty contents only.
  • -type d/f: d(limits the search to only directories), f(limits the search to only files).
  • -size +n,n,-n: Search for a specific size n.
  • -mmin +n,n,-n: Search for content modified at specific minute n.
  • -mtime +n,n,-n: Search for content modified at specific day n.
  • -amin +n,n,-n: Search for content accessed at specific minute n.
  • -atime +n,n,-n: Search for content accessed at specific day n.)
  • -exec: It can be used to perform customized tasks on the matches.
  • -perm PERMISSION: Search for content with specific PERMISSION.
  • -user USERNAME: Search for contents of a specific user.
  • -delete: Perform deletion of the matched contents.
NB: The options in Linux CLI(Command Line Interface) are all case-sensitive, So be cautious while using them.

Practical Examples of the “find” Command in Linux

The find command in Linux is an easy-to-use command which is used to find contents. Some of the most useful applications of the find command have been illustrated below. In this article, I have worked with the following directory hierarchy and files.

Working directory hierarchy in this article.

Note: The tree command lists the contents of a directory recursively in a tree-like configuration. If you’re using the tree command for the first time in Ubuntu you will have to install the command. You can do it simply by running the below command:

sudo apt install tree

Finally, provide the user password and you’re good to go with the tree command.Installing the tree command in Linux.

Example 1: Finding Files by Name Using the “find” Command in Linux

You can search for a file by its name using the find command added with the -name option. Here, I will search for a file named “List1.txt”. To do the same you can follow the below process.

Steps to Follow >

➊ At first open the Ubuntu Terminal in the TestFolder directory.

You can open Ubuntu Terminal from a specific directory. To learn details about that click here.

➋ Type the following command in the command prompt:

find -name List1.txt

➌ Now, press the ENTER button.

Output >

In the below Image, you can see that using the -name option with the find command I have printed the location of my desired file List1.txt on the terminal.Finding a specific file by its name using the find command in Linux.

A. Case-Insensitive Search

Now, If you forget the proper case-sensitive naming you can use the -iname option to make the search case-insensitive. To do the same you can follow the below process.

Steps to Follow >

➊ At first open the Ubuntu Terminal in the TestFolder directory.

You can open Ubuntu Terminal from a specific directory. To learn details about that click here.

➋ Type the following command in the command prompt:

find -iname list1.txt

➌ Now, press the ENTER button.

Output >

In the below Image, you can see that using the -iname(case-insensitive) option with the find command I have printed the location of my desired file List1.txt on the terminal.Case-insensitive search.

Example 2: Finding Empty Files & Directories

You can search and list empty files & directories by using the -empty options with the find command by following the below process.

Steps to Follow >

➊ At first open the Ubuntu Terminal in the TestFolder directory.

You can open Ubuntu Terminal from a specific directory. To learn details about that click here.

➋ Type the following command in the command prompt:

find -empty

➌ Now, press the ENTER button.

Output >

In the below Image, you can see that I have printed the location of empty contents on the terminal.Finding empty contents using the find command in Linux.


Similar Readings


Example 3: Finding Files by Type Using the “find” Command in Linux

You can search for contents by their types(i.e. f for files & d for directories) using the find command added with the -type option. To do the same as I have done you can follow the below process.

Steps to Follow >

➊ At first open the Ubuntu Terminal in the TestFolder directory.

You can open Ubuntu Terminal from a specific directory. To learn details about that click here.

➋ Type the following commands in the command prompt:

find -type f

find -type d

➌ Now, press the ENTER button for every line of commands.

Output >

In the below Image, you can see that using the -type option with the find command I have printed the desired type of contents on the terminal.Finding contents by their types.

Example 4: Finding Files by Size Using the “find” Command in Linux

You can search for files by specific size using the find command added with the -size option. To do the same as I have done you can follow the below process.

Steps to Follow >

➊ At first open the Ubuntu Terminal in the TestFolder directory.

You can open the Ubuntu Terminal from a specific directory. To learn details about that click here.

➋ Type the following commands in the command prompt:

find -size -4k

find -size 4k

find -size +4k

➌ Now, press the ENTER button for every line of commands.

Output >

In the below Image, you can see that using the -size option with the find command I have printed the location of contents of a specific size on the terminal.Finding contents by their size.

Example 5: Finding Files by Extension

You can search for files by their extension using the find command added with the -name option & special character asterisk(*). Here, I will search for files containing the .txt extensions. To do the same you can follow the below process.

Steps to Follow >

➊ At first open the Ubuntu Terminal in the TestFolder directory.

You can open the Ubuntu Terminal from a specific directory. To learn details about that click here.

➋ Type the following command in the command prompt:

find -name *.txt

➌ Now, press the ENTER button.

Output >

In the below Image, you can see that using the -name option & special character asterisk(*) with the find command I have printed the location of files containing .txt extension on the terminal.Finding contents by their extensions using the find command in Linux.

Example 6: Finding Files by Modification/Access Date & Time

A. Finding Files by Modification Date & Time

You can search for files modified at a certain number of minutes ago using the find command added with the -mmin(m represents modification) option. To do the same as I have done you can follow the below process.

Steps to Follow >

➊ At first open the Ubuntu Terminal in the TestFolder directory.

You can open the Ubuntu Terminal from a specific directory. To learn details about that click here.

➋ Type the following commands in the command prompt:

find -mmin -130

find -mmin +130

find -mmin 130

➌ Now, press the ENTER button for every line of commands.

Output >

In the below Image, you can see that using the -mmin option with the find command I have printed the location of files that are modified at certain times on the terminal.Finding contents by their modification time.

Note: If you want to search for files that are modified a certain number of days ago use the -mtime option instead of the -mmin option.

B. Finding Files by Access Date & Time

You can search for files accessed at a certain number of minutes ago using the find command added with the -amin(a represents access) option. To do the same as I have done you can follow the below process.

Steps to Follow >

➊ At first open the Ubuntu Terminal in the TestFolder directory.

You can open the Ubuntu Terminal from a specific directory. To learn details about that click here.

➋ Type the following commands in the command prompt:

find -amin -5

find -amin +120

find -amin 2

➌ Now, press the ENTER button for every line of commands.

Output >

In the below Image, you can see that using the -amin option with the find command I have printed the location of files that are accessed at certain times on the terminal.Finding contents by their access time.

Note: If you want to search for files that are accessed a certain number of days ago use the -atime option instead of the -amin option.

Example 7: Finding Files by Permission Attributes

You can search for files with specific permissions using the find command added with the -perm option. In the following table, I have listed a few different permission modes used in Linux.

Permission Modes(r= read, w=write, x=execute)
Octal Binary File Mode
0 000
1 001 –x
2 010 -w-
3 011 -wx
4 100 r–
5 101 r-x
6 110 rw-
7* 111* rwx*

Here, I will search for the files that have permission attributes set to 777* as shown in the below image.File attribute of a file. To do the same you can follow the below process.

Steps to Follow >

➊ At first open the Ubuntu Terminal in the TestFolder directory.

You can open Ubuntu Terminal from a specific directory. To learn details about that click here.

➋ Type the following command in the command prompt:

find -perm 777

➌ Now, press the ENTER button.

Output >

In the below Image, you can see that using the -perm option with the find command I have printed the location of the desired file on the terminal.Finding a content by its permission attributes using the find command in Linux.


Similar Readings


Example 8: Finding Files by Owner Using the “find” Command in Linux

You can search other users’ directories using the find command added with the -user option. Here, I will search on other softeko‘s Desktop directories. To do the same you can follow the below process.

Steps to Follow >

➊ At first open the Ubuntu Terminal in the TestFolder directory.

You can open Ubuntu Terminal from a specific directory. To learn details about that click here.

➋ Type the following command in the command prompt:

find /home/softeko/Desktop -user softeko

➌ Now, press the ENTER button.

Output >

In the below Image, you can see that using the -user option with the find command I have performed a search operation on other users’ Desktop directory from my terminal.finding contents of other users.

Example 9: Finding a Specific Text Within Text Files

You can search for a specific text using the find command added with the -exec option & the grep command. Here, I will search for “Good”. To do the same you can follow the below process.

Steps to Follow >

➊ At first open the Ubuntu Terminal in the TestFolder directory.

You can open the Ubuntu Terminal from a specific directory. To learn details about that click here.

➋ Type the following command in the command prompt:

find -name *.txt -exec grep 'Good' {} \;

➌ Now, press the ENTER button.

Output >

In the below Image, you can see that using the -exec option with the find command I have searched for a specific text “Good”.Searching for a pattern using the find command in Linux.

Example 10: Finding & Deleting Using the “find” Command in Linux

You can delete a file by its name using the find command added with the -delete option. Here, I will delete a file named “list2.txt”. To do the same you can follow the below process.

Steps to Follow >

➊ At first open the Ubuntu Terminal in the TestFolder directory.

You can open the Ubuntu Terminal from a specific directory. To learn details about that click here.

➋ Type the following command in the command prompt:

find -name list2.txt -delete

➌ Now, press the ENTER button.

Output >

In the below Image, you can see that using the -delete option with the find command I have deleted the list2.txt file.Searching and performing deletion.

Conclusion

In this article, I have tried to present you with the applications and usefulness of the find command in Linux. Hope that you’ll be able to explore more things with the help of these practical examples and find the right direction to be a power user of Linux.


Similar Readings

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
icon linux
Md. Ashakul Islam Sowad

Hi, I am Md. Ashakul Islam Sowad from Dhaka, Bangladesh. I have completed my undergraduate degree in Biomedical Engineering from the Bangladesh University of Engineering and Technology (BUET). I love to watch football and play video games in my free time. Here, I am working as a Linux Content Developer Executive. Furthermore, as a Linux enthusiast, I am always learning new things about Linux-based systems and I’ll be sharing them here. Read Full Bio

Leave a Comment