The find
command is a powerful tool in Linux used for searching files and directories based on various conditions such as name, size, type, date, and permissions.
1. Basic Syntax
[path]
→ The directory to search (default is current directory.
).[options]
→ Conditions like file name, type, size, etc.[expression]
→ Actions to perform on found files.
2. Find Files by Name
- Searches for
file.txt
in the/home
directory. -name
is case-sensitive.
Case-Insensitive Search
-iname
ignores case differences.
3. Find Files by Type
-type f
→ Finds only files.
-type d
→ Finds only directories.
-type l
→ Finds symbolic links.
4. Find Files by Size
- Finds files larger than 100MB.
- Finds files smaller than 50KB.
- Finds files exactly 1GB in size.
5. Find Files by Modification Time
-mtime -7
→ Files modified in the last 7 days.
-mtime +30
→ Files modified more than 30 days ago.
-mmin -60
→ Files modified in the last 60 minutes.
6. Find Files by Permissions
- Finds files with 777 (full) permissions.
- Finds files where the owner has read permission.
- Finds files where the group has write permission.
- Finds files where others have execute permission.
7. Find and Delete Files
- Deletes all
.log
files in/tmp
.
- Deletes
.log
files older than 30 days.
🚨 Be careful while using -delete
or rm
!
8. Find and Execute Commands (-exec
)
- Finds all
.php
files and changes permissions to 644.
- Moves all
.txt
files to/backup/
.
- Searches for the word “error” in all files.
9. Find and Print File Details
- Lists detailed information about each found file.
10. Exclude a Directory from Search
- Skips
/proc
directory and searches for.log
files.
11. Find Empty Files and Directories
- Finds empty files.
- Finds empty directories.
12. Find Recently Accessed Files
- Files accessed in the last 1 day.
- Files accessed in the last 30 minutes.