Bryan's thoughts on web design and development

How to auto-remove old files/directories from linux

I usually just keep old backups, perhaps for some undefined historical purposes, but more likely just out of laziness. But clearly, this method (or lack thereof) can eat up disk space rather quickly.

I recently created a simple bash script that will find an remove all files or directories that are older than a specified number of days (as determined by modification dates). This helps keep Xobni’s backup directories small without having to manually go in there every few months, which is even more lazy-friendly. Awesome.

#!/bin/bash
# removes old directories to clear space
# 7 = number of days to keep directories/files

find /var/backup/ -type d -mtime +7 -exec rm {} -R \;
find /var/backup/something -type d -mtime +7 -exec rm {} -R \;

I have this script running via cron every week and it works like a charm.

Leave a Reply