xargs命令详解

xargs是给命令传递参数的一个过滤器,也是组合多个命令的一个工具。它把一个数据流分割为一些足够小的块,以方便过滤器和命令行进行处理。通常情况下,xargs从管道或者stdin中读取数据,但是它也能够从文件的输出中读取数据。xagrs的默认命令是echo,这意味着通过管道传递给xagrs的输入将会包含换行和空白,不过,通过args的处理,换行和空白将被空格取代。

参数解释:
-0:当stdin含有特殊字元时候,将其当成一般字符,例如
$ echo "// "|xargs echo
//
$ echo "// "|xargs -0 echo
//

-a file:从文件中读入作为stdin
$ xargs -a hello.txt
hello world

-e flag:注意有的时候可能会是-E,flag必须是一个以空格分隔的标志,当xargs分析到含有flag这个标志的时候就停止。
$ cat hello.txt|xargs -e 'world'
hello
$ xargs -E 'world' -a hello.txt
hello

-n num:后面加次数,表示命令在执行的时候一次用的argument的个数,默认是用所有的。
$cat hello.txt |xargs -n 2
hello world

-p:操作具有可交互性,每次执行command都交互式提示用户选择,当每次执行一个argument的时候访问一次用户
$ cat hello.txt |xargs -p
echo hello world ?...y
hello world

-t:先打印命令,然后再执行
$ cat hello.txt |xargs -t

-i: 将xargs的每项名称,一般是一行一行赋值给{},可以用{}代替
$ cat hello.txt|xargs -t -i sed "s/{}/{}aa/g" hello.txt
sed s/hello world/hello worldaa/g hello.txt
hello worldaa

-I:等同于-i。
-L max-lines:每次读取max-line行输入交由xargs处理
$ cat test.txt|xargs -L 1
hello tom
hello marry
hello jack
$ cat test.txt|xargs -L 2
hello tom hello marry
hello jack

-l:类似于-L,区别在于-l可以不指定参数,默认为1

-r: 无输入则停止执行,默认至少执行1次
$ echo ""|xargs -t mvmv mv: missing file operandTry `mv --help` for more information.$ echo ""|xargs -t -r mv #直接退出

-s max-chars: xargs每次执行命令的最大长度(含空格)
$ cat test.txt|xargs -s 12
hello
tom
hello
marry
hello
jack
$ cat test.txt|xargs -s 15
hello tom
hello
marry
hello
jack

-x: 当 xargs 执行的命令长度大于 -s max-char 时,停止执行
$ xargs -x -s 10 -a hello.txt
xargs: argument line too long
$ xargs -x -s 12 -a hello.txt
hello
world

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值