Linux find Command Guide: Search Files by Name, Size, and Time
The find command is one of the most powerful tools in Linux and Unix-like systems for locating files and directories.
Unlike simple search tools, find supports advanced filtering based on attributes like name, size, type, permissions, and timestampsβmaking it essential for system administration and automation.
π Basic Syntax and Structure #
The general syntax of the find command is:
find [path] [expression]
-
Path Starting directory (e.g.,
.for current directory,/home/userfor absolute path) -
Expression Defines search conditions using tests, options, and actions
Expressions can be combined using logical operators:
-and(default)-or-not
π Search Files by Name #
Exact Match #
find . -name "abc.zip"
Searches for files named exactly abc.zip in the current directory and subdirectories.
Case-Insensitive Search #
find . -iname "abc.zip"
Matches variations like ABC.zip, Abc.zip, etc.
π Limit Search Depth #
To restrict how deep find searches into subdirectories:
find . -maxdepth 1 -name "abc.zip"
-maxdepth 1limits the search to the current directory only
This is useful for improving performance and avoiding unnecessary recursion.
β±οΈ Search by Time and File Type #
Modification Time (-mtime)
#
- Modified within the last 7 days:
find . -name "abc.zip" -mtime -7
- Modified more than 7 days ago:
find . -name "abc.zip" -mtime +7
File Type (-type)
#
- Regular files:
find . -type f -name "abc.zip"
- Directories:
find . -type d -name "abc"
βοΈ Combine Conditions for Advanced Queries #
You can chain multiple filters to refine results.
Example: Find Large Recent Files #
find . -name "*.zip" -size +100k -mtime -30
This finds:
.zipfiles- Larger than 100 KB
- Modified within the last 30 days
Size Filters: #
-size +100kβ Larger than 100 KB-size -10Mβ Smaller than 10 MB
π§© Execute Actions on Results #
The -exec option allows you to run commands on matched files.
Delete Matching Files (Use Carefully) #
find . -name "abc.zip" -exec rm {} \;
{}represents each matched file\;terminates the command
Save Results to a File #
find . -name "abc.zip" > found_files.txt
Redirects output to a text file for later use.
π Search the Entire System #
To search from the root directory:
sudo find / -name "abc.zip"
- Requires
sudoto access restricted directories - May take longer depending on system size
π Common Expressions Reference #
| Expression | Description |
|---|---|
-name |
Match filename (case-sensitive) |
-iname |
Match filename (case-insensitive) |
-type f / d |
Filter by file or directory |
-mtime |
Filter by modification time (days) |
-size |
Filter by file size (k, M, G) |
-exec |
Execute command on results |
β Summary #
The find command is a versatile and essential tool for:
- Locating files quickly
- Performing advanced filtered searches
- Automating file operations
By combining conditions like name, size, and time, you can build powerful queries that significantly improve productivity in the Linux command line.