码迷,mamicode.com
首页 > 系统相关 > 详细

linux delete files older than 3 days

时间:2014-11-05 16:47:22      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:style   http   io   ar   os   for   sp   div   on   

4 down vote accepted

This is easy enough (although note that this goes by a modification time more than 3 days ago since a creation time is only available on certain filesystems with special tools):

find /a/b/c/1 /a/b/c/2 -type f -mtime +3 #-delete

Remove the # before the -delete once you are sure that it is finding the files you want to remove.

To have it run by cron, I would probably just create an executable script (add a shebang - #!bin/sh to the top line of the file and make executable with chmod a+x), then put it in an appropriate cron directory like /etc/cron.daily or /etc/cron.weekly. Provided of course that you do not need a more specific schedule and that these directories exist on your distro.

Update

As noted below, the -delete option for find isn‘t very portable. A POSIX compatible approach would be:

find /a/b/c/1 /a/b/c/2 -type f -mtime +3 #-exec rm {} +

Again remove the # when you are sure you have the right files.

Update2

To quote from Stéphane Chazelas comment below:

Note that -exec rm {} + has race condition vulnerabilities which -delete (where available) doesn‘t have. So don‘t use it on directories that are writeable by others. Some finds also have a -execdir that mitigates against those vulnerabilities.

link:http://unix.stackexchange.com/questions/136804/cron-job-to-delete-files-older-than-3-days

linux delete files older than 3 days

标签:style   http   io   ar   os   for   sp   div   on   

原文地址:http://www.cnblogs.com/rosepotato/p/4076584.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!