Friday, February 20, 2015

Delete files older than certain period of time in UNIX

It is often required to delete old unnecessary archived files when administrating a UNIX system. So to make this work easier I have written the following command:

find /directory/path  -type f -mtime +1  -print | xargs -I {} rm {}

By executing this command, all files in the specified folder that are older than a day i.e. 24 hours will be deleted

If you want to specify the time in minutes do this

find /directory/path  -type f -mmin +360  -print | xargs -I {} rm {}

Specify your time instead of 360.

You could also write a shell script and schedule it in crontab to run it any time you want to.

CAUTION: slightest error could cause the loss of invaluable data. So do it at your own risk.

No comments:

Post a Comment