美文网首页
Linux命令xargs命令的使用

Linux命令xargs命令的使用

作者: EnglishMan | 来源:发表于2013-09-17 20:02 被阅读0次

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.

相关文章

  • xargs命令详解

    linux命令之xargs 1、xargs命令简介 xargs命令是给其他命令传递参数的一个过滤器,也是组合多个命...

  • 如何解压目录下的所有zip压缩文件

    使用find xargs命令 xargs命令详解,xargs与管道的区别

  • Linux那点事-xargs命令详解

    Linux xargs 命令 xargs原理剖析及用法详解 1. 为什么需要xargs命令 1.1 管道|的缺陷 ...

  • Linux自带一些命令简单使用

    在linux下解压在win下压缩的zip文件乱码解决 xargs命令的使用,这个命令是linux自带命令也就是说各...

  • Linux命令xargs命令的使用

    xargs是给命令传递参数的一个过滤器,也是组合多个命令的一个工具。它把一个数据流分割为一些足够小的块,以方便过滤...

  • linux命令-xargs使用

    git diff --name-only 67669e07b2f8cb1068cb068693170e61d34...

  • 工具 | Linux 篇 - xargs 命令

    主题:xargs 命令使用 xargs(英文全拼: eXtended ARGuments)是给命令传递参数的一个过...

  • Linux xargs命令使用实例

    写在前面 由于在工作上经常使用mac和linux系统,对于操作命令来说,已经没有什么新鲜感了,但是xargs最近为...

  • Linux xargs 命令

    简介 xargs 是给命令传递参数的一个过滤器,也是组合多个命令的一个工具。 xargs 可以将管道或标准输入(s...

  • Linux:xargs命令

    有些命令只能以命令行参数的形式接受数据,无法通过stdin接受数据流,此时采用命令xargs,将标准输入数据转换成...

网友评论

      本文标题:Linux命令xargs命令的使用

      本文链接:https://www.haomeiwen.com/subject/euxqtttx.html