Ubuntu find Command Safe Usage: Avoid Deletion Accidents
What You'll Learn
- Basic
findsearches (by name, extension, modification date, size) - Safe deletion procedure (list first → verify count → delete)
- Safe practices with
xargs(-print0/xargs -0)
💡 Quick Summary
Follow this order for deletions - it's non-negotiable:
- List first (don't delete)
- Verify count (stop if more than expected)
- Delete safely (use
-print0/xargs -0when possible)
Table of Contents
⚠️ Prerequisites
- OS: Ubuntu
- Shell: bash
- Permissions:
sudowhen needed (for system directories)
1. find Basics (Searching)
1-1. Search by Name (Exact Match)
$ find . -name "error.log"
1-2. Search by Extension (e.g., .log)
$ find . -name "*.log"
1-3. Case-Insensitive Search
$ find . -iname "*.log"
1-4. Files Only / Directories Only
$ find . -type f -name "*.log" # files only $ find . -type d -name "cache" # directories only
2. Filtering by Modification Date & Size
2-1. Files Modified Within Last 7 Days
$ find . -type f -mtime -7
2-2. Files Older Than 30 Days
$ find . -type f -mtime +30
2-3. Files Larger Than 100MB
$ find . -type f -size +100M
💡 Useful for finding "heavy files" during disk space investigations.
3. Before Deletion: List & Count First
3-1. List Files (Without Deleting)
Example: Find .log files under /var/log (use sudo if needed)
$ sudo find /var/log -type f -name "*.log"
3-2. Count Files (Stop If Too Many)
$ sudo find /var/log -type f -name "*.log" | wc -l
💡 If the count is higher than expected, your conditions are too broad. Add -mtime or -size to narrow down.
4. Safe Deletion Methods (Beginner Pattern)
Deletion can cause instant accidents, so it's best to use a fixed procedure.
4-1. First, Narrow Down (e.g., .log older than 30 days)
$ sudo find /var/log -type f -name "*.log" -mtime +30
4-2. Next, Verify Count
$ sudo find /var/log -type f -name "*.log" -mtime +30 | wc -l
4-3. If OK, Delete Safely
Use -print0 and xargs -0 to handle filenames with spaces safely.
$ sudo find /var/log -type f -name "*.log" -mtime +30 -print0 | sudo xargs -0 rm -f
-print0: Output null-delimited (safe for spaces/newlines)xargs -0: Accept null-delimited input correctly
rm -f is powerful. Always follow the "list → count → delete" order.
Is -delete OK to Use? (Answer: Be Careful)
find ... -delete is simple but can cause instant accidents if conditions are wrong.
# Only use if you understand the risks $ sudo find /tmp -type f -mtime +7 -delete
For beginners, master the -print0 | xargs -0 rm pattern first.
5. Common Pitfalls (Where Accidents Happen)
❌ Wrong Path (/ vs ./)
find / ...affects the entire system - be very careful- Always verify the target directory with
pwdandlsfirst
$ pwd $ ls -la
❌ Conditions Too Broad
Just *.log might match too many files. Add -mtime or -size to narrow down.
Example: Older than 30 days AND larger than 10MB
$ sudo find /var/log -type f -name "*.log" -mtime +30 -size +10M
❌ Filenames with Spaces/Newlines Break
This is THE reason to use -print0 / xargs -0.
6. Post-Deletion Verification
After deletion, verify disk usage:
$ df -h
📋 Test Environment
Commands tested on Ubuntu 24.04 LTS / bash 5.2.