我们可以用管道将一个命令的stdout重定向到另一个命令的stdin。例如
cat foo.txt | grep "bar"
但是,有些命令只能以命令行参数的形式接受数据,而无法通过stdin接受数据流。在这种情况下,xargs就显得非常的有用了。
cat example.txt
1 2 3 4
5 6 7
8 9
cat example.txt | xargs
1 2 3 4 5 6 7 8 9
cat eaxmple.txt | xargs -n 3
1 2 3
4 5 6
7 8 9
echo "splitXsplitXsplitXsplit" | xargs -d X
split split split split
首先,看下下面的一行命令有什么效果:
find . -name ‘*.sh‘ | ls -al
它会列出当前目录下的所有文件,因为ls不能接受管道的stdout作为自己的stdin 怎么办呢?
--用xargs:
find . -name ‘*.sh‘ | xargs ls -al
这样就能列出当前目录下所有以sh为后缀的文件了。
另外,上面的这条命令还可以用find -exec来写:
find . -name ‘*.sh‘ -exec ls -al {} \;
【linux学习笔记】xargs,布布扣,bubuko.com
原文:http://blog.csdn.net/wusuopubupt/article/details/20465045