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

  • 相关阅读:
    Codechef Observing the Tree
    bzoj 1367: [Baltic2004]sequence
    bzoj 2375: 疯狂的涂色
    bzoj 1455: 罗马游戏
    codevs 1029 遍历问题
    HNOI2004 宠物收养所 (Treap)
    [ZJOI2007] 报表统计
    bzoj 3261: 最大异或和 (可持久化trie树)
    codevs 1001 舒适的路线
    Codechef Dynamic Trees and Queries
  • 原文地址:https://www.cnblogs.com/garinzhang/p/linux_xargs_pipeline_difference.html
Copyright © 2011-2022 走看看