Setting Cron job to delete file after 24 hours

爱⌒轻易说出口 提交于 2019-12-03 08:34:12

I use this in a shell script...

find /some/path -mtime +7 -exec rm {} \; # delete > 7 days old

If you have access to your Server or SSH, you can simply add it to your crontab.

In your SSH just type

crontab -e

you will see a list of cron jobs on it, just append this line of code to your cronjob:

0 10 * * * rm -rf /var/www/example.com/public/js/compiled/*

The code above means that every 10am in the morning you are removing all the files in the path you provide. Please refer to this link for more info about Cron: http://en.wikipedia.org/wiki/Cron

it worked for me to delete in once a day

0 0 * * * rm -rf /home/user/public_html/folder

if you want to remove everything in this folder, but leave the folder itself:

0 0 * * * rm -f /home/user/public_html/folder/*

To optimize MrCleanX' solution a bit, use xargs:

find /some/path -type f -mtime +7 -print0 | xargs -0 --no-run-if-empty rm

Instead of calling rm for each file to delete, xargs packs many files together to a single call to rm

The -print0 and -0 stuff is to make both find and xargs using NULL terminated strings, which is necessary to handle file names with space and other interesting chars in their names.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!