Friday, 14 May 2010

How to delete old files in Linux

In order to delete OLD files older than XX days, execute following command on your Linux box, where /opt/app/logs is the directory where files are located:

find /opt/app/logs -mtime +XX -exec rm {} \;

For example, to delete files older than 90 days:

find /opt/app/logs -mtime +90 -exec rm {} \;

If the same directory has multiple type of files, and want to delete ONLY those with .log extension:

find /opt/app/logs -name '*.log' -mtime +90 -exec rm {} \;

If you want to see the files to be deleted, run the same command removing "-exec rm {} \;"