LinuxCommandLibrary

find

Find files and directories

TLDR

Find files by extension

$ find [root_path] -name '[*.ext]'
copy

Find files matching multiple path/name patterns
$ find [root_path] -path '[*/path/*/*.ext]' -or -name '[*pattern*]'
copy

Find directories matching a given name, in case-insensitive mode
$ find [root_path] -type d -iname '[*lib*]'
copy

Find files matching a given pattern, excluding specific paths
$ find [root_path] -name '[*.py]' -not -path '[*/site-packages/*]'
copy

Find files matching a given size range, limiting the recursive depth to "1"
$ find [root_path] -maxdepth 1 -size [+500k] -size [-10M]
copy

Run a command for each file (use {} within the command to access the filename)
$ find [root_path] -name '[*.ext]' -exec [wc -l] {} \;
copy

Find all files modified today and pass the results to a single command as arguments
$ find [root_path] -daystart -mtime [-1] -exec [tar -cvf archive.tar] {} \+
copy

Search for either empty files or directories and delete them verbosely
$ find [root_path] -type [f|d] -empty -delete -print
copy

SYNOPSIS

find [path...] [expression]

PARAMETERS

path
    The directory (or directories) in which to begin the search. If no path is specified, the current directory is used.

-name pattern
    Search for files and directories whose base name matches the specified pattern. The pattern can include wildcards (e.g., *, ?, []).

-type c
    Search for files of a specific type, where 'c' is a character representing the file type (e.g., 'f' for regular file, 'd' for directory, 'l' for symbolic link).

-size n[ckMG]
    Search for files of a specific size, where 'n' is the size and 'ckMG' represent units (c=bytes, k=kilobytes, M=megabytes, G=gigabytes).

-mtime n
    Search for files modified 'n' days ago. Use +n for older than n days, -n for newer than n days.

-atime n
    Search for files accessed 'n' days ago. Use +n for older than n days, -n for newer than n days.

-user username
    Search for files owned by the specified user.

-group groupname
    Search for files belonging to the specified group.

-perm mode
    Search for files with the specified permission mode.

-exec command {}
    Execute the specified command on each found file. The '{}' is replaced with the filename.

-delete
    Delete files or directories found (USE WITH CAUTION).

-print
    Prints the files' name to standard output. Default behavior.

DESCRIPTION

The find command is a powerful and versatile utility in Linux and other Unix-like operating systems used for locating files and directories within a file system.

It recursively searches through a specified directory (or the current directory by default) and its subdirectories, applying various filters based on criteria such as file name, size, type, modification time, permissions, and more. This allows users to pinpoint specific files or groups of files that meet certain characteristics.

The find command is a fundamental tool for system administrators, developers, and users who need to manage and manipulate files effectively.

It is often combined with other commands using piping (|) and the -exec option to perform actions on the found files, such as deleting, copying, or modifying them. Proper usage of find requires careful consideration of the search criteria and the intended actions to avoid unintended consequences.

CAVEATS

Using find with the -delete or -exec options can be dangerous if the search criteria are not carefully defined. Always test your find command without these options first to ensure you are targeting the correct files. Be cautious of symlink loops when searching across the entire filesystem.

PERFORMANCE CONSIDERATIONS

Searching large file systems with complex criteria can be resource-intensive. To improve performance, try to narrow down your search as much as possible by providing specific paths and limiting the scope of the search. Consider using locate if speed is a primary concern, although locate depends on an indexed database and may not be up-to-date.

EXIT STATUS

The find command exits with a status of 0 if all files were successfully processed. An exit status greater than 0 indicates that an error occurred.

HISTORY

The find command has been a core utility in Unix-like operating systems since the early days. It has evolved over time with enhancements in its features and options, but its fundamental purpose of searching and locating files within a file system remains unchanged.

It is a descendant of the original Unix find, has been part of the POSIX standard and remains ubiquitous across Linux distributions. Its consistent availability and well-defined behavior contribute to its reliability and widespread usage. The introduction of features like regular expression support and the ability to execute commands on found files have expanded its utility greatly.

SEE ALSO

locate(1), grep(1), xargs(1)

Copied to clipboard