[Linux] xargs 和 管道符的区别

今天刚好遇到需要使用xargs的情况,就来研究一下xargs 和 管道符的区别

举几个例子,下面两个语句执行后的结果是什么呢?

1. zhang$ find . -name "*.properties" | more

--将当前目录下以properties结尾的文件名及路径给more,出来的是文件列表的名称

2. zhang$ find . -name "*.properties" | xargs more

--将当前目录下以properties结尾的文件给more,出来的是所有文件的内容

通过这个例子可以知道,xargs相当于传给后面一个参数,而管道则传给后面命令一个字符串:

例子1,将find出来的内容(假设结果存储为a.txt)传给more,可以分解成以下两个命令:

  zhang$ find . -name "*.properties" > a.txt

  zhang$ more a.txt

例子2,将find出来的文件(假设找到的是a.properties, b.properties, c.properties)传给more,可以分解成以下4个命令:

  zhang$ find . -name "*.properties" #找到三个文件a.properties, b.properties, c.properties

  zhang$ more a.properties

  zhang$ more b.properties

  zhang$ more c.properties

两个执行出来的效果也是完全不一样的

这篇文章(http://blog.csdn.net/yongan1006/article/details/8134581)说的一段话很有道理:

管道是实现“将前面的标准输出作为后面的标准输入”
xargs是实现“将标准输入作为命令的参数”

这两个命令的输出结果清晰的说明了问题:

echo "--help"|cat

echo "--help"|xargs cat

另外参考一个:

http://blog.csdn.net/sunboy_2050/article/details/7303501

 

批量删除文件:

find . -name "*.properties" | xargs sudo -u admin rm -f

转载于:https://www.cnblogs.com/garinzhang/p/linux_xargs_pipeline_difference.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值