The find Command in Linux
Last Updated: 2025-01-01
3 min read
Overview
The find command searches the filesystem for files and directories matching specific criteria. It’s one of the most powerful and versatile commands available on Linux, ideal for locating files by name, size, type, or age.
Basic Syntax
find [path] [options/tests] [actions]
Finding Files by Name
Exact name match
find /home/username/public_html -name "wp-config.php"
Case-insensitive search
find /home/username -iname "readme.md"
Wildcard patterns
find /var/www -name "*.log"
find /home/username -name "*.php"
Finding by File Type
Use the -type flag:
| Type | Description |
|---|---|
f | Regular file |
d | Directory |
l | Symbolic link |
find /home/username -type d -name "cache" # Find directories named "cache"
find /home/username -type f -name "*.txt" # Find only regular files
Finding by Size
find /home/username -type f -size +100M # Files larger than 100 MB
find /home/username -type f -size +1G # Files larger than 1 GB
find /home/username -type f -size -10k # Files smaller than 10 KB
Size suffixes: c (bytes), k (kilobytes), M (megabytes), G (gigabytes).
Finding by Modification Time
The -mtime flag uses days, and -mmin uses minutes:
find /home/username -type f -mtime -7 # Modified within the last 7 days
find /home/username -type f -mtime +30 # Not modified in over 30 days
find /home/username -type f -mmin -60 # Modified in the last 60 minutes
Combining Conditions
# PHP files larger than 1 MB
find /home/username -type f -name "*.php" -size +1M
# Log files older than 30 days
find /var/log -type f -name "*.log" -mtime +30
# Files NOT matching a pattern
find /home/username -type f ! -name "*.html"
Taking Action on Results
-exec — Run a Command on Each Result
# Delete log files older than 90 days
find /home/username/logs -type f -name "*.log" -mtime +90 -exec rm {} \;
# Change permissions of all PHP files
find /home/username/public_html -type f -name "*.php" -exec chmod 644 {} \;
The {} placeholder represents each found file. The \; terminates the command.
-delete — Remove Matching Files
find /home/username/tmp -type f -mtime +7 -delete
Warning: Always test your
findcommand without-deleteor-exec rmfirst to verify the results.
Print results with details
find /home/username -type f -size +50M -exec ls -lh {} \;
Practical Hosting Examples
Find the 10 largest files on your account
find /home/username -type f -exec du -h {} + | sort -rh | head -10
Find recently modified files (potential security check)
find /home/username/public_html -type f -mmin -30
Find empty directories
find /home/username -type d -empty
Find files with specific permissions
find /home/username/public_html -type f -perm 0777
Tips
- Always start with a specific path to avoid searching the entire filesystem.
- Test with a dry run before using
-deleteor-exec rm. - Combine
findwithgrepby piping results for even more powerful searches.
Tags:
ssh
linux
find
file-management