Chinaunix首页 | 论坛 | 博客
  • 博客访问: 244533
  • 博文数量: 91
  • 博客积分: 2510
  • 博客等级: 少校
  • 技术积分: 1100
  • 用 户 组: 普通用户
  • 注册时间: 2007-10-15 14:35
文章分类

全部博文(91)

文章存档

2008年(91)

我的朋友

分类: LINUX

2008-03-23 22:07:22

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 < /etc/passwd | sort | xargs echo
 Debian-exim backup bin cjl daemon games gnats  irc  list lp mail man news nobody proxy root snmp sshd sync sys uucp www-data xiaowang

从这个命令更可以看出他的输入重定向功能。 

 

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

可以用这个命令来实现。注意:%

 


 
阅读(1511) | 评论(0) | 转发(0) |
0

上一篇:时间管理

下一篇:shell学习之二

给主人留下些什么吧!~~