xargs是给命令传递参数的一个过滤器,组合多个命令一起执行的工具。

它把一个数据流分割为一些足够小的块,以方便过滤器和命令进行处理。

通常情况下,xargs从管道或者stdin中读取数据,但是它也能够从文件的输出中读取数据。

xargs的默认命令是echo,这意味着通过管道传递给xargs的输入将会包含换行和空白,不过通过xargs的处理,换行和空白将被空格取代。

 

xargs 命令

# xargs --help
Usage: xargs [-0prtx] [--interactive] [--null] [-d|--delimiter=delim]
       [-E eof-str] [-e[eof-str]]  [--eof[=eof-str]]
       [-L max-lines] [-l[max-lines]] [--max-lines[=max-lines]]
       [-I replace-str] [-i[replace-str]] [--replace[=replace-str]]
       [-n max-args] [--max-args=max-args]
       [-s max-chars] [--max-chars=max-chars]
       [-P max-procs]  [--max-procs=max-procs] [--show-limits]
       [--verbose] [--exit] [--no-run-if-empty] [--arg-file=file]
       [--version] [--help] [command [initial-arguments]]

 

find 命令

find   path   -option   [   -print ]   [ -exec   -ok   command ]   {} \;

find 参数:

path:     find命令所查找的目录路径,例如 用 . 表示当前目录,用 / 表示系统根目录。
-option: find命令的过滤选项,例如 -name 表示查找文件, -type 表示类型(b、c、d、l)
-print: find命令将匹配的文件输出到标准输出。
-exec: find命令对匹配的文件执行该参数所给出的shell命令。相应命令的形式为'command' { } \;,注意{ }和\;之间的空格。
-ok: 和-exec的作用相同,只不过以一种更为安全的模式来执行该参数所给出的shell命令,在执行每一个命令之前,都会给出提示,让用户来确定是否执行。

#-print 将查找到的文件输出到标准输出
#-exec   command   {} \;      —–将查到的文件执行command操作,{} 和 \;之间有空格
#-ok 和-exec相同,只不过在操作前要提示询问用户

例:find . -name .svn | xargs rm -rf

 

xargs 示例

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

下面是一些如何有效使用xargs 的实用例子。

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

find . -name 'aaa*' -print0 | xargs -0 rm -f

 

示例1:

经典用法

# find . -name 'aaa*' 
./aaa.log
./aaa.txt
./aaa
./aaa_error.log

 

示例2: 

-print0 标准输出结果不换行、不空格

# find . -name 'aaa*' -print0
./aaa.log./aaa.txt./aaa./aaa_error.log

 

示例3:

xargs 默认 echo 输出

# find . -name 'aaa*' | xargs
./aaa.log ./aaa.txt ./aaa ./aaa_error.log
#
#
# find . -name 'aaa*' | xargs echo
./aaa.log ./aaa.txt ./aaa ./aaa_error.log

 

示例4:

xargs -0 输出换行

# find . -name 'aaa*' | xargs -0  
./aaa.log
./aaa.txt
./aaa
./aaa_error.log

 

示例5:

xargs 加上显示命令

# find . -name 'aaa*' | xargs ls
./aaa  ./aaa_error.log  ./aaa.log  ./aaa.txt

 

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

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

 

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

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

 

6. xargs 多行输入转成一行输出

# cat aaa2.txt 
1
2
3
4
5
# cat aaa2.txt | xargs 
1 2 3 4 5
# cat aaa2.txt | xargs echo
1 2 3 4 5

 

7. xargs 一行输入转成多行输出

# cat aaa.txt 
1 2 3 4 5
# cat aaa.txt | xargs -n 2
1 2
3 4
5
# cat aaa.txt | xargs -n 1
1
2
3
4
5

 

xargs 命令里,空格是默认的定界符,-n 表示每行显示几个参数,2显示两个参数,1显示一个参数(即单行显示)

如果需要指定字符分隔,则用参数 -d,示例如下:

# echo "blog.mimvp.com,proxy.mimvp.com,money.mimvp.com" | xargs -d "," -n 1
blog.mimvp.com
proxy.mimvp.com
money.mimvp.com

 

 

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.

 

 

参考推荐:

Linux下which、whereis、locate、find 区别

Linux中的find 命令

 

原文: Linux xargs命令