Thursday, January 27, 2011

Linux searching for files order than X days

one of the frequently used commands for system maintenance is purging old files, e.g. log files, database backup, temporary files.


find ... -exec Command

In Linux, to search files older than 30 days:


find ~/log/application-*.log.tar.gz -mtime +30


to remove the files returned by find command, use option exec:


find ~/log/application-*.log.tar.gz -mtime +30 -exec rm {} \;


Adding a cron job

Edit the cron job list


crontab -e
Add the following lines:

# m h  dom mon dow   command
# MAILTO=your@example.com
0 1 * * * find ~/log/application-*.log.tar.gz -mtime +30 -exec rm {} \;
Note that the job is added to the cron job list of current login user. The find and rm command will also be executed as login user.

No comments:

Post a Comment