The find
command is a powerful tool for Linux system administrators and users alike. It allows you to search for files and directories within a directory hierarchy based on specific criteria. Whether you’re managing on-premises systems or cloud-based environments, mastering the find
command can significantly improve your efficiency.
In this article, we’ll explore the most common scenarios for using the find
command, along with examples to help you get started.
Most Common Scenarios to Use find
1. Search for Files by Name
find </path/to/look/into> -name <filename>
Use this command to find files that match a specific name. For case-insensitive searches, use the -iname
option:
find </path/to/look/into> -iname <filename>
2. Search for Directories by Name
find </path/to/look/into> -type d -name <directory_name>
This command locates directories that match the specified name. Use -iname
for case-insensitive searches:
find </path/to/look/into> -type d -iname <directory_name>
3. Find Files by Extension
find </path/to/look/into> -name "*.txt"
Search for files with a specific extension, such as .txt
, .log
, .sh
, etc.
4. Search for Hidden Files
find </path/to/look/into> -type f -name ".*"
Hidden files (those starting with a dot .
) can be located using this command.
5. Search for Hidden Directories
find </path/to/look/into> -type d -name ".*"
Use this command to find hidden directories within a specified path.
6. Find Empty Files
find </path/to/look/into> -type f -empty
Locate empty files, which can be useful for cleanup tasks.
7. Find Empty Directories
find </path/to/look/into> -type d -empty
This command identifies empty directories, allowing you to remove unnecessary folders.
8. Search for Files Modified Within the Last X Days
find </path/to/look/into> -type f -mtime -X
Replace X
with the number of days to find files modified within the last X days (e.g., -mtime -7
for files modified in the last week).
9. Search for Files Larger Than a Specific Size
find </path/to/look/into> -type f -size +500M
Find files larger than 500 MB. Replace 500M
with the desired size, and use K
for kilobytes or G
for gigabytes.
10. Execute Commands on Found Files
find </path/to/look/into> -type f -name "*.log" -exec rm {} \;
This command searches for .log
files and deletes them. Replace rm
with any command you want to execute on the found files.
Reference Documentation
For more advanced usage and options, consult the official find
command manual:
GNU/Linux Man Page for find