免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
1234下一页
最近访问板块 发新帖
查看: 5158 | 回复: 34

[文本处理] 求大神帮忙写一个脚本 [复制链接]

论坛徽章:
3
2015元宵节徽章
日期:2015-03-06 15:53:222015小元宵徽章
日期:2015-03-06 15:58:182015年亚洲杯之澳大利亚
日期:2015-03-18 18:01:18
发表于 2015-03-05 13:41 |显示全部楼层
/home/www/logs/nginx/alllogs下都是nginx的日志,我现在想打包7天前含有aaa字段的日志,之前用的是find -mtime,好像不行,有大神帮忙吗

论坛徽章:
7
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:57:092015小元宵徽章
日期:2015-03-06 15:58:18程序设计版块每日发帖之星
日期:2015-08-09 06:20:00每日论坛发贴之星
日期:2015-08-09 06:20:00程序设计版块每日发帖之星
日期:2015-08-22 06:20:00程序设计版块每日发帖之星
日期:2015-08-27 06:20:00
发表于 2015-03-05 13:43 |显示全部楼层
       -mtime n
              File's data was last modified n*24 hours ago.  See the comments for -atime to understand how rounding affects the  interpretation  of  file
              modification times.

把你全部命令和输出贴一下吧,不然无法判断呀

论坛徽章:
3
2015元宵节徽章
日期:2015-03-06 15:53:222015小元宵徽章
日期:2015-03-06 15:58:182015年亚洲杯之澳大利亚
日期:2015-03-18 18:01:18
发表于 2015-03-05 13:49 |显示全部楼层
回复 2# tuyajie


    #!bin/bash
        cd /home/www/logs/nginx/alllogs

        i=$(date +%y%m%d)

      find /home/www/logs/nginx/alllogs -mtime 7 -name '*aaa*' -exec tar -uvf logs$i.tar {} \;
      find /home/www/logs/nginx/alllogs -mtime 7 -name '*aaa*' -exec rm -rf {} \;

论坛徽章:
33
ChinaUnix元老
日期:2015-02-02 08:55:39CU十四周年纪念徽章
日期:2019-08-20 08:30:3720周年集字徽章-周	
日期:2020-10-28 14:13:3020周年集字徽章-20	
日期:2020-10-28 14:04:3019周年集字徽章-CU
日期:2019-09-08 23:26:2519周年集字徽章-19
日期:2019-08-27 13:31:262016科比退役纪念章
日期:2022-04-24 14:33:24
发表于 2015-03-05 13:52 |显示全部楼层
回复 3# 王子爱李娜


    文件名含aaa还是文件内容含aaa

论坛徽章:
3
2015元宵节徽章
日期:2015-03-06 15:53:222015小元宵徽章
日期:2015-03-06 15:58:182015年亚洲杯之澳大利亚
日期:2015-03-18 18:01:18
发表于 2015-03-05 13:52 |显示全部楼层
回复 4# Shell_HAT


    文件名

论坛徽章:
7
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:57:092015小元宵徽章
日期:2015-03-06 15:58:18程序设计版块每日发帖之星
日期:2015-08-09 06:20:00每日论坛发贴之星
日期:2015-08-09 06:20:00程序设计版块每日发帖之星
日期:2015-08-22 06:20:00程序设计版块每日发帖之星
日期:2015-08-27 06:20:00
发表于 2015-03-05 13:53 |显示全部楼层
本帖最后由 tuyajie 于 2015-03-05 13:58 编辑

-mtime -7
try tihs


more details:

find . -mtime -1  # find files modified less than 1 day ago
find . -mtime 1   # find files modified between 24 and 48 hours ago
find . -mtime +1  # find files modified more than 48 hours ago

论坛徽章:
33
ChinaUnix元老
日期:2015-02-02 08:55:39CU十四周年纪念徽章
日期:2019-08-20 08:30:3720周年集字徽章-周	
日期:2020-10-28 14:13:3020周年集字徽章-20	
日期:2020-10-28 14:04:3019周年集字徽章-CU
日期:2019-09-08 23:26:2519周年集字徽章-19
日期:2019-08-27 13:31:262016科比退役纪念章
日期:2022-04-24 14:33:24
发表于 2015-03-05 13:55 |显示全部楼层
回复 3# 王子爱李娜


如果对语法不熟的话,可以用这个在线生成命令:
http://find.unixpin.com/find/index.html

论坛徽章:
3
2015元宵节徽章
日期:2015-03-06 15:53:222015小元宵徽章
日期:2015-03-06 15:58:182015年亚洲杯之澳大利亚
日期:2015-03-18 18:01:18
发表于 2015-03-05 13:58 |显示全部楼层
回复 6# tuyajie


    我这个脚本贴的有问题,我使用的就是-mtime +7,我在虚拟机上是这么测试的,三天前我在桌面上的目录里touch了几个文件,想用find -mtime +2把文件找出来打包并删除,可是就是找不出来

#!/bin/bash
     cd /root/Desktop/ceshi

        i=$(date +%y%m%d)

        find . -mtime +2 -name '*aaa*' -exec tar -uvf logs$i.tar {} \;
        find . -mtime +2 -name '*aaa*' -exec rm -rf {} \;

论坛徽章:
7
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:57:092015小元宵徽章
日期:2015-03-06 15:58:18程序设计版块每日发帖之星
日期:2015-08-09 06:20:00每日论坛发贴之星
日期:2015-08-09 06:20:00程序设计版块每日发帖之星
日期:2015-08-22 06:20:00程序设计版块每日发帖之星
日期:2015-08-27 06:20:00
发表于 2015-03-05 14:00 |显示全部楼层
As you probably know, the find command can run for minutes or hours depending on the size of the filesystem being searched. The find command makes a note of its own start time. It then looks at a file's mtime and computes how many seconds ago the file was modified. By dividing the seconds by 86,400 (and discarding any remainder), it can calculate the file's age in days:
Code:

0 days in seconds:       0  -   86399
1 day in seconds:    86400  -  172799
2 days in seconds:  172800  -  259159


So now that we know how many days ago a file was modified, we can use stuff like "-mtime 2" which specifies files that are 172800 to 259159 seconds older than the instant that the find command was started.

"-mtime -2" means files that are less than 2 days old, such as a file that is 0 or 1 days old.

"-mtime +2" means files that are more than 2 days old... {3, 4, 5, ...}

It may seem odd, but +0 is supposed to work and would mean files more than 0 days old. It is very important to recognize that find's concept of a "day" has nothing to do with midnight.

that may be the reason. adjust you number and try again.  

论坛徽章:
33
ChinaUnix元老
日期:2015-02-02 08:55:39CU十四周年纪念徽章
日期:2019-08-20 08:30:3720周年集字徽章-周	
日期:2020-10-28 14:13:3020周年集字徽章-20	
日期:2020-10-28 14:04:3019周年集字徽章-CU
日期:2019-09-08 23:26:2519周年集字徽章-19
日期:2019-08-27 13:31:262016科比退役纪念章
日期:2022-04-24 14:33:24
发表于 2015-03-05 14:00 |显示全部楼层
cd /root/Desktop/ceshi
ls -l
find . -mtime +2 -name '*aaa*'
手工执行以上三个命令,结果发出来。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP