find

find #

Search for files in a directory hierarchy.

Examples:

find ~ -type d | wc -l

List number of directories in home dir

find ~ -type f | wc -l

List number of regular files in home dir

File Types #

File Type Description
b Block special device file
c Character special device file
d Directory
f Regular file
l Symbolic link
find ~ -type f -name "*.jpg" -size +1M | wc -l 

Find all .jpg files by type, name, and size.

Size Units #

Character Unit
b 512-byte blocks. Default if no unit specified
c Bytes
w 2-byte words
k Kilobytes
M Megabytes
G Gigabytes

Tests #

Test Description
-cmin n last modified n minutes ago. +/-n more or less than n minutes ago.
-cnewer file files/dirs contents/attributes last modified more recently than file
-ctime n files/dirs contents/attributes last modified n*24 hours ago
-empty empty files/dirs
-group name belonging to group. May be name or ID.
-iname pattern like -name but case-insensitive
-inum n files with inode number n
-mmin n files/dirs whose contents were last modified n minutes ago
-mtime n files/dirs whose contents were last modified n*24 hours ago
-name pattern files/dirs with specified wildcard pattern
-newer file files/dirs whose contents were modified more recently than specified file
-nouser files/dirs that don’t belong to a valid user
-nogroup files/dirs that don’t belong to a valid group
-perm mode files/dirs that have permissions set to the specified mode. Either octal or symbolic
-samefile name Similar to -inum. Match files that share same inode number as file name
-size n Match files of size n
-type c Match files of type c
-user name files/dirs belonging to user name. Username or user ID

Logical Operators #

Operator Description
-and, -a Match if tests on both sides of operator are true
-or, -o Match if tests on either side of operator is true
-not, ! Match if test following the operator is false
( ) Group tests and operators together to form larger expressions.
Since parentheses have special meaning to the shell, must be quoted/escaped

Actions #

Predefined #

Action Description
-delete Delete the currently matching file
-ls Perform the equivalent of ls -dils on the matching file
-print Output the full pathname of the matching file to stdout
-quit Quit once a match has been made

User-defined #

-exec command {} ; -ok command {} ; Note: brace and semicolon have special meaning to the shell and must be quoted Note: the -ok action in place of -exec will prompt user before execution

find ~ -type f -name 'foo*' -ok ls -l '{}' ';'
< ls ... /home/me/bin/foo > ? y
-rwxr-xr-x 1 me  me 224 2007-10-29 18:44 /home/me/bin/foo
< ls ... /home/me/foo.txt > ? y
-rw-r--r-- 1 me  me   0 2016-09-19 12:53 /home/me/foo.txt 

By changing the trailing ‘;’ to a ‘+’ execute the command on the whole list of arguments, instead of for each match

find ~ -type f -name 'foo*' -exec ls -l '{}' +
-rwxr-xr-x 1 me  me 224 2007-10-29 18:44 /home/me/bin/foo
-rw-r--r-- 1 me  me   0 2016-09-19 12:53 /home/me/foo.txt

Options #

Option Description
-depth Process files in dirs before dirs
-maxdepth levels Set the max number of levels will descend into directory tree
-mindepth levels Set the minimum number of levels will descent into a directory tree
-mount Direct find not to traverse directories that are mounted on other file systems
-noleaf Direct find not to optimize based on Unix-like file system