Automatically delete files that are X days old

October 22, 2015 — Leave a comment

Everyone has somewhere that collects old, useless files. Maybe it’s a backup that keeps rotating but not deleting things.. or large video files ftp’d from your home security system. Here’s an easy way to keep that directory from filling up the drive. We’ll show you the command, then help you to run it nightly in cron.

Let’s set it up.. video files from our home security system.. they live in /backup/security/files/ and they’re called 2015-10-08a.mp4.

Here’s what we’d run to delete any older than 30 days:

You can change that 30 to a week if you like.. replace it with 7.

Now, let’s put it in a file called /usr/local/bin/

Make it executable:

Add it to cron.. let’s have it run every night at 10pm:

All done!

No Comments

Be the first to start the conversation.

Leave a Reply