免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 2629 | 回复: 10
打印 上一主题 下一主题

[文本处理] find用法 [复制链接]

论坛徽章:
2
2015年亚洲杯之伊拉克
日期:2015-04-13 10:55:1115-16赛季CBA联赛之山西
日期:2016-05-16 09:54:41
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-04-13 11:14 |只看该作者 |倒序浏览
1,/var/log下有很多.log结尾日志,
2,使用find查找 find /var/log -name "*.log" 然后在.log前添加时间戳,
     如:message.log   
     结果:message1428894841.log

该如何实现?

论坛徽章:
28
2015年迎新春徽章
日期:2015-03-04 10:16:53午马
日期:2015-03-20 17:11:07亥猪
日期:2015-03-20 17:11:23戌狗
日期:2015-03-20 17:11:33酉鸡
日期:2015-03-20 17:11:47申猴
日期:2015-03-20 17:11:58未羊
日期:2015-03-20 17:12:10子鼠
日期:2015-03-20 17:12:23丑牛
日期:2015-03-20 17:12:40卯兔
日期:2015-03-20 17:12:51辰龙
日期:2015-03-20 17:13:00巳蛇
日期:2015-03-20 17:13:12
2 [报告]
发表于 2015-04-13 11:55 |只看该作者
居然不会...来个山寨版的先...
  1. for i in `find /var/log  -type f -name '*.log'`;do mv $i  ${i%.*}$(date +%s).log;done
复制代码

论坛徽章:
2
2015年亚洲杯之伊拉克
日期:2015-04-13 10:55:1115-16赛季CBA联赛之山西
日期:2016-05-16 09:54:41
3 [报告]
发表于 2015-04-13 13:18 |只看该作者
这个还要使用for循环啊?有没有一条命令解决的,find+xargs可以实现,忘记怎么操作了

论坛徽章:
28
2015年迎新春徽章
日期:2015-03-04 10:16:53午马
日期:2015-03-20 17:11:07亥猪
日期:2015-03-20 17:11:23戌狗
日期:2015-03-20 17:11:33酉鸡
日期:2015-03-20 17:11:47申猴
日期:2015-03-20 17:11:58未羊
日期:2015-03-20 17:12:10子鼠
日期:2015-03-20 17:12:23丑牛
日期:2015-03-20 17:12:40卯兔
日期:2015-03-20 17:12:51辰龙
日期:2015-03-20 17:13:00巳蛇
日期:2015-03-20 17:13:12
4 [报告]
发表于 2015-04-13 13:22 |只看该作者
  1. find . -type f -name '*.log' -exec mv {} {}.$(date +%s) \;

复制代码
可以,但是时间戳只能放在最前面或者最后面.放在中间的不会...
坐等jason大神.

论坛徽章:
2
2015年亚洲杯之伊拉克
日期:2015-04-13 10:55:1115-16赛季CBA联赛之山西
日期:2016-05-16 09:54:41
5 [报告]
发表于 2015-04-13 13:37 |只看该作者
回复 4# 我是一隻羊


    之间做过一个题目,貌似就是这样的,{}这个是引用执行结果,想起来了,不过引申下,如果时间戳放中间该如何做,等大神。。。。

论坛徽章:
60
20周年集字徽章-20	
日期:2020-10-28 14:04:3015-16赛季CBA联赛之北京
日期:2016-07-06 15:42:0715-16赛季CBA联赛之同曦
日期:2016-06-12 10:38:0915-16赛季CBA联赛之佛山
日期:2016-05-27 11:54:56黄金圣斗士
日期:2015-12-02 11:44:35白银圣斗士
日期:2015-11-25 14:32:43白银圣斗士
日期:2015-11-23 12:53:352015亚冠之布里斯班狮吼
日期:2015-10-21 16:55:482015亚冠之首尔
日期:2015-09-01 16:46:052015亚冠之德黑兰石油
日期:2015-08-31 11:39:192015亚冠之萨济拖拉机
日期:2015-08-28 21:06:5315-16赛季CBA联赛之广东
日期:2016-07-12 14:58:53
6 [报告]
发表于 2015-04-13 14:00 |只看该作者
  1. find /var/log -name "*.log" -exec rename .log  $(date +%s).log {} +
复制代码

