Linux命令练习第二关(3)
2016-08-28 20:33
274 查看
(5)已知apache服务的访问日志按天记录在服务器本地目录/app/logs下,由于磁盘空间有限,现在要求只能保留最近7天访问日志!请问如何解决?
脚本创建测试数据:
find -mtime -n +n 按照文件的更改时间来查找文件。
- n表示文件更改时间距现在n天以内,+ n表示文件更改时间距现在n天以前。
-7 表示距现在4/15在7天以内的。
+7 表示距现在4/15在7天以前的。
解答: 保留最近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命令练习第二关(8)
- Linux命令练习第二关(5)
- Linux命令练习第二关(1)
- Linux命令练习第二关(6)
- C语言第二课练习
- 高级编程技术作业第二周2 第四章课后练习
- javascript第二课练习
- 记录——《C Primer Plus (第五版)》第十一章编程练习第二题
- linux基础学习 第二-四单元练习
- 第二单元练习
- 练习:输入5个数字,取最后一个数字加上中间第二到四个数字,最后加上第一个数字,然后用alert显示出来
- 完成编程练习6,但从文件中读取所需的信息。该文件的第一项应为捐款人数,余下的内容应为成对的行。在每一对中,第一行为捐款人姓名,第二行为捐款数额。
- 华为机试练习150817第二题:求后序序列
- JavaScript入门(八) - 第二阶段练习
- 第二练习 第一题
- 记录——《C Primer Plus (第五版)》第十章编程练习第二题
- Android菜鸟练习第二课 ViewPager自动循环加载网络图片
- Linux命令练习第二关(7)
- 第二课的课后练习
- 『LeetCode』练习第二弹_算法3,4题