find Command
The find command searches for files and directories in the file system based on various criteria such as name, size, type, permissions, and modification time. It's an essential tool for system administration, file management, and automation scripts.
Syntax
Description
The find command recursively searches through directory trees to locate files and directories that match specified criteria. It can search by name patterns, file types, sizes, permissions, timestamps, and many other attributes.
Key features:
- Recursive directory traversal
- Multiple search criteria combinations
- Pattern matching with wildcards
- Execute actions on found files
- Logical operators (AND, OR, NOT)
Common Search Criteria
| Criteria | Description | Example |
|---|---|---|
-name |
Search by filename (case-sensitive) | find . -name "*.txt" |
-iname |
Search by filename (case-insensitive) | find . -iname "*.TXT" |
-type |
Search by file type (f=file, d=directory) | find . -type d |
-size |
Search by file size | find . -size +100M |
-mtime |
Search by modification time | find . -mtime -7 |
-perm |
Search by permissions | find . -perm 755 |
-user |
Search by owner | find . -user john |
Actions on Found Files
- -print - Print the pathname (default action)
- -delete - Delete the found files
- -exec command {} \; - Execute command on each file
- -execdir command {} \; - Execute command in file's directory
- -ls - List files in ls -dils format
- -printf format - Print using format string
Examples
Find files by name
find . -name "config.txt"
find /var/log -iname "*error*"
Search for PDF files, specific config file, or files containing "error" (case-insensitive)
Find by file type
find /home -type d -name "backup*" # Find backup directories
find . -type l # Find symbolic links
Search for specific file types: regular files, directories, or symbolic links
Find by size
find /tmp -size -1k # Files smaller than 1KB
find . -size 50c # Files exactly 50 bytes
Size units: c (bytes), k (KB), M (MB), G (GB)
Find by modification time
find /var/log -mtime +30 # Modified more than 30 days ago
find . -mmin -60 # Modified in last 60 minutes
Time-based searches using days (-mtime) or minutes (-mmin)
Execute actions on found files
find . -name "*.c" -exec grep "main" {} \;
find /var/log -name "*.log" -exec gzip {} \;
find . -type f -exec chmod 644 {} \;
Delete files, search within files, compress logs, or change permissions
Complex searches with logical operators
find . -name "*.log" -a -size +10M # AND: .log files larger than 10MB
find . -type f ! -name "*.tmp" # NOT: files that are NOT .tmp
find . \( -name "*.c" -o -name "*.h" \) -exec grep "TODO" {} \;
Combine multiple criteria using logical operators
⚡ Performance Tips
- Limit search scope: Start from specific directories, not root
- Use -maxdepth: Limit how deep to search in directory tree
- Put -name first: More efficient than other criteria
- Use -prune: Skip certain directories entirely
- Consider locate: Use locate command for filename-only searches
💡 Tips and Best Practices
- Quote patterns: Always quote wildcards to prevent shell expansion
- Test first: Use -print before -delete to verify what will be removed
- Use -print0: Handle filenames with spaces safely
- Combine with xargs: Process many files efficiently
- Check permissions: Ensure you have read access to search directories
- Use absolute paths: Be explicit about where to search