The find
command in Linux is used to search for files and directories based on name, type, size, date, or other conditions. It scans the specified directory and its sub directories to locate files matching the given criteria.
find command uses are:
Here is the syntax for the find
Command in Linux:
find [path] [options] [expression]
~/Documents
).-type
for files/directories).This syntax allows you to customize your file search by specifying the path, adding options, and defining search criteria using expressions.
Options in find Command in LinuxHere are the `find`
command options along with brief descriptions of their purposes.
-name "pattern"
Searches files by name (case-sensitive). find ~ -name "notes.txt"
-iname "pattern"
Case-insensitive name search. find ~ -iname "notes.*"
-type f/d
Finds only files (f
) or directories (d
). find /var/log -type f
-size +10M
Finds files larger than 10MB. find / -size +100M
-mtime -7
Finds files modified in the last 7 days. find ~ -mtime -7
-perm 644
Finds files with specific permissions. find ~ -perm 644
-exec
Runs commands on found files (e.g., delete). find . -name "*.tmp" -exec rm {} \;
-empty
Finds empty files/directories. find ~ -empty
How find Command Works in Linux
The find
command performs real-time searches like they directly traversing the specified directory structure in the filesystem. Unlike tools such as locate
, which is depend on a prebuilt database that may not reflect recent changes, find command in
linux
examines the files and directories as they exist at the moment of search. This ensures that it can accurately locate even newly created or modified files. Here is the working of search process:
find
command begins its search from the directory specified in the path argument. It recursively explores all files and subdirectories.find
prints the paths of matching files. However, it can also execute commands on the results using the -exec
option.The find
command is included in the GNU findutils
package, typically pre-installed on most Linux distributions.
Using the find
command is straightforward. To find a file in Linux, open a terminal
Syntax:
find /path/to/search -options criteria
Replace "/path/to/search
"
with the directory where you want to start the search and customize the options and criteria based on your requirements.
For example :
To find a file named "example.txt" in the home directory, you would use:
find ~ -name "example.txt"
This command will locate and display the path to the file if it exists in the specified directory or its subdirectories.
Examples of find Command in Linux 1. How to find a Specific File Using `find` Command in LinuxThis query is designed to identify a file within a designated directory. In the provided example, it seeks a file named "sample.txt" within the "GFG" directory.
find ./GFG -name sample.txt
The find
command traverses the specified directory (./GFG
) and looks for a file named "sample.txt." If found, it displays the path to the file.
Output:
Search a file with specific name 2. How to Search Files with a Pattern Using `find` Command in LinuxThis command is use for discovering files within a directory that attach to a specific naming pattern. In this case, it identifies files ending with '.txt' within the "GFG" directory.
find ./GFG -name *.txt
The command looks for files with names ending in '.txt' within the "GFG" directory, presenting a list of matching files.
Output:
Search a file with pattern 3. How to find and Confirm File Deletion Using `find` Command in LinuxThis command not only locates a specified file but also prompts the user for confirmation before initiating its removal. The example seeks to delete a file named "sample.txt" within the "GFG" directory.
find ./GFG -name sample.txt -exec rm -i {} \;
The -exec
option executes the rm
command on the located file, and the -i
flag prompts the user for confirmation before deletion. When this command is entered, a prompt will come for confirmation, if you want to delete sample.txt or not. if you enter 'Y/y' it will delete the file.
Output :
find and delete a file with confirmation 4. Search for Empty Files and Directories Using `find` Command in LinuxThis query is tailored for discovering and listing empty files and directories within a specified directory.
find ./GFG -empty
The `find`
command identifies and lists all empty folders and files within the "GFG" directory or its subdirectories.
Output:
This command is used to locate files within a directory that have specific permissions. In the provided example, it identifies files with permissions set to 664 within the "GFG" directory.
find ./GFG -perm 664
The command searches for files within the "GFG" directory with the specified permissions (664) and displays the results.
Output:
Search for file with entered permissions 6. Display Repository Hierarchy Using `find` Command in LinuxThis command is utilized to display the hierarchical structure of repositories and sub-repositories within a given directory.
find . -type d
This command displays all the repositories and sub-repositories present in the current repository. In the below example, we are currently in a repository namely "GeeksforGeeks" which contains a repo "Linux", which contains sub-repo "LinuxCmds" which further contains a repo "FindCmd". The ouput of below cmd is simply displaying this info. Please note that in this case if you will use "ls" cmd then it will only show "/Linux".
Output:
7. Search Text Within Multiple Files Using `find` Command in LinuxThis command is tailored for finding lines containing specific text within multiple files. The example looks for lines containing the word 'Geek' within all '.txt' files in the current directory and its subdirectories.
find ./ -type f -name "*.txt" -exec grep 'Geek' {} \;
The command searches for '.txt' files (-type f
and -name "*.txt"
) and uses grep
to print lines containing the specified text ('Geek').
Output:
Search text within multiple files 8. Find Files by When They Were Modified Using `find` Command in LinuxThe -mtime
option is handy for finding files based on their modification time. To find files modified within the last 7 days, you can use:
find /path/to/search -mtime -7
This command will list files modified in the last week.
Finding Last modificationsIn this example we are searching changes in directory "/home/administrator/Downloads" which are done is past 7 days.
9. Find Large Files (Over 100MB)Find the files which is >100MB across the entire system. You can also change the file like 200M, 400M etc
find / -type f -size +100MFind large files 10. Find Files Modified in the Last 24 Hours
In this it shows the files edited in the last day. You can also change 2 days (-2).
find ~ -type f -mtime -1Find Files Modified files
~
: This represents your home directory.-type f
: This option tells find
to only look for files-mtime
refers to the modification time of a file.-1
means "less than 1 day ago."This command is used to find and delete temporary files in the /tmp
directory.
sudo find /tmp -type f -name "*.tmp" -exec rm {} \;Find and Delete Temporary Files from your syste, 12. Limit Search Depth
In this we looks for config.yml
only in the current directory and one subfolder deep or we can search any file with search depth.
find . -maxdepth 2 -name "*.txt"Limit Search Depth 13. Use Grep to find Files Based on Content Using `find` Command in Linux
Combining the find
command in linux with grep
allows you to search for files based on their content. For example, to find files containing the word "pattern" in the current directory and its subdirectories, you can use:
find . -type f -exec grep -l "pattern" {} \;
This command will display the names of files containing the specified content.
Breakdown of the Command:find .
: Initiates the search from the current directory (.
).-type f
: Specifies that the search is for files only, excluding directories.-exec grep -l "pattern" {} \;
: Executes the grep
command on each found file ({}
) to search for the specified content ("pattern"). The -l
option in grep
ensures that only the names of files containing the pattern are displayed.-type f
) found in the search, the -exec
option executes the grep
command.grep
command searches for the specified content ("pattern") in each file.-l
option in grep.
The find
command in Linux is more than just a search tool — it's a lifesaver for navigating complex file systems. Whether you're managing a large codebase, auditing system logs, or cleaning up old files, find
helps you locate exactly what you need in seconds.
find command in Linux | Linux Tutorial
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4