博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
linux shell脚本学习xargs命令使用详解
阅读量:4159 次
发布时间:2019-05-26

本文共 1252 字,大约阅读时间需要 4 分钟。

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

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

 

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

1

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

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

1

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

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

1

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

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

1

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

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

1

# 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.

转载地址:http://aabxi.baihongyu.com/

你可能感兴趣的文章
Python 基础语法(一)
查看>>
Python 基础语法(二)
查看>>
Python 基础语法(三)
查看>>
MXBean already registered报错解决
查看>>
feign接口调用使用header
查看>>
mac 根目录权限问题处理
查看>>
Spring AOP 详解
查看>>
Double数值过大时,默认使用科学计数法的解决办法
查看>>
Spring项目搭建(一)
查看>>
spring项目搭建二(整合springmvc)
查看>>
Spring项目搭建三(整合mybaties)
查看>>
git 提交文件撤销操作
查看>>
SpringBoot入门-helloworld
查看>>
SpringBoot入门-配置文件
查看>>
springboot-定时任务
查看>>
springboot-jpa
查看>>
Spring-定时任务
查看>>
json和对象之间的互转
查看>>
springboot-redis整合
查看>>
Springboot-interceptor(拦截器)
查看>>