bash find files by name, extension, or age
Quick Answer
# Find by name (case-sensitive)
find . -name "config.js"
# Find by extension
find . -name "*.log"
# Find and execute a command on each result
find . -name "*.tmp" -exec rm {} \;
Usage
You need to locate files matching certain criteria across a directory tree.
Other causes & fixes
Find by modification time
# Modified in the last 7 days
find . -mtime -7
# Modified more than 30 days ago
find . -mtime +30
# Modified today
find . -mtime 0
Find by type
find . -type f # files only
find . -type d # directories only
find . -type l # symlinks only
Find by size
find . -size +100M # larger than 100 MB
find . -size -1k # smaller than 1 KB
Exclude directories
find . -name "*.js" -not -path "*/node_modules/*"
Use -exec efficiently with + instead of ;
# + batches results into one command call (faster)
find . -name "*.log" -exec gzip {} +
# Equivalent with xargs
find . -name "*.log" | xargs gzip
Related