• xargs用法


    XARGS用

    1.  manpage手册内容

    xargs - build and execute command lines from standard input

    This manual page documents the GNU version of xargs.  xargs reads items from the standard input, delimited by blanks (which can be protected with double or single quotes or a backslash) or newlines, and executes the command (default is /bin/echo) one or more times with any initial-arguments followed by items read from standard input.  Blank lines on the standard input are ignored.

    Because Unix filenames can contain blanks and newlines, this default behaviour is often problematic; filenames containing blanks and/or newlines are incorrectly processed  by xargs.  In these situations it is better to use the -0 option, which prevents such problems.   When using this option you will need to ensure that the program which produces the input for xargs also uses a null character as a separator.  If that program is GNU find for example, the -print0 option does this for you.

    If  any  invocation  of  the  command  exits with a status of 255, xargs will stop immediately without reading any further input.  An error message is issued on stderr when this happens.

    2.  管道和xargs区别

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

    你可以试试运行:

    代码:
    echo "--help"|cat
    echo "--help"|xargs cat

    参考:http://blog.csdn.net/yongan1006/article/details/8134581

    3.      xargs用法

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

    xargs 是一个强有力的命令,它能够捕获一个命令的输出,然后传递给另外一个命令,下面是一些如何有效使用xargs 的实用例子。

    1. 当你尝试用rm 删除太多的文件,你可能得到一个错误信息:/bin/rm Argument list too long. 用xargs 去避免这个问题

    find ~ -name ‘*.log’ -print0 | xargs -0 rm -f

    2. 获得/etc/ 下所有*.conf 结尾的文件列表,有几种不同的方法能得到相同的结果,下面的例子仅仅是示范怎么实用xargs ,在这个例子中实用 xargs将find 命令的输出传递给ls -l

    # find /etc -name "*.conf" | xargs ls –l

    3. 假如你有一个文件包含了很多你希望下载的URL, 你能够使用xargs 下载所有链接

    # cat url-list.txt | xargs wget –c

    4. 查找所有的jpg 文件,并且压缩它

    # find / -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz

    5. 拷贝所有的图片文件到一个外部的硬盘驱动 

    # ls *.jpg | xargs -n1 -i cp {} /external-hard-drive/directory

    EXAMPLES
    find /tmp -name core -type f -print | xargs /bin/rm -f
    Find files named core in or below the directory /tmp and delete them.  Note that this will work incorrectly if there are any filenames containing newlines or spaces.

    find /tmp -name core -type f -print0 | xargs -0 /bin/rm -f
    Find  files  named core in or below the directory /tmp and delete them, processing filenames in such a way that file or directory names containing spaces or newlines are correctly handled.

    find /tmp -depth -name core -type f -delete
    Find files named core in or below the directory /tmp and delete them, but more efficiently than in the previous example (because we avoid the need to use fork(2) and exec(2) to launch rm and we don't need the extra xargs process).

    cut -d: -f1 < /etc/passwd | sort | xargs echo
    Generates a compact listing of all the users on the system.

    xargs sh -c 'emacs "$@" < /dev/tty' emacs
    Launches the minimum number of copies of Emacs needed, one after the other, to edit the files listed on xargs' standard input.  This example achieves the same effect as BSD's -o option, but in a more flexible and portable way.

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

  • 相关阅读:
    第一冲刺阶段——站立会议第六天4月23日
    第一冲刺阶段——站立会议第五天4月22日
    第一冲刺阶段——站立会议第四天4月21日
    第一冲刺阶段——站立会议第三天4月20日
    第一冲刺阶段——站立会议第二天4月19日
    第一冲刺阶段——站立会议第一天4月18日
    WBS
    团队项目计划
    团队项目——班级派发布视频
    团队简介
  • 原文地址:https://www.cnblogs.com/embedded-linux/p/5586461.html
Copyright © 2020-2023  润新知