Deleting files older than n days

There is always a need to delete or housekeep filesystem at OS level and normally this is done by deleting files older than certain number of days.

Especially in applications where there are lot of logs or backups being generated, there is a need to automate this process of housekeeping.

Below command helps in deleting files which are older than specified number of days.

GENERAL PRECAUTION: Please avoid running these commands using root account

Command:  find -mtime +5 -exec rm {} \;

The above command deletes all the files which are ending with extension ".log" and is older than 5 days.

Example: You want to delete all the files ending with .log extension, are in /tmp and older than 10 days then below would be the command:

find /tmp/*.log -mtime +10 -exec rm {} \;

0 Response to "Deleting files older than n days"

Post a Comment