How to delete files in Linux older than 2 days

Thursday, 11th November 2010

If you’re looking for a way on how to delete all files in a directory older than let’s say 2 days. Here is how:

1. Enter into the directory where you want to delete the files:
cd /some/example/dir/

2. Use the Linux find command like so:

find . -mtime +2 -exec rm -f {} ;

If you want to delete all files older than the last 3 days or the last X days all you need to change in that example is the +2

To delete all files on your Linux in a certain directory older than 3 days issue:

find . -mtime +3 -exec rm -f {} ;

If you need to put it to execute on crontab, create some little shell script, let’s say:

#!/bin/shcd /some/example/dirfind . -mtime +2 -exec rm -f {} ;

And set it to be executed on your crontab at your desired time, I saw the part with the crontab because this was something I needed to do just recently and I thought it might be helpful to somebody willing to do the same 🙂

Share this on:

Download PDFDownload PDF

Tags:

Leave a Reply

CommentLuv badge