search

Loading

Saturday, November 23, 2013

How to delete file names with spaces older than 30 days in linux?

I want to delete files which are modified before 30 days from a public folder (uploads) in my linux server. The files are uploaded by different users so it includes white spaces and other characters in the file name. How can I automate this process?

You can use any of the below commands to achieve this:


find /var/www/uploads/ -type f -mtime +30 -exec rm -f {} \;

find /var/www/uploads/ -type f -mtime +30 -delete


Both these commands will do the job for you. Then you can add it to cron job to automate this process.

While using this command, you need to be careful and take necessary precautions.There may be other system files which has not been changed during 30 days in the server. So you need to make sure that these commands will affect only a particular folder. I have purposefully omitted removing directories in the above command.

If you want to remove directories/folders also, you can omit "-type f" option. If you are using my first command, you need to change "rm -f" to "rm -rf" to remove folders. If you are using my second command, you don't need to make any changes except omitting the "-type f" option.

Also you can make necessary changes to achieve other conditions also. You may use -atime for access time, -30 for reversing the days condition (not older than 30 days) etc. Make sure that you don't use system directories to run the find command.


No comments :

Post a Comment