linux管道命令xargs参数,xargs总结   管道命令和xargs的区别(经典解释)

1. 简介

之所以能用到这个命令,关键是由于很多命令不支

持|管道来传递参数,而日常工作中有有这个必要,

所以就有了xargs命令,例如:

find /sbin -perm +700 |ls -l       这个命令是

错误的

find /sbin -perm +700 |xargs ls -l   这样才是

正确的

xargs 可以读入 stdin 的资料,并且以空白字元或

断行字元作为分辨,将 stdin 的资料分隔成为

arguments 。 因为是以空白字元作为分隔,所以,

如果有一些档名或者是其他意义的名词内含有空白字

元的时候, xargs 可能就会误判了~他的用法其实

也还满简单的!就来看一看先!

2. 选项解释

-0 当sdtin含有特殊字元时候,将其当成一般字符,

想/'空格等

例如:root@localhost:~/test#echo "//"|xargs

echo

root@localhost:~/test#echo "//"|xargs

-0 echo

/

-a file 从文件中读入作为sdtin,(看例一)

-e flag ,注意有的时候可能会是-E,flag必须是一

个以空格分隔的标志,当xargs分析到含有flag这个

标志的时候就停止。(例二)

-p 当每次执行一个argument的时候询问一次用户。

(例三)

-n num 后面加次数,表示命令在执行的时候一次用

的argument的个数,默认是用所有的。(例四)

-t 表示先打印命令,然后再执行。(例五)

-i 或者是-I,这得看Linux支持了,将xargs的每项

名称,一般是一行一行赋值给{},可以用{}代替。(

例六)

-r no-run-if-empty 当xargs的输入为空的时候则

停止xargs,不用再去执行了。(例七)

-s num 命令行的最好字符数,指的是xargs后面那个

命令的最大命令行字符数。(例八)

-L  num Use at most max-lines nonblank input

lines per command line.-s是含有空格的。

-l  同-L

-d delim 分隔符,默认的xargs分隔符是回车,

argument的分隔符是空格,这里修改的是xargs的分

隔符(例九)

-x exit的意思,主要是配合-s使用。

-P 修改最大的进程数,默认是1,为0时候为as many

as it can ,这个例子我没有想到,应该平时都用不

到的吧。

3. 应用举例

例一:

root@localhost:~/test#cat test

#!/bin/sh

echo "hello world/n"

root@localhost:~/test#xargs -a test echo

#!/bin/sh echo hello world/n

root@localhost:~/test#

例二:

root@localhost:~/test#cat txt

/bin tao shou kun

root@localhost:~/test#cat txt|xargs -E 'shou'

echo

/bin tao

root@localhost:~/test#

例三:

root@localhost:~/test#cat txt|xargs -p echo

echo /bin tao shou kun ff ?...y

/bin tao shou kun ff

例四:

root@localhost:~/test#cat txt|xargs -n1 echo

/bin

tao

shou

kun

root@localhost:~/test3#cat txt|xargs  echo

/bin tao shou kun

例五:

root@localhost:~/test#cat txt|xargs -t echo

echo /bin tao shou kun

/bin tao shou kun

例六:

$ ls | xargs -t -i mv {} {}.bak

例七:

root@localhost:~/test#echo ""|xargs -t mv

mv

mv: missing file operand

Try `mv --help' for more information.

root@localhost:~/test#echo ""|xargs -t -r  mv

root@localhost:~/test#

(直接退出)

例八:

root@localhost:~/test#cat test |xargs -i -x

-s 14 echo "{}"

exp1

exp5

file

xargs: argument line too long

linux-2

root@localhost:~/test#

例九:

root@localhost:~/test#cat txt |xargs -i -p

echo {}

echo /bin tao shou kun ?...y

root@localhost:~/test#cat txt |xargs -i -p -d

" " echo {}

echo /bin ?...y

echo tao ?.../bin

y

echo shou ?...tao

再如:

root@localhost:~/test#cat test |xargs -i -p

-d " " echo {}

echo exp1

exp5

file

linux-2

ngis_post

tao

test

txt

xen-3

?...y

root@localhost:~/test#cat test |xargs -i -p

echo {}

echo exp1 ?...y

echo exp5 ?...exp1

y

echo file ?...exp5

y

一直弄不懂,管道不就是把前一个命令的结果作为参

数给下一个命令吗,那在 | 后面加不加xargs有什么

区别

NewUserFF 写道:

懒蜗牛Gentoo 写道:

管道是实现“将前面的标准输出作为后面的标准输入

xargs是实现“将标准输入作为命令的参数”

你可以试试运行:

代码:

echo "--help"|cat

echo "--help"|xargs cat

看看结果的不同。

[root@maintt ~]# echo "--help"|cat

--help

[root@maintt ~]# echo "--help"|xargs cat

用法:cat [选项]... [文件]...

将[文件]或标准输入组合输出到标准输出。

-A, --show-all           等于-vET

-b, --number-nonblank    对非空输出行编号

-e                       等于-vE

-E, --show-ends          在每行结束处显

示"$"

-n, --number             对输出的所有行编号

-s, --squeeze-blank      不输出多行空行

-t                       与-vT 等价

-T, --show-tabs          将跳格字符显示为^I

-u                       (被忽略)

试过了,依然不是很确定的明白到底是什么意思,自

己再探索一下看看把

如果你直接在命令行输入cat而不输入其余的任何东

西,这时候的cat会等待标准输入,因此你这时候可

通过键盘输入并按回车来让cat读取输入,cat会原样

返回。而如果你输入--help,那么cat程序会在标准

输出上

打印自己的帮助文档。也就是说,管道符 | 所传递

给程序的不是你简单地在程序名后面输入的参数,它

们会被

程序内部的读取功能如scanf和gets等接收,而xargs

则是将内容作为普通的参数传递给程序,相当于你手

写了

cat --help

来自:

http://forum.ubuntu.org.cn/viewtopic.php?

t=354669

补充解释:

在一个目录中有如下三个文件

a.c        b.c            c.c

find   . /   -print命令会打印出三个文件名

find . /    -print | grep a.c  只会打印出a.c

这个文件

如果只输入命令grep a.c

那么你在键盘中只输入a.c字符串时,a.c会被打印两

次,否则只打印你输入的字符

如果要找三个文件中,那个文件包括有hello字符

find ./ -print | xargs grep hello

总结:管道符后不加xargs相当于先将xargs后面的命

令回车执行一下再从键盘里输入

管道符前面命令执行的结果内容

加上xargs 相当于直接从键盘输入管道符前面命令执

行的结果内容再回车

再总结一下,就是回车的先后顺序不太一样。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值