find(1)
Search for files in a directory hierarchy matching specified criteria.
Synopsis
find [-H] [-L] [-P] [-D debugopts] [-Olevel] [starting-point...] [expression]Description
find recursively descends the directory tree rooted at each starting-point (default is current directory) and evaluates an expression against each file. It outputs pathnames of files matching the criteria. Expressions consist of options (like -type, -name), tests (like -size, -mtime), and actions (like -print, -delete).
By default, find outputs matching paths to stdout. It handles symbolic links carefully: -H follows symlinks on the command line, -L follows all symlinks, and -P (default) never follows symlinks. This makes it suitable for both simple filename searches and complex filtering by permissions, ownership, timestamps, and file attributes.
Common options
| Flag | What it does |
|---|---|
-type f | Match regular files only |
-type d | Match directories only |
-name pattern | Match pathname against glob pattern (e.g., '*.txt') |
-iname pattern | Case-insensitive version of -name |
-path pattern | Match full pathname against glob pattern |
-size n | Match files of size n (e.g., '+10M', '-5k', '100c') |
-mtime n | Match files modified n days ago (e.g., '-3' for last 3 days, '+7' for older than 7) |
-perm mode | Match files with specific permissions (e.g., '-755', '/u+x') |
-user name | Match files owned by user |
-exec cmd {} \; | Execute command on each match; {} replaced with filename |
-delete | Delete matched files (use with caution) |
-maxdepth n | Limit recursion depth to n levels |
Examples
Find all .log files in current directory and subdirectories
find . -name '*.log'Find regular files larger than 100MB in /home
find /home -type f -size +100MFind .txt files modified in the last 7 days
find . -type f -mtime -7 -name '*.txt'Find setuid files in /etc
find /etc -type f -perm /u+sFind 'backup' directories up to 2 levels deep, excluding deeper matches
find . -maxdepth 2 -type d -name 'backup'Find all files containing the string 'TODO'
find . -type f -exec grep -l 'TODO' {} \;Delete all .tmp files (be careful with this)
find . -name '*.tmp' -deleteDelete log files older than 30 days in /var/log
find /var/log -type f -mtime +30 -exec rm {} \;