Delete files that are x days old

Rob —  September 6, 2013 — 5 Comments

Sometimes in Linux, you want to clear out older files in a directory. One instance would be if you have a security system and it continuously writes video files to a directory on your NAS (Network Attached Storage) until it fills it up. You’ve figured out that if you keep a week’s worth of video, it will usually leave plenty of space for other users.

What I would suggest here is creating a cron job that runs every night and runs something like the following command:

What it all means:
find: the command that will search for the files
/path/to/files/: the top level directory to start searching
-type f: so we don’t remove directories, only files
-mtime +7: files older than ‘7’ days. Change to ‘+14’ to delete files older than 2 weeks.
-exec: what to do with the files we find
rm -rf: remove them recursively, force
{}: this represents each file we find
\;: the end of the exec

SO – the crontab entry would be this:

This will run every night at 2am.

Related posts from Linux Brigade!

  • Run the same command on many Linux servers at onceRun the same command on many Linux servers at once Ever have to check a list of Linux servers for various things like what version of CentOS they’re running, maybe how long each has been running to get an uptime report? You can – and it’s […]
  • Keep your WordPress site secure!Keep your WordPress site secure! I work for a hosting company. On average, I’ll clean up at least 3-4 WordPress sites each month for our clients. Most of the time, they’re running on old versions of WordPress and have […]
  • Valve’s Steambox will help Linux gamingValve’s Steambox will help Linux gaming If you’re a gamer, then you’ve heard of the steambox. This is Valve’s answer to the xbox, ouya, etc.. offering gaming in your living room.

    They’ll be announcing something today at 10am […]

  • Install Ubuntu / Xubuntu from USBInstall Ubuntu / Xubuntu from USB Installing via USB is much better/easier than via cd/dvd simply because it’s not going to get scratched up, you can easily carry it around with you to loan to friends, etc..

    Let’s set […]

  • Install CSF/LFD Firewall on your Linux Server The first thing on your mind when setting up a new Linux server (dedicated or VPS) is how you can increase security.  One of the best tools in your bag of tricks should be the CSF […]

5 responses to Delete files that are x days old

  1. Finally….I can free up so much space from my Linux now. I’m relatively new to Linux so 90% of the time I have no idea what I’m doing haha. Thanks for posting this.

  2. Wow this is a great use of the cron job. I’m definitely keeping this command because it’d be great for any type of server that keeps old files or constant backups. For example a Minecraft server that does backups every hour. Then you could have it delete all saves older than few days or a week. Not to mention how useful this could be for managing all sorts of backups. Bookmarked for sure.

  3. Thanks so much for posting this,

    It has helped me out so much,

    I’ve freed up so much space thanks to you.

    Eoin

  4. This looks incredibly useful! I’ve wanted to be able to do something like this before, but I didn’t realize it was actually possible. I should’ve realized it was. Linux is awesome like that.

  5. I like the fact that you explained each entry in the command so that anyone can modify accordingly.

    Thanks

Leave a Reply

Text formatting is available via select HTML.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url=""> 

*