Managing disk space is a key responsibility for sysadmins, developers, and DevOps engineers alike. Over time, temporary directories, media folders, and backups accumulate outdated or oversized files that eat into precious storage. Left unchecked, this can lead to full disks, failed deployments, or even data loss.
In this tech concept, you’ll learn how to clean up old files based on age and size like a pro, using a combination of find
, du
, and xargs
. We’ll cover safe deletion practices, moving files to archive folders, handling hidden files, permission concerns, and real-world cleanup strategies for common directories like /tmp
, /var/log
, and media libraries.
For 20+ years, I’ve turned code into products and ideas into growth stories. I believe in tech’s power to transform—and I’m here to help new tech enthusiast tap into it. Your journey starts now with me, and it’s full of possibility.
Why Combine Age and Size for Cleanup?
Cleaning based only on size might keep your disk space low, but may delete recent files. Cleaning only by age might keep old but tiny files. The ideal cleanup strategy combines both:
- Delete files older than X days
- Filter files that are larger than Y MB
- Handle hidden files and restricted permissions
- Provide a safety prompt or move files to an archive first
Prerequisites
You’ll need:
- Basic knowledge of the Linux shell
- Access to
find
,du
, andxargs
(installed by default on most systems) - Sudo access if targeting system directories (e.g.,
/var/log
,/tmp
)
Step 1: Identify Old and Large Files with find
To find files older than 30 days and larger than 100MB:
find /path/to/dir -type f -mtime +30 -size +100M
-type f
: Only files (ignores directories)-mtime +30
: Modified more than 30 days ago-size +100M
: Size greater than 100MB
Example: Check /tmp
for old, large files
find /tmp -type f -mtime +15 -size +50M
Step 2: View Disk Usage with du
for Context
Before deleting anything, it’s smart to see how much space is being used.
du -sh /tmp
Or for more detail:
du -ah /tmp | sort -hr | head -n 20
This shows the 20 largest items in /tmp
, sorted by size.
Step 3: Safely Delete Files with xargs
+ Confirmation
To delete interactively, use xargs
with rm -i
:
find /tmp -type f -mtime +15 -size +50M -print0 | xargs -0 rm -i
-print0
and-0
handle filenames with spacesrm -i
prompts before each deletion
Step 4: Move Files to an Archive Instead of Deleting
If you want to archive before deleting, create an archive folder and move matching files:
mkdir -p ~/old_files_archive
find /var/log -type f -mtime +60 -size +10M -exec mv {} ~/old_files_archive/ \;
Later, you can back them up, compress, or delete as needed.
Step 5: Handle Hidden Files and Dotfiles
Hidden files (starting with .
) are often skipped, but you may want to clean them too. find
doesn’t ignore them unless specified.
find /home/user/.cache -type f -mtime +20 -size +5M
To clean both visible and hidden files:
find /home/user -type f -name ".*" -mtime +30 -size +10M
Or just drop -name
altogether and scan the full directory tree.
Step 6: Manage Permission Issues
When dealing with system directories, you’ll often hit permission-denied errors. Prefix with sudo
:
sudo find /var/log -type f -mtime +60 -size +10M -delete
⚠️ Use
-delete
only after verifying with-ls
or
Example safe dry-run before real deletion:
sudo find /var/log -type f -mtime +60 -size +10M -exec ls -lh {} \;
Step 7: Wrap It Up in a Cleanup Script
Here’s a reusable Bash script for automated cleanups:
#!/bin/bash
TARGET_DIR="/tmp"
AGE_DAYS=15
SIZE_LIMIT="+50M"
ARCHIVE_DIR="$HOME/tmp_archive"
mkdir -p "$ARCHIVE_DIR"
find "$TARGET_DIR" -type f -mtime +"$AGE_DAYS" -size "$SIZE_LIMIT" -exec mv -v {} "$ARCHIVE_DIR" \;
echo "Moved files older than $AGE_DAYS days and larger than $SIZE_LIMIT to $ARCHIVE_DIR"
Make it executable and schedule with cron if needed:
chmod +x cleanup_tmp.sh
Step 8: Real-World Examples
1. Clean /tmp
Directory Automatically
sudo find /tmp -type f -mtime +10 -size +20M -delete
Schedule it with cron to run every night:
0 2 * * * /usr/local/bin/cleanup_tmp.sh
2. Clean Media Directories
find ~/Videos -type f -mtime +60 -size +500M -print0 | xargs -0 rm -i
This interactively deletes large videos not accessed in the last 2 months.
Bonus: Dry Run Before Destruction
Always dry-run before deletion:
find /path/to/check -type f -mtime +30 -size +100M -exec ls -lh {} \;
This lets you audit what will be deleted or archived.
My Tech Advice: Disk cleanup is not just about saving space—it’s about improving system performance, avoiding service outages, and staying organized. By combining
find
,du
, andxargs
, you can build a powerful, automated, and safe cleanup strategy tailored to your needs. Whether it’s cleaning/tmp
, archiving logs, or pruning a bloated media directory, mastering this approach makes you a true command-line pro.Ready to build your own tech solution ? Try the above tech concept, or contact me for a tech advice!
#AskDushyant
Note: The names and information mentioned are based on my personal experience; however, they do not represent any formal statement.
#TechConcept #TechAdvice #Scripting #Shell #Linux #MacOS #Server #Production
Leave a Reply