免费注册 查看新帖 |

Chinaunix

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

find和xargs以及grep的合作 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-03-23 22:07 |只看该作者 |倒序浏览
xargs - build and execute command lines from standard input
This manual page documents the GNU version of xargs.  xargs reads items from
       the standard input, delimited by blanks (which can be protected with  double
       or  single  quotes  or  a  backslash)  or newlines, and executes the command
       (default is /bin/echo) one or more times with any initial-arguments followed
       by  items  read  from standard input.  Blank lines on the standard input are
       ignored.
    默认情况下:xargs是忽略了空格和线这些文件名。让查找跟准确。
Because Unix  filenames  can  contain  blanks  and  newlines,  this  default
       behaviour  is often problematic; filenames containing blanks and/or newlines
       are incorrectly processed by xargs.  In these situations it is better to use
       the  `-0' option, which prevents such problems.   When using this option you
       will need to ensure that the program which produces the input for xargs also
       uses a null character as a separator.  If that program is GNU find for exam-
       ple, the `-print0' option does this for you.
       If any invocation of the command exits with a status of 255, xargs will stop
       immediately  without  reading any further input.  An error message is issued
       on stderr when this happens.

也就是说查找的文件中如果含有空格和线,者xargs会出现报错。当加上-o后,他就可以识别。

find /tmp -name core -type f -print | xargs /bin/rm -f
       Find files named core in or below the directory /tmp and delete them.  Note that this will work incorrectly if there  are
       any filenames containing newlines or spaces.
       find /tmp -name core -type f -print0 | xargs -0 /bin/rm -f
       Find  files  named  core  in or below the directory /tmp and delete them, processing filenames in such a way that file or
       directory names containing spaces or newlines are correctly handled.
注意,向find /tmp -name core -type f -print | xargs /bin/rm –f如果改成:

xargs 的作用是输入重定向。
Find /tmp –name core –type f –print |rm –f 则不会起作用。
cut -d: -f1 sudo find /home/liangyin/mrtgbackup/ -type f  -print|xargs grep -rl "10.163.28.3.*"


/home/liangyin/mrtgbackup/b/mrtg/10.163.28.3_146800777.html
/home/liangyin/mrtgbackup/b/mrtg/10.163.28.3_146800785.html
/home/liangyin/mrtgbackup/b/mrtg/10.163.28.3_146800793.html
/home/liangyin/mrtgbackup/b/mrtg/10.163.28.3_146800801.html
/home/liangyin/mrtgbackup/b/mrtg/10.163.28.3_146800809.html
/home/liangyin/mrtgbackup/b/mrtg/10.163.28.3_146800817.html


-R, -r, --recursive
              Read all files under each directory, recursively; this is equivalent  to  the  -d  recurse
              option.

-l, --files-with-matches
              Suppress normal output; instead print the name of each input file from which output  would
              normally have been printed.  The scanning will stop on the first match.

-l 的意思找到内容后输出的是文件的形式,不是文件的内容。

而grep 默认情况下:输出的是文件的内容。




find -type f -name '*.png' | xargs -n1  | while read ABC; do echo $ABC && mv $ABC ${ABC%.png}.gif ; done

这里-n 意思是一个传N个选项给命令。这样查找更准确。

find -type f -name '*.png' |while read ABC; do mv  $ABC ${ABC%.png}.gif;done
可以用这个命令来实现。注意:%






本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/50921/showart_505630.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP