How to Use the Linux Command find and 8 Essential Options

Finding files or directories in Linux is crucial. The Linux Command find is an efficient search tool, especially when you need to locate a specific file among many. The Linux Command find is a powerful tool that can search for files or directories within a specified path and offers various options for more precise searches.

In this post, we’ll explain the basic usage of the find command, cover various options, and provide tips on how to use it effectively. Mastering the find command is essential for anyone using a Linux system.

What is the Linux Command find?

The find command is used in Linux to search for files or directories. This command allows you to locate files based on various criteria such as file name, size, modification time, and more.

The find command is used as follows:

find [options...] [path...] [expression]
ShellScript

In this basic format, the path specifies the directory to search, the expression defines the filter to find files, and the action specifies any additional tasks to be performed on the found files.

Basic Usage of the find Command

Let’s look at the basic usage of the find command. The simplest form is to search for all files in a specific path.

find ~/linux/tree
ShellScript

The above command lists all files and directories within the ~/linux/tree directory as shown in the image below. In this case, the find command recursively explores the directory and its subdirectories.

Figure 1. Linux Command find: Execution Result
Figure 1. Linux Command find: Execution Result

Key Options and Usage Examples

The find command allows you to fine-tune your search criteria using various options. Below are some commonly used options.

Searching by Name (-name)

To search for files by name, use the -name option. For example, if you want to find a file named “app.py” within the linux/tree path under your home directory, you would use:

find ~/linux/tree -name "app.py"
ShellScript

The search result is shown in the image below. An important point to note here is that the -name option is case-sensitive.

Figure 2. Linux Command find: Searching by File Name with the -name Option
Figure 2. Linux Command find: Searching by File Name with the -name Option

If you want to search without case sensitivity, you can use the -iname option:

find ~/linux/tree -iname "app.py"
ShellScript

By disabling case sensitivity, both App.py and app.py files are found.

Figure 3. Linux Command find: Case-Insensitive Search by File Name with the -iname Option
Figure 3. Linux Command find: Case-Insensitive Search by File Name with the -iname Option

You can also use wildcards to search for files containing specific strings:

find ~/linux/tree -name "*.py"
ShellScript

Below is the result of finding all files with the .py extension.

Figure 4. Linux Command find: Searching with Wildcards
Figure 4. Linux Command find: Searching with Wildcards

If you want to find all files containing the string “test” in their names in the current directory, use wildcards around the string like this:

find . -name "*test*"
ShellScript

Searching by File Type (-type)

The find command allows you to specify the file type you’re searching for using the -type option:

  • f: Regular file
  • d: Directory
  • l: Symbolic link (symlink)
  • c: Character device file
  • b: Block device file

For example, to search only for directories, use:

find ~/linux/tree -type d
ShellScript

As shown in the image below, only directories are listed, excluding other file types.

Figure 5. Linux Command find: Searching for Directories Only
Figure 5. Linux Command find: Searching for Directories Only

Searching by File Size (-size)

To search based on file size, use the -size option. Size units are denoted as follows:

  • c: Bytes
  • k: Kilobytes
  • M: Megabytes
  • G: Gigabytes

For example, to find files smaller than 50 bytes, use:

find ~/linux/tree -size -50c
ShellScript

The + sign means files larger than the specified size, and the - sign means files smaller than the specified size. Thus, entering +1M will search for files larger than 1MB.

Figure 6. Linux Command find: Searching for Files Smaller Than 50 Bytes
Figure 6. Linux Command find: Searching for Files Smaller Than 50 Bytes

Searching by Modification Time (-mtime)

To search based on when a file was last modified, use the -mtime option. For example, to find files modified within the last three days, use:

find ~/linux/tree -mtime -3
ShellScript

Here, -3 means files modified within the last three days, and +3 means files modified more than three days ago.

Figure 7. Linux Command find: Searching for Files Modified Within the Last 3 Days
Figure 7. Linux Command find: Searching for Files Modified Within the Last 3 Days

Searching for Files Owned by a Specific User or Group (-user, -group)

To search for files owned by a specific user or group, use the -user and -group options. For example, to find files owned by the user “www-data”, use:

find ~/linux/tree -user www-data
ShellScript

The image below shows the result of searching for files owned by the www-data user.

Figure 8. Linux Command find: Searching for Files Owned by a Specific User
Figure 8. Linux Command find: Searching for Files Owned by a Specific User

Additional Actions on Search Results

One of the powerful features of the find command is the ability to perform additional actions on the search results. The most commonly used option for this is -exec. This option allows you to execute a specific command on the found files.

For example, to delete all “.log” files found, use:

find /home/user -name "*.log" -exec rm {} \;
ShellScript

Here, {} represents the name of each file found by the find command, and \; indicates the end of the command. This command searches for and deletes all .log files.

Precautions

The find command is very powerful, but if used incorrectly, it can accidentally delete or modify important system files. Be especially careful when using the -exec option. Always verify the results before executing the command, and if needed, use the -ok option instead of -exec to prompt for confirmation before performing each action.

find /home/user -name "*.log" -ok rm {} \;
ShellScript

This way, a confirmation message will appear before each file is deleted, reducing the chance of mistakes.

Summary

The find command in Linux is a powerful tool for efficiently searching for files or directories. By utilizing various options, you can easily locate files that meet specific criteria, and the -exec option allows you to automate tasks. However, it’s important to use it with caution, especially when executing critical operations, and always verify the results beforehand.

Now, take full advantage of the find command to quickly and accurately find files on your Linux system. Experiment with different combinations of options to develop your own methods for using find effectively.

References

Leave a Comment