2019独角兽企业重金招聘Python工程师标准>>>

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.

转载于:https://my.oschina.net/huangsz/blog/162069

Linux下xargs命令的使用相关推荐

  1. linux 命令xargs,Linux下xargs命令详解

    Linux下xargs命令详解 1. 简介   之所以能用到这个命令,关键是由于很多命令不支持|管道来传递参数,而日常工作中有有这个必要,所以就有了xargs命令,例如: find /sbin -pe ...

  2. linux下xargs命令用法详解

    From:http://biancheng.dnbcw.info/shell/268829.html xargs在 linux中是个很有用的命令,它经常和其他命令组合起来使用,非常的灵活. xargs ...

  3. linux中which命令详解,Linux下which命令使用详解(转)

    我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索: which 查看可执行文件的位置. whereis 查看文件的位置. locate 配合数据库查看文件位置. f ...

  4. linux下find命令用法

    一.Linux中find常见用法示例 ·find    path    -option    [    -print ]    [ -exec    -ok    command ]    {} /; ...

  5. Linux下查找命令

    转载自  Linux下查找命令 一.Linux查找文件的相关命令 常 用 命 令 简要中文说明 程序所在目录 more 分页显示一个文件或任何输出结果 /bin less 分页显示一个文件并且可以回头 ...

  6. Linux下 ls 命令的高级用法8例

    Linux下 ls 命令的高级用法8例 在Linux下,ls这个命令大家肯定太熟悉了,良许相信只要是Linux工程师,每天都会离不开这个命令,而且一天会使用个几百次.但是,除了 ls -l 以外,你还 ...

  7. linux find ok用法,linux下find命令用法

    一.Linux中find常見用法示例·find    path    -option    [    -print ]    [ -exec    -ok    command ]    {} /; ...

  8. Linux下addr2line命令用法

    Linux下addr2line命令用于将程序指令地址转换为所对应的函数名.以及函数所在的源文件名和行号.当含有调试信息(-g)的执行程序出现crash时(core dumped),可使用addr2li ...

  9. Linux查看WAS的jvm信息,linux 下使用命令查看jvm信息

    java程序员除了编写业务代码之外,特别是项目上线之后,更需要关注的是系统的性能表现,这个时候就需要了解一下jvm的性能表现了,可以借助于java虚拟机自带的一些分析工具,主要有三个常用的命令. 1. ...

最新文章

  1. Windows 公共控件库研究
  2. 操作系统基本分段存储管理方式
  3. GC之7大垃圾收集器详解(上)
  4. java httpcomponents_java – 如何使用Apache httpcomponents从NHttpRequ...
  5. 后台传值给前台p标签
  6. 机器学习基石笔记9——机器可以怎样学习(1)
  7. 基于simulink的Active anti-islanding-AFD主动反孤岛模型仿真
  8. R 回归 虚拟变量na_互助问答第30期:工具变量、GARCH模型操作和多项选择效信度...
  9. DWF低代码开发技术及其在数字化运营和运维平台建设中的应用
  10. 水冷计算机配置单,新手水冷电脑组装的详细图文教程
  11. 细讲如何解决Idear中使用@Test时提示Junit不存在问题
  12. uni-app ios 添加测试新设备,只需下载描述文件
  13. Win10 上切换至Administrator用户
  14. 用 django 构建翻译网站——软件工程课程设计
  15. Ethercat学习-从站源码移植
  16. MIFI与随身wifi、wifi共享软件,玩坏wifi的几种方法
  17. IIS W3C 字段 解释
  18. android地鼠游戏,非常地鼠手游
  19. 机顶盒联机调试的方法
  20. Excel数据分析:美国牛油果销售分析

热门文章

  1. Sentinel熔断限流器工作原理
  2. JVM优化系列-详解常用的虚拟机调优参数
  3. 假如给Go语言加上注解,程序会变怎样?
  4. 《Go语言程序设计》读书笔记(七) 包
  5. mysql使用二进制日志恢复数据
  6. java手动分页工具类
  7. mongod启动问题
  8. 服务端架构设计及功能说明-续1
  9. TP3.2项目 MySQL5.7报错1055 group by新特性
  10. Objective-C Property 和 Synthesize