Finding Files (find)
The find command searches for files in a directory hierarchy. It's incredibly powerful for locating files based on various criteria.
Basic Usage
find path -name "filename"
Find Syntax
find [where to look] [criteria] [action]
- Where: Starting directory (
.for current) - Criteria: What to look for
- Action: What to do (default: print)
Common Search Criteria
| Option | Description | Example |
|---|---|---|
-name | By filename | -name "*.txt" |
-type | By type | -type f (file), -type d (dir) |
-size | By size | -size +10M |
-mtime | By modification time | -mtime -7 |
-user | By owner | -user alice |
-perm | By permissions | -perm 755 |
Finding by Name
find . -name "*.txt" # All .txt files
find . -name "config*" # Files starting with config
find . -iname "readme*" # Case insensitive
Finding by Type
find . -type f # Files only
find . -type d # Directories only
find . -type l # Symbolic links
Exercise: Find Files
Find all .txt files in your home directory:
Combining Criteria
Use -and (default) or -or:
find . -name "*.txt" -type f
find . -name "*.txt" -or -name "*.md"
find . -type f -not -name "*.log"
Finding by Time
# Modified in last 7 days
find . -mtime -7
# Modified more than 30 days ago
find . -mtime +30
# Accessed in last 24 hours
find . -atime -1
Finding by Size
find . -size +10M # Larger than 10MB
find . -size -1k # Smaller than 1KB
find . -size 0 # Empty files
Actions
Execute Command
find . -name "*.tmp" -delete
find . -type f -exec chmod 644 {} \;
find . -name "*.txt" -exec cat {} \;
Print with Format
find . -type f -printf "%s %p\n" # Size and path
Real-World Examples
Find Large Files
find /home -size +100M -type f
Find Old Files
find /tmp -mtime +30 -type f
Find and Delete
find . -name "*.bak" -delete
Find Empty Directories
find . -type d -empty
Exercise: Find Directories
Find all directories under /home/user:
Key Takeaways
findsearches files by various criteria- Start with path, add options
- Use
-namefor filenames,-typefor file types - Combine criteria with
-andand-or - Use
-execto run commands on found files

