Delete files older than X days.

Hi All,

I am using below code to delete files older than 2 days. In case if there are no files, I should log an error saying no files to delete.

Please let me know, How I can achive this.

 
find /path/*.xml -mtime +2 

Thanks and Regards
Nagaraja.

if [ "`find -mtime +2`" = "" ];then
echo "NOFILE TO DELETE"
fi

1 Like

Just a reminder:

Your command may fail with "argument list too long" error if there is a lot of files in the directory.

A more fail safe way is:

find "/path" -maxdepth 1 -type f -name "*.xml" -mtime +2
1 Like

Thanks both of you..!!