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.
Table of Contents
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]ShellScriptIn 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/treeShellScriptThe 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.

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"ShellScriptThe search result is shown in the image below. An important point to note here is that the -name option is case-sensitive.

If you want to search without case sensitivity, you can use the -iname option:
find ~/linux/tree -iname "app.py"ShellScriptBy disabling case sensitivity, both App.py and app.py files are found.

You can also use wildcards to search for files containing specific strings:
find ~/linux/tree -name "*.py"ShellScriptBelow is the result of finding all files with the .py extension.

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*"ShellScriptSearching by File Type (-type)
The find command allows you to specify the file type you’re searching for using the -type option:
f: Regular filed: Directoryl: Symbolic link (symlink)c: Character device fileb: Block device file
For example, to search only for directories, use:
find ~/linux/tree -type dShellScriptAs shown in the image below, only directories are listed, excluding other file types.

Searching by File Size (-size)
To search based on file size, use the -size option. Size units are denoted as follows:
c: Bytesk: KilobytesM: MegabytesG: Gigabytes
For example, to find files smaller than 50 bytes, use:
find ~/linux/tree -size -50cShellScriptThe + 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.

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 -3ShellScriptHere, -3 means files modified within the last three days, and +3 means files modified more than three days ago.

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-dataShellScriptThe image below shows the result of searching for files owned by the www-data 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 {} \;ShellScriptHere, {} 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 {} \;ShellScriptThis 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.