xargs命令

一、官方解读

 The xargs utility(功效,此处翻译为执行程序) reads space, tab, newline and end-of-file delimited strings from the standard input and executes utility with the strings as arguments. 
Any arguments specified on the command line are given to utility upon each invocation, followed by some number of the arguments read from the standard input of xargs.
The utility is repeatedly executed until standard input is exhausted(完结).

总结来说:

(1)xargs从stdio读取以空格、tab、换行、文件开头、文件终结的字符串组

  换言之,xargs会使用类似于java中的String.split方法截断空格、tab、换行、文件开头、文件终结把返回内容变成String[],然后来个for循环将String[]的内容作为参数用于执行后面的命令

  例如:

    find . -name "*.xml" | xargs grep project

    ps:

      find . -name "*.xml"会按行返回当前目录下所有xml文件

      Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。

    

    那么用此命令的执行类似于这个情况

    String filePaths = find . -name "*.xml";

    String[] arrFilePath = filePaths.split("\r\n");

    for(String filePath:arrFilePath){

      filePath grep project;//在文件filePath中查找包含project关键字的内容

    }

        

常用的xargs命令

#以arg.txt文件内容作为脚本参数执行传入
cat arg.txt | xargs -I {} ./sk.sh -p {} -l

./sk.sh -p aaa -l
./sk.sh -p bbb -l
./sk.sh -p ccc -l
#将所有某类型文件拷贝至某文件夹下
ls *.jpg | xargs -n1 -I cp {} /data/images
#统计系统内php文件数量
find . -type f -name "*.php" -print0 | xargs -0 wc -l

 

参见网站: 

  http://man.linuxde.net/xargs

posted @ 2018-08-13 09:02  chendeming  阅读(162)  评论(0编辑  收藏  举报