find
Find files and directories
TLDR
Find files by extension
Find files matching multiple path/name patterns
Find directories matching a given name, in case-insensitive mode
Find files matching a given pattern, excluding specific paths
Find files matching a given size range, limiting the recursive depth to "1"
Run a command for each file (use {} within the command to access the filename)
Find all files modified today and pass the results to a single command as arguments
Search for either empty files or directories and delete them verbosely
SYNOPSIS
find [path...] [expression]
path...: Specifies the starting directories where the search begins. If no path is given, the current directory ('.') is assumed.
expression: Consists of options, tests (criteria), actions, and operators that define what to search for and what to do with the results.
PARAMETERS
-name pattern
Searches for files whose base name matches pattern. Wildcards (*, ?, []) are allowed and should often be quoted.
-iname pattern
Like -name, but performs a case-insensitive search.
-type c
Searches for files of type c: f (regular file), d (directory), l (symbolic link), b (block device), c (character device), p (named pipe), s (socket).
-size [+|-]n[cwbkMG]
Searches for files with size n. +n means greater than n, -n means less than n. Units: c (bytes), w (two-byte words), b (512-byte blocks), k (kilobytes), M (megabytes), G (gigabytes).
-mtime [+|-]n
Searches for files whose data was last modified n days ago. +n means more than n days, -n means less than n days. n means exactly n days.
-atime [+|-]n
Similar to -mtime, but based on last access time.
-ctime [+|-]n
Similar to -mtime, but based on last status change time (metadata, e.g., permissions or owner).
-perm mode
Searches for files with permissions matching mode (e.g., 644, /u=r,go=r).
-user name
Searches for files owned by user name (or UID).
-group name
Searches for files owned by group name (or GID).
-maxdepth levels
Descends at most levels (a non-negative integer) levels of directories below the starting point.
-mindepth levels
Does not apply any tests or actions at levels less than levels (a non-negative integer) below the starting point.
-print
Prints the full file name to standard output, followed by a newline. This is the default action if no other action is specified.
-print0
Prints the full file name to standard output, followed by a null character. This is useful for piping to commands like xargs -0 to handle filenames with spaces or other special characters correctly.
-exec command {} \;
Executes command on each found file. {} is replaced by the current file name. The command must be terminated by an escaped semicolon (\;).
-exec command {} +
Executes command on all found files as a single batch, similar to xargs. This is generally more efficient than -exec ... \; for multiple files.
-delete
Deletes the found files. This action should be used with extreme caution.
-ok command {} \;
Like -exec, but prompts the user for confirmation before executing the command for each file. Useful for interactive deletion or modification.
DESCRIPTION
find is a powerful command-line utility used to search for files and directories within a specified directory hierarchy. It can locate files based on a wide range of criteria, including name, type (file, directory, symbolic link, etc.), size, modification/access/change times, permissions, owner, and group. Once files matching the criteria are found, find can perform various actions on them, such as printing their names, executing other commands, or deleting them.
Its recursive nature allows it to traverse entire file systems, making it an indispensable tool for system administration, scripting, and general file management. find expressions can combine multiple criteria using logical operators, providing highly granular control over searches. It's often used in conjunction with xargs for more complex operations on found files.
CAVEATS
- Performance: On extremely large or slow file systems (e.g., network mounts), find operations can be very time-consuming and resource-intensive.
- Operator Precedence: The order of operations in complex expressions can be tricky. -a (AND) has higher precedence than -o (OR). Parentheses \( ... \) can be used to group expressions, but they must be escaped.
- Filename Handling: Filenames containing spaces, newlines, or other special characters can cause issues when processing find output with other commands. Always prefer -print0 with xargs -0 for robust scripting.
- Permissions: find might encounter "Permission denied" errors when searching directories it does not have read access to. These errors are typically printed to standard error.
- Case Sensitivity: Most criteria like -name are case-sensitive by default. Use -iname for case-insensitive matching.
EXPRESSION ELEMENTS
find expressions are composed of several types of elements, evaluated from left to right:
- Options: Affect the overall operation of find (e.g., -maxdepth, -L).
- Tests (Predicates): Return true or false based on file attributes (e.g., -name, -type, -size, -mtime).
- Actions: Perform an operation on the current file if the preceding tests are true (e.g., -print, -exec, -delete). If no action is specified, -print is implied.
- Operators: Combine expressions logically.
- -a (AND): Logical AND (implied if not specified, e.g., find . -name "*.txt" -type f means AND).
- -o (OR): Logical OR.
- ! (NOT): Logical NOT, negates the following expression. Must be escaped as \!.
- \( ... \): Group expressions. Must be escaped.
HISTORY
The find command is one of the oldest and most fundamental utilities in the Unix ecosystem, dating back to early versions of Unix in the 1970s. It was designed from the outset to recursively search directory hierarchies, a feature crucial for managing complex file systems. Over the decades, it has evolved, with various enhancements like the addition of more sophisticated tests, actions (-delete, -exec {} +), and performance optimizations.
Its design principles, emphasizing flexibility through expressions and actions, have made it an enduring and indispensable tool for system administrators and developers alike, forming a cornerstone of shell scripting and automation on Unix-like operating systems.