find
find
is a powerful command-line utility used to search for files and directories in a directory hierarchy based on various criteria. It can perform actions on the found items.
Basic Syntax
Commonly Used Options
-name pattern
: Search for files matching the pattern.-iname pattern
: Search for files matching the pattern, case-insensitive.-type type
: Search for a specific type (e.g.,f
for files,d
for directories).-size n
: Search for files of a specific size (e.g.,+100M
for files larger than 100MB).-mtime n
: Search for files modifiedn
days ago.-atime n
: Search for files accessedn
days ago.-ctime n
: Search for files changedn
days ago.-user name
: Search for files owned by a specific user.-group name
: Search for files owned by a specific group.-perm mode
: Search for files with specific permissions.-exec command {} \;
: Execute a command on each found item.-delete
: Delete the found files.-maxdepth n
: Limit the search ton
levels of directories.-mindepth n
: Start the search atn
levels of directories.
Examples
Basic Search
Search for files named "example.txt" in the current directory and subdirectories:
Case-Insensitive Search
Search for files named "example.txt" (case-insensitive) in the current directory and subdirectories:
Search by Type
Search for directories named "backup":
Search by Size
Search for files larger than 100MB:
Search by Modification Time
Search for files modified in the last 7 days:
Search by Access Time
Search for files accessed in the last 7 days:
Search by Change Time
Search for files changed in the last 7 days:
Search by User
Search for files owned by the user "john":
Search by Group
Search for files owned by the group "staff":
Search by Permissions
Search for files with 755 permissions:
Execute Command on Found Items
Search for files named "example.txt" and delete them:
Delete Found Files
Search for and delete files named "example.txt":
Limit Search Depth
Search for files named "example.txt" only in the current directory (no subdirectories):
Start Search at Specific Depth
Search for files named "example.txt" starting from the second level of directories:
Conclusion
find
is a versatile tool for searching files and directories based on various criteria. Understanding its options and expressions allows for efficient and powerful file searching and manipulation directly from the command line.