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

linux 删除指定日期之前的文件

时间:2017-05-30 18:07:41      阅读:341      评论:0      收藏:0      [点我收藏+]

标签:linu   ctime   写入   设置   log   工作   时间   bsp   重启   

两种方法:

1. 在一个目录中保留最近三个月的文件,三个月前的文件自动删除。

find /email/v2_bak -mtime +92 -type f -name *.mail[12] -exec rm -rf {} \;

/email/v1_bak --设置查找的目录;
-mtime +92 --设置时间为91天前;
-type f --设置查找的类型为文件;
-name *.mail[12] --设置文件名称中包含mail1或者mail2;
-exec rm -f --查找完毕后执行删除操作;
    将此命令写入crontab后即可自动完成查找并删除的工作了。
2. 或者用:find . -ctime +40 -type f | xargs rm -rf
 
1、编写清理日志脚本clear.sh
   #!/bin/sh
   find /opt/bak -mtime +6 -name "*.log" -exec rm {} \;
   find /opt/bak -mtime +6 -name "*.dmp" -exec rm {} \;
2、给脚本授权
    chmod +x clear.sh
3、加入定时执行任务
    vi /etc/crontab
    在最后加入:00 2 * * *  root /opt/sh/clear.sh
4、重启crontab服务

    service crond restart

linux 删除指定日期之前的文件

标签:linu   ctime   写入   设置   log   工作   时间   bsp   重启   

原文地址:http://www.cnblogs.com/fashflying/p/6920520.html

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