Introduction#
Hello! In this article, we’ll learn about the find command.
find is a powerful command that recursively searches through directory trees to locate files and directories that match specific criteria. You can search by name, size, modification time, permissions, and many other conditions.
What is the find Command?#
The find command is an external command used to search for files and directories within the file system that match specific criteria. It’s an extremely flexible tool that can perform simple name searches, complex conditional searches, and even execute commands on search results.
Basic Syntax#
1
|
find [starting directory] [search criteria] [action]
|
Main Options#
Search Criteria Options#
| Option |
Description |
Example |
-name pattern |
Search by filename (case-sensitive) |
find . -name "*.txt" |
-iname pattern |
Search by filename (case-insensitive) |
find . -iname "*.TXT" |
-type type |
Search by file type (f=file, d=directory) |
find . -type f |
-size size |
Search by file size |
find . -size +100M |
-mtime days |
Search by modification time |
find . -mtime -7 |
-user username |
Search by owner |
find . -user john |
-perm permissions |
Search by permissions |
find . -perm 644 |
Action Options#
| Option |
Description |
-print |
Display search results (default behavior) |
-exec command {} \; |
Execute command on search results |
-delete |
Delete search results |
-ls |
Display search results in ls -l format |
Usage Examples#
Example 1: Search for File by Name#
1
|
find /home/user -name "report.txt"
|
Result:
1
2
|
/home/user/documents/report.txt
/home/user/backup/report.txt
|
Example 2: Search for Files with Specific Extension#
Result:
1
2
3
|
./app.log
./system/error.log
./backup/access.log
|
Using the wildcard (*), we search for all files ending with .log.
Example 3: Search by File Type#
1
2
3
4
5
|
# Search for directories only
find . -type d
# Search for regular files only
find . -type f
|
Result:
1
2
3
4
5
6
7
8
9
|
# With -type d
./documents
./documents/work
./images
# With -type f
./readme.md
./documents/report.txt
./images/photo.jpg
|
Example 4: Search by File Size#
1
2
3
4
5
6
7
8
|
# Find files larger than 100MB
find . -type f -size +100M
# Find files smaller than 1KB
find . -type f -size -1k
# Find files exactly 10MB
find . -type f -size 10M
|
Result:
1
2
|
./videos/movie.mp4
./backup/archive.tar.gz
|
Size units: c(bytes), k(kilobytes), M(megabytes), G(gigabytes).
Example 5: Search by Modification Time#
1
2
3
4
5
6
7
8
|
# Files modified within the last 7 days
find . -type f -mtime -7
# Files modified exactly 7 days ago
find . -type f -mtime 7
# Files modified more than 7 days ago
find . -type f -mtime +7
|
Result:
1
2
3
|
./recent-file.txt
./logs/today.log
./documents/updated.md
|
Example 6: Combine Multiple Conditions#
1
2
3
4
5
|
# .txt files modified within the last 7 days
find . -type f -name "*.txt" -mtime -7
# .log files larger than 100MB
find . -type f -name "*.log" -size +100M
|
Result:
1
2
|
./notes/recent.txt
./documents/work.txt
|
Example 7: Execute Commands on Search Results#
1
2
3
4
5
6
7
8
|
# Display detailed info of found files
find . -name "*.txt" -exec ls -lh {} \;
# Display contents of found files
find . -name "config.json" -exec cat {} \;
# Process multiple files together
find . -name "*.txt" -exec grep "error" {} +
|
Result:
1
2
|
-rw-r--r-- 1 user group 1.2K Oct 26 10:30 ./notes.txt
-rw-r--r-- 1 user group 3.4K Oct 25 15:20 ./report.txt
|
{} is replaced with the found filename, and \; marks the end of the command.
Example 8: Find Empty Files or Directories#
1
2
3
4
5
|
# Find empty files
find . -type f -empty
# Find empty directories
find . -type d -empty
|
Result:
1
2
|
./temp/placeholder.txt
./logs/empty-dir
|
Example 9: Find Files Owned by Specific User#
1
|
find /home -type f -user john
|
Result:
1
2
|
/home/john/document.txt
/home/shared/john-report.pdf
|
Example 10: Delete Old Log Files#
1
2
|
# Find and delete log files older than 30 days
find /var/log/myapp -name "*.log" -mtime +30 -delete
|
Warning: Use the -delete option carefully!
Tips & Notes#
- Starting directory: If omitted, searches from current directory (
.)
- Quote wildcards: Use quotes around patterns like
"*.txt" to prevent shell expansion
- Be careful with -delete: Always verify with
-print before using -delete
1
2
3
4
|
# First verify
find . -name "*.tmp" -print
# Then delete if OK
find . -name "*.tmp" -delete
|
- Performance: Large directory trees can take significant time to search
- Permission errors: Directories without access permissions will show error messages. Use
2>/dev/null to hide them
1
|
find / -name "file.txt" 2>/dev/null
|
- -exec termination:
-exec must end with either \; or +
\;: Execute command for each file individually
+: Process multiple files together (more efficient)
Practical Usage#
Find Large Files to Save Disk Space#
1
2
|
# Display files larger than 100MB sorted by size
find /home -type f -size +100M -exec ls -lh {} \; | sort -k5 -h
|
Find Recently Modified Files#
1
2
3
4
5
|
# Files modified today
find . -type f -mtime 0
# Files modified within the last 24 hours
find . -type f -mmin -1440
|
Exclude Specific Extensions from Search#
1
2
|
# Find all files except .log files
find . -type f ! -name "*.log"
|
Search with Multiple Conditions (OR)#
1
2
|
# Find .txt or .md files
find . -type f \( -name "*.txt" -o -name "*.md" \)
|
Save Search Results to File#
1
|
find /var/log -name "*.log" -mtime +30 > old-logs.txt
|
Delete Backup Files in Bulk#
1
2
|
# Delete backup files ending with .bak or ~
find . -type f \( -name "*.bak" -o -name "*~" \) -delete
|
Summary#
In this article, we learned about the find command.
Key Points:
find is a powerful file search tool
- Can search by name, size, time, permissions, and more
-exec allows executing any command on search results
- Multiple conditions can be combined for precise searches
- Use dangerous options like
-delete carefully
Mastering the find command allows you to quickly locate files among thousands and perform bulk operations efficiently. Start with basic searches and gradually work your way up to more complex conditions!
Keep learning Linux commands!