The find command in Linux is one of the powerful tools that allows you to search for a file or directory with a simple syntax. As its name suggests, it lets users not only search by name but also by multiple criteria, like type, size, date, owner, and more. It is widely used by developers, system administrators, and regular users alike. In this article, we will learn about the find command of Linux, with its working and real-world use cases.
Table of Contents:
What is the find Command in Linux?
The find command in Linux is used to search and locate files and directories based on various factors such as name, type, size, permissions, modification date, ownership, and more. It is one of the most powerful and versatile utilities, and it recursively searches through directories from a specified path and applies search filters to match the specific file or directory.
The find command in Linux has the following features:
- It traverses through the directories and subdirectories.
- Multiple actions can be performed on the found item, like delete, move, etc.
- Expressions can be combined using the logical operators like -and, -or, and !.
Syntax of the find Command in Linux
Below is the syntax of the find command in Linux:
find [path] [expression]
In the above expression,
- The path is the directory to start the search
- The expression is the condition for filters applied.
Master DevOps Today - Accelerate Your Future
Enroll Now and Transform Your Future
How Does the find Command Work in Linux?
The find command in Linux begins its search by directly traversing the specified path in the file system. Unlike tools such as locate, which depend on a prebuilt index, the find command searches the actual state of files and directories when the command is executed. This approach makes it extremely easy and accurate to find newly created, updated, or dynamically changed directories.
Now, let us discuss the searching process of the find command in Linux.
1. Starting Point
The find command starts its search from the directory path given as the parameter, the path can be an absolute or relative directory path. After that, it performs a recursive descent into all subdirectories and explores every directory before moving to another directory.
2. Applying the Criterion
Each file or directory that was found during the traversal is evaluated as per the expression you provide, while giving the command. These expressions are evaluated in logical order, and only files that meet the criteria are considered matches. For example,
- -name “*.txt”, will match the file names ending in .txt
- -mtime -7, will match files modified in the last 7 days
3. Refining the Options
Options defined with the find command allow the user to adjust the search more precisely. For example,
- -maxdepth N option limits how deep the search goes in the directory tree.
- -mindepth N option skips top levels and starts searching deeper.
4. Taking Action
By default, the find command prints the full path of every file or directory that matches the criteria, as per the definition. However, it can also perform actions to explicitly print the matched paths using the -print command, -delete command to permanently delete the files or directories, and so on.
Now, before using the find command in Linux, we first have to create a directory and a file. Below, we are creating a directory, Intellipaat, and a file, sample.txt, having the content as shown in the image below.
In the above image,
- mkdir -p intellipaat is used to create a new directory named intellipaat.
- Flag -p prevents errors if the directory already exists, and creates parent directories if needed.
- cd intellipaat changes your current directory to intellipaat.
- touch sample.txt creates a new empty file named sample.txt.
- cat > sample.txt allows you to enter content directly into the file. Further, when finished writing the content, press Ctrl+D to save and exit from file.
To view the content present in sample.txt, use the cat sample.txt command as shown below.
Now, let us use the find command to find the file in the section below.
1. Find a Single File by Name
To search by name, we use the -name option with the find command in Linux, which searches for files and directories in a specified location and looks at the current state of the file system.
Below is the syntax of it.
find -name "<file_name>"
Example:
The find -name sample.txt gives the location of the file as an output.
2. Find a File by Approximate Name
The find command also allows the user to search files using patterns like a number of characters and a single character, which allows the search by partial or approximate name, not just an exact match.
Below is the syntax of it.
find -name *approx_file_name*
In the above syntax, * matches any number of characters
Example:
The find -name *paat* gives the full file name as per the approximate characters defined by the user.
3. Find Everything
If you want to list everything, i.e., all files and directories under a certain folder, you can use the find command without any filters like -name.
Below is the syntax of it.
find <path>
In the above syntax, the <path> is the directory for which you want all the files or directories.
Example:
The find intellipaat command list all the directories and files present in it.
4. Find by Content
The find command locates files and directories but does not read their content. To search the content inside a file, the find and grep are used together.
- find locates files,
- grep searches the content of the file by matching the content inside it
Below is the syntax of it.
find <path> -type f -exec grep -i "text" {} +
In the above syntax,
- -type f looks only for files
- -exec grep runs on each file found
- -i is used for ignoring upper and lower cases
- {} is the placeholder for each file
- + is used to run the grep command on multiple files at once.
Example:
The find intellipaat -type f -exec grep -i “this” { } + command finds the text “this” in the directory intellipaat, and displays the matching lines along with file paths.
5. Find Files by Type
Linux treats everything as a file, but you can find the specific types of filesystem objects, like files, directories, symbolic links, etc., based on the type in which they are stored. There are the following types of files in Linux.
Below is the syntax of it, with some commonly used flags.
find <path> -type <type_flag>
Type Flag |
Meaning |
f |
Regular file (like .txt, .pdf, .sh, etc.) |
d |
Directory (folder) |
l |
Symbolic link |
c |
Character device |
b |
Block device |
Example:
The find intellipaat -type f command finds the regular file type in the intellipaat directory.
Get 100% Hike!
Master Most in Demand Skills Now!
6. List the Directories
Use the -type d option with the find command to find the present directories in any directory.
Below is the syntax of it.
find <path> -type d
Example:
The find intellipaat -type d command finds the directory in the intellipaat directory, while the find -type d command finds all the directories present.
7. Limit Listing Results
In Linux, files and directories are stored in a tree structure, where:
- Depth 0 is the folder you start with, the root folder
- Depth 1 is the folder directly inside the root folder
- Depth 2 is the folder inside the Depth 1 Folder, and so on.
For example,
Folder(Root) Depth 0
|___________Folder1 Depth 1
|___________Folder2 Depth 2
In the above example, the Root folder has Depth 0, Folder1 has Depth 1, and Folder2 has Depth 2, and it will continue like this.
There are 2 categories to limit the listing results. These are:
1. MinDepth
-mindepth option tells the find command to ignore directories shallower than N levels.
Below is the syntax of it.
find <path> -mindepth N
Example:
The find . -mindepth 1 command lists all the directories and files present in the current directory and deeper, and skips only the starting point. While the find intellipaat -mindepth 1 command finds all the directories and files present starting from the intellipaat directory.
2. MaxDepth
-maxdepth option tells the find command to stop going deeper after N levels from the starting directory.
Below is the syntax of it.
find <path> -maxdepth N
Example:
The find . -maxdepth 1 command shows all the directories and files after Depth 1 in the root user, while the command find intellipaat -maxdepth 1 lists all the directories and files in the intellipaat folder after level 1.
8. Find Empty Files
A file with a size of 0 bytes of content is known as an empty file. The command can locate empty files using the following command
find <path> -type f -empty
Example:
The find . -type f -empty command finds all the empty files present in the current directory.
9. Find Recently Modified Files
In Linux, we have multiple timestamps, like:
- mtime: It returns the time when the content was last modified.
- ctime: It returns the time when metadata (like owner, permissions) was last changed
- atime: It returns when the file was last read.
To find the recently modified files, we use the -mtime option with the find command.
Below is the syntax of it
find <path> -type f -mtime <days>
For days,
- -1 is used to indicate modifications done within the last 24 hours
- -7 is used to indicate modifications done within the last 7 days
- +7 is used to indicate modifications done more than 7 days ago
Example:
The above command, find intellipaat -type f -mtime -1 returns the modified file that was in the last 24 hours in the intellipaat directory.
10. Search a Path
Sometimes, you know a part of the directory path, but not exactly where the files are present in your system. In such cases, you can use the –ipath or -path option with the find command to search for full or partial pathnames.
- –ipath option is not case sensitive
- –path is case sensitive
Both of the above options match the relative or exact path string, not just the filename.
Below is the syntax of it
find <starting_dir> -ipath "*part/of/path*"
- * are used to match anything, i.e., any character, any number of times.
- “” are used to prevent the shell from expanding and directly passing the path to the find command
Example:
The find . -ipath “*samplE.TxT*” command returns the path of the file sample.txt irrespective of the characters’ case defined by the user, while the find . -path “*samplE.TxT*” command returns the path of the file sample.txt as per the case of the characters defined.
11. Search Files Based On Owners and Groups
Every file in Linux is associated with an owner or group. To find a file based on the user and owner, you can use the following option with the find command
- Use <username> to search for a file owner
- Use <groupname> to search a file based on a group
Searching the files by group or user is helpful when there are multiple users and the file needs to be found on behalf of a specific user.
Below is the syntax of it
find <path> -user <username>
find <path> -group <groupname>
Example:
The find -user akshat command finds all the files and directories owned by the user akshat.
12. Find and Delete Temporary Files
Temporary files are created for short-term use and commonly have an extension .tmp, .temp, .bak, and so on. These temporary location directories have names like /tmp, /temp, etc. The find command in Linux helps locate and optionally locate and delete these files permanently.
Below is the syntax of it
find <path> -type f -name "*<extension_of_file>*" -delete
Example:
The find . -type f -name “*.txt*” -delete command finds all the files present in the current directory and deletes them.
Note: Once you delete files using -delete, they are not recoverable through normal means, unless you already had a backup or use special recovery tools immediately.
Use Cases of the find command in Linux
The find command in Linux is one of the most practical tools, used in both personal systems and professional environments. For example, you are a developer or a system administrator managing a large codebase or a server that contains thousands of files. After some time, when the data in the file becomes large, multiple files with the same name are created. Due to this, locating specific files manually becomes inefficient. In this situation, the find command proves to be very helpful to identify the files with the names, size, extension, date modified, and so on.
During debugging, you may want to search for all .java or .py files containing a specific function or error keyword. The find command, combined with the grep option, lets you search across all source files and find a full hierarchy of a project for documentation or code reviews. Also, it is often used to scan for files with improper permissions or world-writable flags, helping to prevent vulnerabilities.
Unlock Your Future in Linux
Start Your Linux Journey Today
Conclusion
From the above article, we learned that the find command in Linux helps users find a specific file, or a set of files on characteristics such as size, date modified, username, group, type, and content present in it. It not only searches for a file, but the files can also be deleted at the same time when located, but one should stay alert while deleting the file, as it cannot be restored or retrieved easily once deleted. The find command not only helps students, but it is also used by professional developers and system administrators to find files as per their needs.
If you want to learn more about the find command in Linux, you can refer to our Linux Course.
Find Command in Linux – FAQs
Q1. What is the find command in Linux?
The find command is used to search for files and directories in your system based on name, type, size, date, and more.
Q2. How to find a line in Linux?
To find a line in Linux, use the grep command for specific lines or text inside files.
Q3. How to find a folder in Linux?
To find a folder in Linux, use the `find <path> -type d -name “<foldername>”` command. For example, `find . -type d -name “docs”` searches for a folder named “docs” in the current directory and its subdirectories.
Q4. What is the grep command?
grep is a command-line tool that searches for specific text or patterns inside files.
Q5. How to use the find command?
find -name “” is the basic syntax for the find command. For example: find . -name “fileA.txt” finds a file having the name fileA.txt.