pipeline

find

Recursively walks directories evaluating expressions on each entry. Combines discovery with actions like exec or delete.

find [PATH...] [EXPRESSION]

Common flags

Flag Purpose
-name Match filename by glob (case sensitive)
-iname Match filename by glob, case insensitive
-type Filter by type: f (file), d (dir), l (symlink)
-mtime Match by modification age in days (+N older, -N newer)
-size Match by size (e.g. +100M, -1k)
-exec Run a command on each match (terminate with \; or +)
-print0 Output NUL-separated paths, safe for xargs -0
-maxdepth Limit recursion depth

Examples

find . -name '*.log' -type f

Find all log files below the cwd

find /var/log -mtime +7 -delete

Delete files older than 7 days

find . -type f -size +100M

Find files larger than 100 MB

find . -name '*.pyc' -print0 | xargs -0 rm

Safely delete matches whose names may contain spaces

Gotcha

Expressions like -name '*.log' must be quoted so the shell does not expand the glob before find sees it.

Related commands

← All Linux commands · Linux cheat sheet