$linuxjunkies
>

find(1)

Search for files in a directory hierarchy matching specified criteria.

UbuntuDebianFedoraArch

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

FlagWhat it does
-type fMatch regular files only
-type dMatch directories only
-name patternMatch pathname against glob pattern (e.g., '*.txt')
-iname patternCase-insensitive version of -name
-path patternMatch full pathname against glob pattern
-size nMatch files of size n (e.g., '+10M', '-5k', '100c')
-mtime nMatch files modified n days ago (e.g., '-3' for last 3 days, '+7' for older than 7)
-perm modeMatch files with specific permissions (e.g., '-755', '/u+x')
-user nameMatch files owned by user
-exec cmd {} \;Execute command on each match; {} replaced with filename
-deleteDelete matched files (use with caution)
-maxdepth nLimit 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 +100M

Find .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+s

Find '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' -delete

Delete log files older than 30 days in /var/log

find /var/log -type f -mtime +30 -exec rm {} \;

Related commands