Linux命令练习第二关(3)

(5)已知apache服务的访问日志按天记录在服务器本地目录/app/logs下,由于磁盘空间有限,现在要求只能保留最近7天访问日志!请问如何解决?

脚本创建测试数据:

for n in `seq 14`
do
  date -s "2016/04/$n"
  touch access_www_`(date +%F)`.log
done
date -s "2016/04/15"

这里写图片描述

这里写图片描述

find -mtime -n +n 按照文件的更改时间来查找文件。
- n表示文件更改时间距现在n天以内,+ n表示文件更改时间距现在n天以前。

[root@ianLinux iantest]# date
Fri Apr 15 00:00:40 CST 2016
[root@ianLinux iantest]# find ./ -type f -name  "*.log" -mtime 7
./access_www_2016-04-08.log

-7 表示距现在4/15在7天以内的。

[root@ianLinux iantest]# find ./ -type f -name  "*.log" -mtime -7
./access_www_2016-04-13.log
./access_www_2016-04-11.log
./access_www_2016-04-09.log
./access_www_2016-04-12.log
./access_www_2016-04-14.log
./access_www_2016-04-10.log

+7 表示距现在4/15在7天以前的。

[root@ianLinux iantest]# find ./ -type f -name  "*.log" -mtime +7 
./access_www_2016-04-01.log
./access_www_2016-04-06.log
./access_www_2016-04-05.log
./access_www_2016-04-07.log
./access_www_2016-04-04.log
./access_www_2016-04-03.log
./access_www_2016-04-02.log

这里写图片描述

解答: 保留最近7天访问日志

[root@ianLinux iantest]# find ./ -type f -name "*.log" -mtime +7|xargs rm -f

这里写图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值