论坛徽章:
10
2015年亚洲杯之科威特
日期:2015-04-13 13:51:5315-16赛季CBA联赛之山东
日期:2016-01-16 18:35:0815-16赛季CBA联赛之广东
日期:2016-01-04 15:26:23程序设计版块每日发帖之星
日期:2015-11-13 06:20:002015亚冠之阿尔艾因
日期:2015-11-10 12:32:242015亚冠之阿尔沙巴布
日期:2015-05-15 18:26:49羊年新春福章
日期:2015-04-28 16:45:112015年亚洲杯之伊拉克
日期:2015-04-25 20:06:30羊年新春福章
日期:2015-04-13 13:53:2015-16赛季CBA联赛之八一
日期:2016-01-20 18:23:49
7 [报告]
发表于 2015-04-13 14:03 |只看该作者
reyleon 发表于 2015-04-13 14:00


六婶的真短啊

论坛徽章:
28
2015年迎新春徽章
日期:2015-03-04 10:16:53午马
日期:2015-03-20 17:11:07亥猪
日期:2015-03-20 17:11:23戌狗
日期:2015-03-20 17:11:33酉鸡
日期:2015-03-20 17:11:47申猴
日期:2015-03-20 17:11:58未羊
日期:2015-03-20 17:12:10子鼠
日期:2015-03-20 17:12:23丑牛
日期:2015-03-20 17:12:40卯兔
日期:2015-03-20 17:12:51辰龙
日期:2015-03-20 17:13:00巳蛇
日期:2015-03-20 17:13:12
8 [报告]
发表于 2015-04-13 14:26 |只看该作者
回复 6# reyleon

学习了...弱弱的问下,结尾的+怎么解释呢...求大神指点...
   

论坛徽章:
60
20周年集字徽章-20	
日期:2020-10-28 14:04:3015-16赛季CBA联赛之北京
日期:2016-07-06 15:42:0715-16赛季CBA联赛之同曦
日期:2016-06-12 10:38:0915-16赛季CBA联赛之佛山
日期:2016-05-27 11:54:56黄金圣斗士
日期:2015-12-02 11:44:35白银圣斗士
日期:2015-11-25 14:32:43白银圣斗士
日期:2015-11-23 12:53:352015亚冠之布里斯班狮吼
日期:2015-10-21 16:55:482015亚冠之首尔
日期:2015-09-01 16:46:052015亚冠之德黑兰石油
日期:2015-08-31 11:39:192015亚冠之萨济拖拉机
日期:2015-08-28 21:06:5315-16赛季CBA联赛之广东
日期:2016-07-12 14:58:53
9 [报告]
发表于 2015-04-13 14:44 |只看该作者
回复 8# 我是一隻羊


   
其实与 -exec command {} \; 差不多的, 就是一个变体而已, 相当于

find -name "*.log" | xargs -I{} command {}



具体可以看man page:
  1.        -exec command ;
  2.               Execute command; true if 0 status is returned.  All following arguments to find are taken to be arguments to the command  until
  3.               an  argument consisting of ‘;’ is encountered.  The string ‘{}’ is replaced by the current file name being processed everywhere
  4.               it occurs in the arguments to the command, not just in arguments where it is alone, as in some versions of find.  Both of these
  5.               constructions  might  need  to be escaped (with a ‘\’) or quoted to protect them from expansion by the shell.  See the EXAMPLES
  6.               section for examples of the use of the ‘-exec’ option.  The specified command is run once for each matched file.   The  command
  7.               is  executed  in  the  starting  directory.    There are unavoidable security problems surrounding use of the -exec option; you
  8.               should use the -execdir option instead.

  9.        -exec command {} +
  10.               This variant of the -exec option runs the specified command on the selected files, but the command line is built  by  appending
  11.               each selected file name at the end; the total number of invocations of the command will be much less than the number of matched
  12.               files.  The command line is built in much the same way that xargs builds its command lines.   Only  one  instance  of  ’{}’  is
  13.               allowed within the command.  The command is executed in the starting directory.
复制代码

论坛徽章:
28
2015年迎新春徽章
日期:2015-03-04 10:16:53午马
日期:2015-03-20 17:11:07亥猪
日期:2015-03-20 17:11:23戌狗
日期:2015-03-20 17:11:33酉鸡
日期:2015-03-20 17:11:47申猴
日期:2015-03-20 17:11:58未羊
日期:2015-03-20 17:12:10子鼠
日期:2015-03-20 17:12:23丑牛
日期:2015-03-20 17:12:40卯兔
日期:2015-03-20 17:12:51辰龙
日期:2015-03-20 17:13:00巳蛇
日期:2015-03-20 17:13:12
10 [报告]
发表于 2015-04-13 14:58 |只看该作者
回复 9# reyleon


    多谢,学习了!man page看的还是太少了...
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP