· Jonathan Cutrer · Linux · 2 min read
Automating Disk Cleanup with Cron and Bash
Disk space creep is slow and then suddenly urgent. Here's the cron-based cleanup pattern I use on Linux servers to keep things tidy without thinking about it.
This post was originally published in April 2017 and is part of the legacy archive migration.
Disk space is one of those things that sneaks up on you. A few log files here, some old backups there, and suddenly /var is at 97% at 2am. I started automating cleanup after that happened on a production box — it ate a couple hours of my Saturday and I decided it wasn’t happening again.
The basics are straightforward: cron runs a shell script on a schedule, the script deletes whatever you’ve decided is safe to remove. The trick is being specific enough that you don’t accidentally wipe something important. I scope my cleanup scripts to specific directories and file patterns, and I always log what got deleted so I can audit it later.
Here’s the core pattern I settled on. This particular script handles old log files in /var/log/myapp that are more than 30 days old:
#!/bin/bash
LOGDIR="/var/log/myapp"
ARCHIVE="/var/log/myapp-archive"
LOGFILE="/var/log/cleanup.log"
find "$LOGDIR" -name "*.log" -mtime +30 -type f | while read -r f; do
echo "$(date '+%Y-%m-%d %H:%M:%S') deleting: $f" >> "$LOGFILE"
rm "$f"
doneI drop this in /usr/local/bin/cleanup-logs.sh, make it executable, and add a crontab entry to run it at 3am daily:
0 3 * * * /usr/local/bin/cleanup-logs.shThe find command’s -mtime +30 flag matches files last modified more than 30 days ago. Adjust that number to taste. I also add a check at the top of the script that aborts if free disk space is already above a threshold — no point running cleanup if you have 80% free.
One thing worth noting: rm here is intentional. Sending old logs to a compressed archive first is safer if you’re not sure what to keep, but for truly ephemeral temp files and rotated logs that your system already manages, just deleting them is fine.