1. Man手册

NAME
xargs - build and execute command lines from standard input

SYNOPSIS
xargs [OPTIONS] [command [initial-arguments]]

DESCRIPTION
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.
The command line for command is built up until it reaches a system-defined limit (unless the -n and -L options are used). The specified command will be invoked as many times as necessary to use up the list of
input items. In general, there will be many fewer invocations of command than there were items in the input. This will normally have significant performance benefits. Some commands can usefully be executed in parallel too; see the -P option.
Because Unix filenames can contain blanks and newlines, this default behaviour is often problematic;
file names 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 example, 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.

OPTIONS
-0, —null
Input items are terminated by a null character instead of by whitespace, and the quotes and back
slash are not special (every character is taken literally). Disables the end of file string, which
is treated like any other argument. Useful when input items might contain white space, quote marks,
or backslashes. The GNU find -print0 option produces input suitable for this mode.

  1. -a file, --arg-file=file<br /> Read items from file instead of standard input. If you use this option, stdin remains unchanged<br /> when commands are run. Otherwise, stdin is redirected from /dev/null.
  2. --delimiter=delim, -d delim<br /> Input items are terminated by the specified character. The specified delimiter may be a single character, a C-style character escape such as \n, or an octal or hexadecimal escape code. <br /> Octal and hexadecimal escape codes are understood as for the printf command. Multibyte <br /> characters are not supported. When processing the input, quotes and backslash are not special; 、<br /> every character in the input is taken literally. The -d option disables any end-of-file string, which is <br /> treated like any other argument. You can use this option when the input consists of simply newline-<br /> separated items, although it is almost always better to design your program to use --null where this <br /> is possible.
  3. -I replace-str<br /> Replace occurrences of replace-str in the initial-arguments with names read from standard input.<br /> Also, unquoted blanks do not terminate input items; instead the separator is the newline character.<br /> Implies -x and -L 1.
  4. -i[replace-str], --replace[=replace-str]<br /> This option is a synonym for -Ireplace-str if replace-str is specified. **If the replace-str argument**<br />** is missing, the effect is the same as -I{}**. This option is deprecated; use -I instead.
  5. -l[max-lines], --max-lines[=max-lines]<br /> Synonym for the -L option. Unlike -L, the max-lines argument is optional. If max-lines is not<br /> specified, it defaults to one. The -l option is deprecated since the POSIX standard specifies -L<br /> instead.
  6. -n max-args, --max-args=max-args<br /> Use at most max-args arguments per command line. Fewer than max-args arguments will be used if<br /> the size (see the -s option) is exceeded, unless the -x option is given, in which case xargs will exit.
  7. -r **If the standard input does not contain any nonblanks, do not run the command**. <br /> Normally, the command is run once even if there is no input.

2. 命令常用选项方式及组合

xargs 多用于将标准输出作用于管道,将经过管道后转换为的标准输入 作为 cmd 的执行参数,进行一次或多次执行。

2.1 基本组合

  • xargs + cmd # 标准一次执行

查询当前目录及以下的所有以”.txt”为尾缀的文件的总行数:

  1. find . -name "*.txt" | xargs wc -l
  2. 3 ./test.txt
  3. 3 ./haha.txt
  4. 6 total
  • xargs -n + cmd # 标准多次执行

将前命令对比以下执行方式

  1. find . -name "*.txt" | xargs -n 1 wc -l
  2. 3 ./test.txt
  3. 3 ./haha.txt

对比易知:
xargs 无 -n 选项时,所有入参将作为一个入参,执行一次 cmd
xargs -n 1 选项时,所有入参将按默认的 空格 为分隔,每1个参数执行一次,作为cmd的执行参数。

  • xargs -I replace-str + cmd # 重新格式化 cmd+参数 的格式

将所有jpg图片拷贝到/data/images目录下

  1. ls *.jpg | xargs -n1 -I {} cp {} /data/images

-I 选项将 { } 作为替换占位符,将经过管道后的 stdin 替换 { } ,作用于 cp 命令

  • 结合 find 对文件操作
  1. # 删除文件
  2. find . -type f -name "*.txt" -print0 | xargs -0 rm -rf
  3. # 统计所有php文件的源代码行数
  4. find . -type f -name "*.php" -print0 | xargs -0 wc -l
  5. # 查找所有的jpg文件并压缩
  6. find . -type f -name "*.jpg" -print | xargs tar -czvf images.tar.gz

3. xargs原理剖析及用法详解

https://my.oschina.net/xway/blog/1532185