File System Housekeeping with find Command
A tidy filesystem is key to a healthy and efficient server. The find
command is a powerful tool for cleaning, compressing, or inspecting files based on name, size, or modification time. Use this guide to manage trace logs, old backups, and large junk files like a pro.
Basic Syntax
find [path] [conditions] [actions]
path
: Where to begin the search (.
for current dir, or/u01/app/logs
for specific)conditions
: Filters like-name
,-mtime
,-size
actions
: What to do:-exec
,-print
,-delete
, etc.
Common Examples
# 1. Delete .trc files older than 1 day
find . -name "*.trc" -type f -mtime +1 -exec rm {} \;
# 2. Compress .log files older than 10 days
find . -name "*.log" -type f -mtime +10 -exec gzip {} \;
# 3. Delete empty files (size 0)
find . -type f -size 0 -exec rm {} \;
# 4. Compress files larger than 10MB
find . -type f -size +10M -exec gzip {} \;
# 5. Find files modified in the last 5 days
find . -type f -mtime -5 -print
# 6. Delete .arc files older than 30 days in a specific directory
find /path/to/directory -name "*.arc" -type f -mtime +30 -exec rm {} \;
# 7. Find & print 5 largest files modified in the last day
find . -type f -mtime -1 -printf "%p %s\n" | sort -k2nr | head -5
# 8. Delete all .gz files older than 7 days
find . -name "*.gz" -type f -mtime +7 -exec rm {} \;
# 9. Compress .trm files older than 0 days (today's)
find . -name "*.trm" -type f -mtime +0 -exec gzip {} \;
# 10. Delete files larger than 100MB
find . -type f -size +100M -exec rm {} \;
📌 Notes
-mtime +N
: Modified more than N days ago-mtime -N
: Modified within the last N days-exec ... {} \;
: Executes a command on each result- Always test with
-print
before-exec rm
to avoid accidental deletions - Use cron to automate regular cleanups