xargs命令
来源:互联网 发布:怎么治疗灰指甲知乎 编辑:程序博客网 时间:2024/05/04 03:10
今天在代码中发现类似于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 命令使用 更少的自变量:- 如果积累的命令行长度超出了 由 -s Size 标志指定的字节。
- 最后的迭代有少于 Number(但是非 零)的自变量保留。
注: -L、-I(小写的 L)和 -n 标志是互相排斥的;最后指定的标志生效。
-p询问是否运行 Command 参数。 它显示构造的命令行,后跟一个 ?...(问号和省略号)提示。输入肯定的、特定于语言环境的响应 以运行 Command 参数。 任何其它响应都会引起 xargs 命令 跳过那个特定的参数调用。每个调用都将询问您。 -p 标志同样打开 -t 标志。-s Size设置构造的 Command 行的最大 总大小。Size 参数必须是正整数。如果满足以下条件,则使用更少的自变量:- 自变量的总数超出 -n 标志指定的自变量数。
- 总行数超出 -L 或 -I(小 写 L)标志指定的行数。
- 累积由 Size 参数指定的字节数之前达到 EOF。
退出状态
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$
0 0
- xargs命令
- xargs命令
- xargs 命令
- xargs命令
- xargs 命令
- xargs命令
- xargs命令
- xargs 命令
- xargs命令
- xargs 命令
- xargs 命令
- xargs命令
- xargs 命令
- xargs命令
- xargs命令
- xargs命令
- xargs命令
- xargs 命令
- DELPHI里byte型数组取值与比较
- 关灯游戏
- Cocos2d-x 3.0 打造一个全平台概念目录
- 黑马程序员_C语言_字符串
- apkbuilder找不到了!!!
- xargs命令
- 调用lua函数executeGlobalFunction
- win7下Apache ant的安装与配置
- jsp 自定义标签库
- shell之source exec fork
- xcode 编译openssl (good)
- 图片拖拽布局
- Extjs4自定义Window关闭按钮
- FFPZ傺岸房守涝辟频sishai