Linux – How to delete specific files in directory that are n days old

Ok so this happened recently when there was a requirement to delete all files in a Linux directory that were 30 days old.

The best I could do is to check the last modified date for each file in the folder in question and then go on deleting them individually but that would have been a nightmare

Below is how I did it.

There could be a better way out there, as I am not a Linux expert this is the best solution I can come up with.

  1. find /home/jaspreet/somefolder/ -mtime +30 -exec rm {} \; 

The above should be read as “find all those files in /home/jaspreet/somefolder whose last modified time is more that 30 days and then execute rm on all of them”

Please note that we are using absolute path to the folder to find files in it. 

Now this works very well for me.

You can extend this to say finding all the TXT files in that folder and then executing the rm command on that batch found.

I strongly recommend that before you do anything its a wise thing to just run find command and check the returned files are what you really would like to remove.

I hope this helps

Cheers

VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)

Speak Your Mind

*

CommentLuv badge