All cheat sheets

find Cheat Sheet

GNU find reference: by name/type/size/time, actions, exec, and prune patterns.

CLI & Shell
find
filesystem
cli

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.

bash
find . -name '*.js'
find src -iname '*test*'
find . -type f -name '*.log'
find . -type d -name 'node_modules'
Table
PredicateMeaning
-name PATTERNCase-sensitive basename match.
-iname PATTERNCase-insensitive basename match.
-type fRegular file.
-type dDirectory.
-type lSymbolic link.
-path PATTERNMatch the full path.
-ipath PATTERNCase-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.

bash
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
Table
TestMeaning
-size +10MLarger than 10 megabytes in GNU find units.
-mmin -30Modified within the last 30 complete minutes.
-mtime +7Modified more than seven complete 24-hour periods ago.
-newer FILEModified more recently than FILE.
-daystartFor 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.

bash
find . -type f \( -name '*.js' -o -name '*.ts' \)
find . -type f -not -path './node_modules/*'
find . -type f -name '*.log' -a -size +1M
Table
OperatorMeaning
-aAND; implicit between adjacent expressions.
-oOR.
! EXPRNegate 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.

bash
find . -type f -name '*.tmp' -print
find . -type f -name '*.tmp' -exec rm -- {} +
find . -type f -name '*.txt' -exec wc -l {} +
bash
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.

bash
find . -path './node_modules' -prune -o -path './.git' -prune -o \
  -type f -name '*.ts' -print
bash
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.

bash
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