The find command
search for files in a directory hierarchy 36 examples from the lessons of the IVski Linux Playground — try them in the terminal next to them.
Options in the examples
-name pattern- Base of file name (the path with the leading directories removed) matches shell pattern pattern.
-type c- File is of type c:
-size n[cwbkMG]- File uses less than, more than or exactly n units of space, rounding up.
-mtime n- File's data was last modified less than, more than or exactly n*24 hours ago.
-perm mode- File's permission bits are exactly mode (octal or symbolic).
-exec command ;- Execute command; true if 0 status is returned.
-delete- Delete files or directories; true if removal succeeded.
-iname pattern- Like -name, but the match is case insensitive.
-newer reference- Time of the last data modification of the current file is more recent than that of the last data modification of the reference file.
-not expr- Same as ! expr, but not POSIX compliant.
-print0- True; print the full file name on the standard output, followed by a null character (instead of the newline character that -print uses).
Examples
find / -type f -size +100M 2>/dev/null
find [where] [what] [action]
find . -name "*.txt".txt files in current dir (recursive)
find / -name "config.php"Find config.php anywhere
find . -iname "*.TXT"Case-insensitive
find . -name "log*"Files starting with 'log'
find . -type fFiles only
find . -type dDirectories only
find . -type lSymlinks only
find / -size +100MLarger than 100MB
find / -size -1kSmaller than 1KB
find . -size +10M -size -100MBetween 10MB and 100MB
find . -mtime -7Modified in last 7 days
find . -mtime +30Modified more than 30 days ago
find . -newer reference.txtNewer than reference.txt
find . -perm 777Exact permissions 777
find . -perm -u+xOwner has execute
find / -perm -4000Setuid files
find . -name "*.log" -deleteDelete found files
find . -name "*.txt" -exec cat {} \;Run command on each
find . -name "*.sh" -exec chmod +x {} +Make scripts executable
find . -type f -exec ls -la {} +List found files
find . -name "*.log" -size +10MAND
find . -name "*.log" -o -name "*.tmp"OR
find . -not -name "*.txt"NOT
find . -name "*.txt" | xargs catCat all .txt files
find . -name "*.log" | xargs rmDelete all .log files
find . -name "*.txt" -print0 | xargs -0 rm
find . -name "*.jpg" | xargs -P 8 -I{} convert {} {}.png
find . -name "*.txt" | xargs -I{} cp {} /backup/{}
find /var/log -name '*.log' -mtime +7 | xargs gzip
find . -name '*.py' | xargs wc -l
find /etc -name '*.conf' | xargs grep -l 'ServerName'
find / -size +100M -type fIndividual large files
find / -perm -4000 -type f 2>/dev/nullSetuid files (privilege-escalation risk)
find /home -mtime -1 -type fFiles modified in the last 24 hours
Lessons that use find
- df & du — Disk Usage
- find — Searching Files
- xargs — Build Command Lines
- Mission: Disk Full
- Mission: Security Sweep