xargs是给命令传递参数的一个过滤器,也是组合多个命令的一个工具。它把一个数据流分割为一些足够小的块,以方便过滤器和命令进行处理。通常情况下,xargs从管道或者stdin中读取数据,但是它也能够从文件的输出中读取数据。xargs的默认命令是echo,这意味着通过管道传递给xargs的输入将会包含换行和空白,不过通过xargs的处理,换行和空白将被空格取代。
下面是一些使用例子:
1. 找出sclib目录下 *.c文件, 并对结果执行 wc -l, 就是计算每个文件多少行:
sudo find sclib/ -name "*.c" |xargs wc -l
2. 找出 /etc/ 目录下 *.conf 文件,并ls -l.
sudo find /etc -name "*.conf" |xargs wc -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. 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 -print | xargs /bin/rm -f