您的位置:首页 > 运维架构 > Linux

Linux命令练习第二关(3)

2016-08-28 20:33 274 查看
(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


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux 命令