Power Rangers: Find Force
Table of Contents
What is Find?
Find searches for files and directories based on name, type, size, time, permissions, owner, and more.
1find [path] [options] [expression]
Find by name
1find . -name "file.txt"
Case senstive
1find . -iname "file.txt"
Find by file type
1find . -type f # files
2find . -type d # directories
3find . -type l # symlinks
Find in a specific directory
1find /var/log -name "*.log"
Limit search depth
1find . -maxdepth 1 -name "*.txt"
Find by size
1find . -size 10M # exactly 10 MB
2find . -size +10M # larger than 10 MB
3find . -size -10M # smaller than 10 MB
Find by time
Modified time
1find . -mtime 7 # modified exactly 7 days ago
2find . -mtime -7 # modified within last 7 days
3find . -mtime +7 # older than 7 days
Access time
1find . -atime -1 # accessed today
Changed time
1find . -ctime -1
Find empty files and directories
1find . -empty
Find by permissions
1find . -perm 644
Executable files
1find . -perm /111
Find by owner
1find . -user root
2find . -group www-data
Combination Conditions
-and (default) -or ! (NOT)
AND
1find . -type f -and -name "*.txt"
OR
1find . -name "*.jpg" -or -name "*.png"
NOT
1find . ! -name "*.txt"
Actions with -exec
Delete files
1find . -name "*.tmp" -delete
or
1find . -name "*.tmp" -exec rm {} \;
Run commands on results
1find . -name "*.log" -exec ls -lh {} \;
Use + for better performance
1find . -name "*.jpg" -exec rm {} +
Advance Usage
Find and pipe safely with space
1find . -name "*.jpg" -print0 | xargs -0 rm
Find large files (top space users)
1find / -type f -size +100M 2>/dev/null
Find files modified in last 10 minutes
1find . -mmin -10
Find files and copy them
1find . -name "*.conf" -exec cp {} /backup/ \;
Find duplicate filenames
1find . -type f -printf "%f\n" | sort | uniq -d
Find broken symlinks
1find . -xtype l
Exclude directories
1find . -path "./node_modules" -prune -o -name "*.js" -print
Find large log files older than 30 days and delete
1find /var/log -type f -name "*.log" -size +100M -mtime +30 -delete