今天在代码中发现类似于ls . | xargs rm -f这样形式的命令。下面来总结一下xargs这个命令。
man xargs后:
NAME: 发现xargs - build and execute command lines from standard input(大概的意思是说:xargs这个命令利用从标准输入的内容构建并执行命令行)
SYNOPSIS:
xargs [-0prtx] [-E eof-str] [-e[eof-str]] [--eof[=eof-str]] [--null] [-d delimiter] [--delimiterdelimiter] [-I replace-str] [-i[replace-str]] [--replace[=replace-str]] [-l[max-lines]] [-L max-lines][--max-lines[=max-lines]] [-n max-args] [--max-args=max-args] [-s max-chars] [--max-chars=max-chars] [-Pmax-procs] [--max-procs=max-procs] [--interactive] [--verbose] [--exit] [--no-run-if-empty][--arg-file=file] [--show-limits] [--version] [--help] [ command [initial-arguments]]这个很长,可以简化成 xargs [-option] command [-option]
DESCRIPTION
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命令的帮助手册是GNU版本的(那应该还有其他版本的)。xargs从标准输入读由空格分隔的数据,然后执行一次或多次在SYNOPSIS在指定的command(默认情况是echo命令)、option和从标准输入的数据,其实就是执行下面命令: command [-option] items from stdin)
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 example, the -print0 option does this for you.(大致意思:Unix的文件名中可以包括空白符或换行符,这样经常是有问题的,以至于不能被xargs正常处理。在这种情况,使用-0选项可以解决这样的问题。但使用-0选项时,要保证为xargs产生输入的命令(即管道前面的命令)使用null字符作为分隔符。如果这个(即管道前面的命令)是find命令,使用-print0代替-0)
If any invocation of the command exits with a status of 255, xargs will stop immediately without readingany further input. An error message is issued on stderr when this happens.(大致意思:如果调用command命令以状态255退出时,xargs将立即停止从stdin读数据,并且error被输出到stderr)
选项
-e[EOFString] | 废弃的标志。请使用 -E 标志。
将 EOFString 参数用作逻辑 EOF 字符串。如果不指定 -e 或 -E 标志, 则假定下划线(_)为逻辑 EOF 字符串。如果不指定 EOFString 参数,则禁用逻辑 EOF 字符串 能力,且下划线按照字面含义使用。xargs 命令读取标准输入直到达到 EOF 或指定的字符串。
|
-E EOFString | 指定逻辑 EOF 字符串以替换缺省的下划线(_)。 xargs 命令读取标准输入直到达到 EOF 或指定的字符串。 |
-i[ReplaceString] | 废弃的标志。请使用 -I(大写 i)标志。
如果没有指定 ReplaceString 参数, 则使用字符串 "{}"。
注:-I(大写 i)和 -i 标志是互相排斥的;最后指定的标志生效。 |
-I ReplaceString | (大写 i)。插入标准输入的每一行作为 Command 参数的自变量,把它插入每个发生 ReplaceString 的 Argument 中。ReplaceString 不能在超过 5 个自变量中使用。 在每个标准输入行开始的空字符被忽略。 每个 Argument 能包含一个或多个 ReplaceString,但不能大于 255 字节。-I 标志同样打开 -x 标志。注:-I(大写 i)和 -i 标志是互相排斥的;最后指定的标志生效。 |
-l[Number] | (小写的 L)。废弃的标志。请使用 -L 标志。
如果没有指定 Number 参数,使用缺省值 1。-l 标志同样打开 -x 标志。
注: -L、-I(小写的 L)和 -n 标志是互相排斥的;最后指定的标志生效。 |
-L Number | 用从标准输入读取的指定行数的非空参数运行 Command 命令。如果保留少于指定的 Number,Command 参数 的最后调用可以有少数几个参数行。行以第一个换行字符结束,除非该行的最后一个字符是一个空格 或制表符。后续的空格表示延续至下一个非空行。注: -L、-I(小写的 L)和 -n 标志是互相排斥的;最后指定的标志生效。 |
-n Number | 运行 Command 参数,且使用尽可能多的标准输入自变量,直到 Number 参数指定的最大值。如果满足以下条件,则 xargs 命令使用 更少的自变量:
注: -L、-I(小写的 L)和 -n 标志是互相排斥的;最后指定的标志生效。
|
-p | 询问是否运行 Command 参数。 它显示构造的命令行,后跟一个 ?...(问号和省略号)提示。输入肯定的、特定于语言环境的响应 以运行 Command 参数。 任何其它响应都会引起 xargs 命令 跳过那个特定的参数调用。每个调用都将询问您。 -p 标志同样打开 -t 标志。 |
-s Size | 设置构造的 Command 行的最大 总大小。Size 参数必须是正整数。如果满足以下条件,则使用更少的自变量:
|
-t | 启用跟踪方式,并在运行之前将构造的 Command 行回送到标准错误。 |
-x | 如果有任何 Command 行大于 -s Size 标志指定的字节数,停止运行 xargs 命令。如果指定 -I(大写 i)或 -l(小写 L)标志,则打开 -x 标志。如果 没有指定 -i、-I(大写 i)、-l(小写 L)、-L 或 -n 标志,则 Command 行的总长度必须 在 -s Size 标志指定的限制内。 |
退出状态
0
|
所有 Command 参数的调用 都返回退出状态 0。
|
1-125
|
不能汇编满足指定需求的命令行, 一个或多个 Command 参数的调用 返回一个非零的退出状态,或发生一些其它的错误。
|
126
|
Command 已找到但不能被调用。
|
127
|
找不到 Command。
|
如果不能汇编满足指定需求的命令行, 则不能调用这个命令,命令的调用被一个信号终止,或以退出状态 255 退出。xargs 命令将写一条诊断消息并 退出而不处理任何保留的输入。
例子1:
iubuntu@ubuntu:~$ cd test
iubuntu@ubuntu:~/test$ ls
test test1 trTest variables.sh
iubuntu@ubuntu:~/test$ ls . | xargs rm -f
iubuntu@ubuntu:~/test$ ls
iubuntu@ubuntu:~/test$
iubuntu@ubuntu:~/test$ ls
test test1 trTest variables.sh
iubuntu@ubuntu:~/test$ ls . | xargs rm -f
iubuntu@ubuntu:~/test$ ls
iubuntu@ubuntu:~/test$
通过上面的例子,可以看出ls . | xargs rm -f就等价与rm -f test test1 trTest variables.sh
例子2:
iubuntu@ubuntu:~/test$ cp ../example/test .
iubuntu@ubuntu:~/test$ ls
test
iubuntu@ubuntu:~/test$ more test
ttt.tgz abc
def ghi
xyz
iubuntu@ubuntu:~/test$ more test | xargs echo
ttt.tgz abc def ghi xyz
iubuntu@ubuntu:~/test$ more test | xargs -t echo
echo ttt.tgz abc def ghi xyz
ttt.tgz abc def ghi xyz
iubuntu@ubuntu:~/test$ more test | xargs -t -n 1 echo # -t就是显示详细的命令, -n就是一次从stdin读n行
echo ttt.tgz
ttt.tgz
echo abc
abc
echo def
def
echo ghi
ghi
echo xyz
xyz
iubuntu@ubuntu:~/test$
iubuntu@ubuntu:~/test$ ls
test
iubuntu@ubuntu:~/test$ more test
ttt.tgz abc
def ghi
xyz
iubuntu@ubuntu:~/test$ more test | xargs echo
ttt.tgz abc def ghi xyz
iubuntu@ubuntu:~/test$ more test | xargs -t echo
echo ttt.tgz abc def ghi xyz
ttt.tgz abc def ghi xyz
iubuntu@ubuntu:~/test$ more test | xargs -t -n 1 echo # -t就是显示详细的命令, -n就是一次从stdin读n行
echo ttt.tgz
ttt.tgz
echo abc
abc
echo def
def
echo ghi
ghi
echo xyz
xyz
iubuntu@ubuntu:~/test$
例子3:备份
iubuntu@ubuntu:~/test$ ls
test test1 test2 test3
iubuntu@ubuntu:~/test$ ls . | xargs -i -t cp ./{} ./{}.bak
cp ./test ./test.bak
cp ./test1 ./test1.bak
cp ./test2 ./test2.bak
cp ./test3 ./test3.bak
iubuntu@ubuntu:~/test$ ls
test test1 test1.bak test2 test2.bak test3 test3.bak test.bak
iubuntu@ubuntu:~/test$
test test1 test2 test3
iubuntu@ubuntu:~/test$ ls . | xargs -i -t cp ./{} ./{}.bak
cp ./test ./test.bak
cp ./test1 ./test1.bak
cp ./test2 ./test2.bak
cp ./test3 ./test3.bak
iubuntu@ubuntu:~/test$ ls
test test1 test1.bak test2 test2.bak test3 test3.bak test.bak
iubuntu@ubuntu:~/test$