xargs 用作替换工具,读取输入数据重新格式化后输出
xargs 用作替换工具,读取输入数据重新格式化后输出
xargs 用作替换工具,读取输入数据重新格式化后输出

xargs 是一个强有力的命令,它能够捕获一个命令的输出,然后传递给另外一个命令。

之所以能用到这个命令,关键是由于很多命令不支持|管道来传递参数,而日常工作中有有这个必要,所以就有了 xargs 命令,

demo

在目录中搜索含有固定字符串文件命令,比如:

在当前目录(及其子目录)中搜索扩展名为 .txt 结尾文件中包含 hello 的内容的行

1
2
3
4
5
6
7
8
9
10
11
搜索文件中内容
grep "hello" test01.txt

查找 .txt 结尾的文件
find -name "*.txt"
find /home/fei/www -name "*.txt"
******************************************************************************

find -name "*.txt" | xargs grep "hello"
find /home/fei/www -name "*.txt" | xargs grep "hello"
locate *.txt | xargs grep "hello"

xargs 典型使用

单行输出文本内容

1
2
3
cat test01.txt | xargs

cat test01.txt | xargs -n3 [选项 -n]

xargs 典型使用02

xargs 典型使用03

其他使用场景demo

1
2
cat url.txt | xargs wget -c     [下载url.txt 中链接的内容]
ls | xargs -n2