find
Quick Summary
find is a real-time filesystem search tool. It walks directories directly, filters by metadata, and can run actions on the matches.
Learning Path
1. Overview
- What
finddoes and when to use it - How it differs from indexed search tools
- When to prefer it for audits and automation
2. Syntax
- Basic command structure
- Path, expression, and action order
- Default output behavior
3. Core Filters
-nameand-iname-type-size-mtime,-atime, and-ctime-userand-group-perm
4. Actions
-print-ls-delete-exec-ok-printf
5. Practical Search Patterns
- Find specific files
- Find by extension
- Find directories only
- Find empty files and folders
- Limit depth with
-maxdepthand-mindepth
6. Maintenance Workflows
- Bulk permission fixes
- Ownership corrections
- Safe cleanup with
-okand-delete - Exporting search results
7. Safety and Edge Cases
- Quoting patterns correctly
- Avoiding destructive actions accidentally
- Using
-prunefor exclusions - Testing with dry-run style commands first
Core Syntax
find [PATH] [EXPRESSION] [ACTION]
| Part | Meaning |
|---|---|
PATH | Directory to start searching from |
EXPRESSION | Filters like -name, -type, -size |
ACTION | What to do with matches, such as -print or -delete |
Core Filters
| Filter | Meaning | Example |
|---|---|---|
-name | Match filename exactly | find /var/www -name "wp-config.php" |
-iname | Case-insensitive filename match | find . -iname "*.jpg" |
-type f | Match files only | find . -type f -name "*.php" |
-type d | Match directories only | find . -type d -name "uploads" |
-size +100M | Match files larger than 100 MB | find /backups -size +100M |
-mtime -1 | Modified in the last day | find . -mtime -1 |
-ctime -2 | Metadata changed in the last 2 days | find . -ctime -2 |
-user root | Match owner | find /var/www -user root |
-perm 777 | Match permission bits | find . -perm 777 |
Actions
| Action | Meaning | Example |
|---|---|---|
-print | Show matches | find . -name "*.log" -print |
-ls | Show ls -l style output | find . -type f -ls |
-delete | Remove matches | find /var/log -name "*.log" -delete |
-exec CMD {} \; | Run a command on each match | find . -name "*.php" -exec chmod 644 {} \; |
-ok CMD {} \; | Confirm before running | find . -name "*.bak" -ok rm {} \; |
-printf | Custom formatted output | find . -printf "%f %s\n" |
Practical Examples
Find a specific file
find /var/www/html -name "wp-config.php"
Find all PHP files
find /var/www/html -name "*.php"
Find directories only
find /var/www/html -type d -name "uploads"
Find large files
find /var/www/html -type f -size +100M
Find empty files
find /var/www/html -type f -empty
Find and delete logs
find /var/log -name "*.log" -delete
Fix permissions
find /var/www/html -name "*.php" -exec chmod 644 {} \;
Best Practices
- Start with a narrow path.
- Test the search before using
-deleteor-exec. - Quote patterns like
*.phpso the shell does not expand them first. - Use
-maxdepthwhen you only want the top level. - Use
-pruneto skip cache or vendor directories.
What's Next
- Learn the syntax and expression rules in the examples above.
- Practice a few searches against
/var/www,/home, or/var/log. - Add
findto shell scripts for cleanup, audits, and bulk maintenance.