find Cheat Sheet
GNU find reference: by name/type/size/time, actions, exec, and prune patterns.
find walks a directory tree and evaluates tests and actions for each path. Quote patterns, use -print0 for arbitrary filenames, and test destructive commands with -print first.
Name and type
The search path comes first, followed by predicates. -name matches a basename with shell-style wildcards; those wildcards must be quoted so the shell passes them to find.
find . -name '*.js'
find src -iname '*test*'
find . -type f -name '*.log'
find . -type d -name 'node_modules'
| Predicate | Meaning |
|---|---|
-name PATTERN | Case-sensitive basename match. |
-iname PATTERN | Case-insensitive basename match. |
-type f | Regular file. |
-type d | Directory. |
-type l | Symbolic link. |
-path PATTERN | Match the full path. |
-ipath PATTERN | Case-insensitive full-path match. |
Size and time
Size units are based on 512-byte blocks for -b and commonly use suffixes such as k, M, and G. Time tests count complete periods; minute variants are often easier to reason about.
find . -type f -size +10M
find . -type f -size -100k
find . -type f -mmin -30
find . -type f -mtime +7
find . -type f -atime -1
| Test | Meaning |
|---|---|
-size +10M | Larger than 10 megabytes in GNU find units. |
-mmin -30 | Modified within the last 30 complete minutes. |
-mtime +7 | Modified more than seven complete 24-hour periods ago. |
-newer FILE | Modified more recently than FILE. |
-daystart | For some day-based tests, measure from today’s midnight. |
Operators and grouping
Adjacent predicates are implicitly ANDed. Use -o for OR, ! or -not for negation, and escaped parentheses to control precedence.
find . -type f \( -name '*.js' -o -name '*.ts' \)
find . -type f -not -path './node_modules/*'
find . -type f -name '*.log' -a -size +1M
| Operator | Meaning |
|---|---|
-a | AND; implicit between adjacent expressions. |
-o | OR. |
! EXPR | Negate an expression. |
\( EXPR \) | Group expressions. |
Printing and executing
-print is the default action in many simple expressions, but making it explicit improves readability. -exec ... {} + batches paths; {} \; runs once per path.
find . -type f -name '*.tmp' -print
find . -type f -name '*.tmp' -exec rm -- {} +
find . -type f -name '*.txt' -exec wc -l {} +
find . -type f -print0 | xargs -0 grep -n 'needle'
-delete removes matching entries directly. Combine it with a precise expression and run the same command with -print before deleting.
Prune excluded directories
-prune prevents descent into a directory. With OR, the excluded branch is pruned and all other paths are printed.
find . -path './node_modules' -prune -o -path './.git' -prune -o \
-type f -name '*.ts' -print
find . -type d \( -name node_modules -o -name .git \) -prune -o \
-type f -print
Useful recipes
Start with . for the current directory, an absolute path for scripts, or multiple starting paths for a combined search.
find src tests -type f -iname '*fixture*' -print
find . -type f -empty -print
find . -type f -perm -111 -print
find . -maxdepth 2 -type f -name '*.json'
References
- GNU findutils Manual
man find