Monday, 16 November 2015

Delete Files Older Than 2 years on Linux

The logic helps in the automatic deletion of unnecessary files that keep on getting accumulated in the server without manual intervention .

//To delete the Cost* folders older than 2years under /usr/test/reports:

find /usr/test/reports -maxdepth 0 -type f -name Cost* -mtime +730 -exec rm '{}' '+'; > log.txt

//The above script deletes all the files as per the mentioned criteria and also creates a log for them and stores it in log.txt We can also use the :/ instead of + .The only difference is :/ deletes each file when found but + deletes all the files at once at the end.

//To delete all the files under /usr/test/reports older than 2 years:

find /usr/test/reports -maxdepth 0 -type f -mtime +730 -exec rm {} \;

//To delete all the files under /usr/test/reports older than 2 years(including sub directories):

find /usr/test/reports -type f -mtime +730 -exec rm {} \;

Note:-The statements mentioned after // are the comments for the script.

shell Scripting Routines Click here


No comments:

Post a Comment