3.5 KiB
Comprehensive Guide to find Command
The find command in Unix/Linux is a powerful utility for traversing directory trees to search for files and directories based on a wide range of criteria. This guide covers its syntax, usage examples, and some tips for creating effective searches.
Syntax
The basic syntax of the find command is:
find [path...] [expression]
[path...]specifies the starting directory/directories for the search. If omitted,finddefaults to the current directory.[expression]is used to define search criteria and actions. It can include options, tests, and actions.
Common Options
-name pattern: Search for files matching the pattern.-iname pattern: Case-insensitive version of-name.-type [f|d|l]: Search for a specific type of item:ffor files,dfor directories,lfor symbolic links.-size [+-]N[cwbkMG]: Search by file size.+Nfor greater than,-Nfor less than,Nfor exactly N units. Units can be specified:c(bytes),w(two-byte words),k(kilobytes),M(megabytes),G(gigabytes).-perm mode: Search for files with specific permissions. Mode can be symbolic (e.g.,u=rwx) or octal (e.g.,0755).-user name: Find files owned by the user name.-group name: Find files owned by the group name.-mtime [+-]N: Files modified in the last N days.+Nfor more than N days ago,-Nfor less than N days ago,Nfor exactly N days ago.-maxdepth levels: Descend at most levels of directories below the command line arguments.-mindepth levels: Do not apply tests or actions at levels less than levels.
Combining Tests
You can combine multiple tests to refine your search:
- AND (implicit):
find . -type f -name "*.txt"finds files (-type f) with a.txtextension. - OR:
find . -type f \( -name "*.txt" -o -name "*.md" \)finds files that end in.txtor.md. - NOT:
find . -type f ! -name "*.txt"finds files that do not end in.txt.
Executing Commands on Found Items
-exec command {} \;: Executecommandon each item found.{}is replaced with the current file name.
Example: find . -type f -name "*.tmp" -exec rm {} \; deletes all .tmp files.
-exec command {} +: Similar to-exec, butcommandis executed with as many found items as possible at once.
Example: find . -type f -exec chmod 644 {} + changes the permissions of all found files at once.
Practical Examples
-
Find All
.jpgFiles in the Home Directory:find ~/ -type f -iname "*.jpg" -
Find and Delete Empty Directories:
find . -type d -empty -exec rmdir {} + -
Find Files Modified in the Last 7 Days:
find . -type f -mtime -7 -
Find Files Larger than 50MB:
find / -type f -size +50M -
Find Files by Permission Setting:
find . -type f -perm 0644
Tips for Effective Searches
- Use Quotation Marks: Always use quotation marks around patterns to prevent shell expansion.
- Test Commands with
-print: Before using-exec, use-printto see what files are found. - Be Specific with Paths: Specify a starting path to reduce search time and avoid unnecessary system-wide searches.
find is an indispensable tool for file management and system administration, offering unparalleled flexibility in searching for files by attributes, sizes, modification times, and more. Mastery of find enhances your command-line efficiency significantly.