xargs命令实例分析

Man page 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从标准输入读取由空格或者换行符分隔的条目,并将其作为xargs后面所跟命令参数的一部分(默认使用/bin/echo,所接的命令可以带参数),然后执行命令。

 

常用参数:

-f file 从文件中读取条目而不是从标准输入;

-0 读取的条目以null作为结束,而不是以空格或换行符进行分隔;

-d delim 指定分隔符;

--verbose 执行命令前显示所执行的命令;

-n max-args 指定参数个数

 

1xargs  –verbose       #从标准输入读,使用-f可从文件读参数

输入:hello world (Ctrl + D)

输出:/bin/echo hello world   #--verbose显示的执行命令

hello world

 

2ls /home/ydzhang/xargs | xargs rm –rf  #从管道读

输入:/home/ydzhang/xargs目录下的条目

输出:rm –rf a b c a,b,c/home/ydzhang/xargs下的文件)

 

3find /home/ydzhang | xargs grep “hello” #从管道读

输入:/home/ydzhang下的所有文件名

输出:/home/ydzhang目录下所有包含hello的文件对应的行

 

4find /home/ydzhang –print0 | xargs -0 grep “hello” #从管道读

输入:以null作为字符串结束符的文件名序列(-print0),将这些参数传给xargs时,如果使用默认参数,xargs将不能正确解析,必须告诉xargs这个信息,即使用-0参数。

输出:/home/ydzhang目录下所有包含hello的文件对应的行

 

posted @ 2013-04-19 14:08  ydzhang  阅读(282)  评论(0编辑  收藏  举报