zoukankan      html  css  js  c++  java
  • [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

  • 相关阅读:
    为什么股票一买就跌,一卖就涨?终于找到答案了!
    搜集的一些股票讲师的博客
    一位操盘手的临别赠言
    VMware网络连接 桥接、NAt、host-only模式
    我常用的网络测试工具
    linux下性能测试工具netperf使用
    vm10虚拟机安装Mac OS X10.10教程
    ACE_Svc_Handler 通信原理
    mypwd实现
    2019-2020-1 20175307 20175308 20175319 实验五 通讯协议设计
  • 原文地址:https://www.cnblogs.com/garinzhang/p/linux_xargs_pipeline_difference.html
Copyright © 2011-2022 走看看