🔍
find cheat sheet
find walks a directory tree and tests each file. Syntax: find WHERE TESTS ACTIONS. Put -maxdepth early, and quote patterns so the shell does not expand them.
Click any command to copy it.
By name and type
| Command | What it does |
|---|---|
find . -name "*.log" | Files whose name matches (case-sensitive). |
find . -iname "*.jpg" | Case-insensitive name match. |
find . -type d | Directories only. |
find . -type f | Regular files only. |
find . -maxdepth 2 -name '*.conf' | Limit how deep it searches. |
find / -name "nginx.conf" 2>/dev/null | Search the whole system and hide permission errors. |
find . -empty | Empty files and directories. |
By size, time and permissions
| Command | What it does |
|---|---|
find . -size +100M | Files larger than 100 MB. |
find . -size 0 | Zero-byte files. |
find . -mtime -7 | Modified in the last 7 days. |
find . -mtime +30 | Modified more than 30 days ago. |
find . -mmin -60 | Modified in the last 60 minutes. |
find . -newer reference.txt | Newer than another file. |
find . -perm 777 | Files with exactly these permissions. |
find . -user alice | Files owned by a user. |
find . -xtype l | Broken symbolic links. |
Combine and exclude
| Command | What it does |
|---|---|
find . \( -name '*.jpg' -o -name '*.png' \) | Match either pattern. |
find . -not -path '*/node_modules/*' -name '*.js' | Exclude a directory. |
find . -name '*.bak' -not -newer ref.txt | Combine conditions with -not. |
Do something with the results
| Command | What it does |
|---|---|
find . -name '*.log' -exec ls -lh {} + | Run a command on matches (batched). |
find . -name '*.tmp' -delete | Destructive: Delete all matching files. Run without -delete first to preview. |
find . -type f -exec chmod 644 {} + | Set permissions on every file. |
find . -type d -exec chmod 755 {} + | Set permissions on every directory. |
find . -name '*.txt' -print0 | xargs -0 grep -l 'error' | Safely pipe file names with spaces to another command. |
find . -type f -exec du -h {} + | sort -rh | head -20 | The 20 largest files. |
Frequently asked questions
Why does find say 'paths must precede expression'?
A shell expanded your wildcard. Quote the pattern: -name '*.txt'.
How do I safely delete files found by find?
Run the same command without -delete first to see the list, then add -delete (put it last).