find — Linux basic command
Linux / Basic Linux commands / find.html
A powerful command that searches a directory tree for files matching conditions.
Basic Information
| Command Name | find |
|---|---|
| One-Line Summary | A powerful command that searches a directory tree for files matching conditions. |
| Provided By | findutils (pre-installed in all Linux distributions) |
| Official Documentation | https://man7.org/linux/man-pages/man1/find.1.html |
| Supported Operating Systems | Linux, macOS, BSD, WSL |
| Installation Required | Pre-installed (no separate installation needed) |
| Category | Basic Linux Command (tags: file search, explore, linux, basic) |
What is this command?
The find command recursively searches for files matching specified conditions under a given directory. You can search by various criteria such as name, size, modification time, permissions, and owner.
From simple searches to complex condition combinations, and even executing commands on the search results, it is a core tool for Linux administration.
What problem does it solve?
- Finds files with a specific name.
- Finds and cleans up old files.
- Finds files above a certain size.
- Finds and corrects files with incorrect permissions.
Key Features
Name-Based Search (-name)
Searches using wildcards in the file name.
- Usage Scenario: When looking for files with a specific name pattern.
- Expected Result: The file paths matching the conditions are displayed.
- Limitations: It is case-sensitive, so using
-inameis recommended.
Size & Time Conditions
Searches by file size (-size) or modification time (-mtime).
- Usage Scenario: When finding and cleaning up old or large files.
- Expected Result: Only files matching the conditions are selected.
- Limitations: The time unit (in days) may not be precise.
Compound Conditions (-and, -or)
Combines multiple conditions for precise searching.
- Usage Scenario: When complex search criteria are needed.
- Expected Result: Only the exact files are found.
- Limitations: The command can become long and difficult to understand.
Execute Command on Search Results (-exec)
Executes a command on each found file.
- Usage Scenario: Used for batch deletion, permission changes, etc., on found files.
- Expected Result: Enables batch processing without manual work.
- Limitations: Risk of mass deletion on mistakes, requiring caution.
Installation and Execution
Check Pre-installation
It is included by default in all Linux distributions.
which find
find --version
Installation (Minimal Environments)
It is generally already installed.
sudo apt install findutils # Debian/Ubuntu
sudo dnf install findutils # Fedora
Practical Examples
Search by Name
Finds files with a specific name.
# Search for files in the current directory
find . -name "*.conf"
# Search in a specific directory
find /etc -name "nginx*.conf"
# Case-insensitive search
find . -iname "readme*"
# Files only (excluding directories)
find . -name "*.log" -type f
Search by Size and Time
Finds old or large files.
# Files older than 30 days
find . -mtime +30
# Files larger than 10MB
find . -size +10M
# Files modified within the last 24 hours
find . -mtime -1
# Empty files
find . -empty -type f
Execute Command on Search Results
Executes commands in batch on found files.
# Delete found files
touch test1.log test2.log
find . -name "*.log" -delete
# Change permissions in batch
find . -name "*.sh" -exec chmod +x {} \;
# Save search results to a file
find /var/log -name "*.log" > log-files.txt