(转载)linux命令之二十一find命令之xargs

时间:2021-06-06 17:40:59

        在使用 find命令的-exec选项处理匹配到的文件时, find命令将所有匹配到的文件一起传递给exec执行。但有些系统对能够传递给exec的命令长度有限制(#getconf ARG_MAX可以获得操作系统允许的最大参数长度为2621440),这样在find命令运行几分钟之后,就会出现溢出错误。错误信息通常是“参数列太长”或“参数列溢出”。这就是xargs命令的用处所在,特别是与find命令一起使用。  

        find命令把匹配到的文件传递给xargs命令,而xargs命令每次只获取一部分文件而不是全部,不像-exec选项那样。这样它可以先处理最先获取的一部分文件,然后是下一批,并如此继续下去。  

         在有些系统中,使用-exec选项会为处理每一个匹配到的文件而发起一个相应的进程,并非将匹配到的文件全部作为参数一次执行;这样在有些情况下就会出现进程过多,系统性能下降的问题,因而效率不高; 而使用xargs命令则只有一个进程。另外,在使用xargs命令时,究竟是一次获取所有的参数,还是分批取得参数,以及每一次获取参数的数目都会根据该命令的选项及系统内核中相应的可调参数来确定。

          exec与xargs比较:

(1)exec 每处理一个文件或者目录,它都需要启动一次命令,打开一个进程,效率不好。并且参数列表有长度限制。

(2)exec 格式麻烦,必须用 {} 做文件的代位符,必须用 \; 作为命令的结束符,书写不便。

(3)xargs不能操作文件名有空格的文件,比如#find . -name *test |xargs rm,若有文件名为a test,则会报错,因为这时执行的命令
无法区分哪些是分割符、哪些是文件名中的空格! 而用exec则不会有这个问题。

(4)xargs后面的命令须支持多参数,比如rm a.txt b.txt,同时删除两个文件;而有些命令如unzip 后面直接加一个文件名,故不能使用xargs,必须用-exec。

1.命令格式:

xargs [ -p ] [ -t] [ -e[ EOFString ] ] [ -EEOFString ] [ -i[ ReplaceString ] ] [ -IReplaceString ] [ -l [ Number ] ] [ -L Number ] [ -n Number [ -x ] ] [ -s Size ] [ Command [ Argument ... ] ]

注: 不要在小写标志和参数之间放置空格。

2.命令功能:

它的作用是将参数列表转换成小块分段传递给其他命令,以避免参数列表过长的问题

3.命令参数:

-0 当sdtin含有特殊字元时候,将其当成一般字符,想/'空格等

例如:#echo -n "//"|xargs -0 echo

            //

-a file 从文件中读入作为sdtin

-e flag ,注意有的时候可能会是-E,flag必须是一个以空格分隔的标志,当xargs分析到含有flag这个标志的时候就停止。

-p 当每次执行一个argument的时候询问一次用户。

-n num 后面加次数,表示命令在执行的时候一次用的argument的个数,默认是用所有的。

-t 表示先打印命令,然后再执行。

-i 或者是-I,这得看linux支持了,将xargs的每项名称,一般是一行一行赋值给{},可以用{}代替。

-r no-run-if-empty 当xargs的输入为空的时候则停止xargs,不用再去执行了。

-s num 命令行的最好字符数,指的是xargs后面那个命令的最大命令行字符数。

-L  num Use at most max-lines nonblank input lines per command line.-s是含有空格的。

-l  同-L

-d delim 分隔符,默认的xargs分隔符是回车,argument的分隔符是空格,这里修改的是xargs的分隔符。

-x exit的意思,主要是配合-s使用。

-P 修改最大的进程数,默认是1,为0时候为as many as it can ,这个例子我没有想到,应该平时都用不到的吧。

退出时所返回的值

    0          所有 Command 参数的调用 都返回退出状态 0。

    1-125不能汇编满足指定需求的命令行, 一个或多个 Command 参数的调用 返回一个非零的退出状态,或发生一些其它的错误。

    126     Command 已找到但不能被调用。

    127     找不到 Command。

4.使用实例:

实例1: 查找系统中的每一个普通文件,然后使用xargs命令来测试它们分别属于哪类文件 

命令:

find . -type f -print | xargs file

输出:

[root@localhost test]# find . -type f -print | xargs file
./log2014.log: empty
./log2013.log: empty
./log2012.log: ASCII text

实例2:在整个系统中查找内存信息转储文件(core dump) ,然后把结果保存到/tmp/core.log 文件中

命令:

 find / -name "core" -print | xargs echo >/tmp/core.log

输出:

[root@localhost test]# find / -name "core" -print | xargs echo  >/tmp/core.log
[root@localhost test]# cd /tmp
[root@localhost tmp]# ll
总计 16
-rw-r--r-- 1 root root 1524 11-12 22:29 core.log

实例3:在当前目录下查找所有用户具有读、写和执行权限的文件,并收回相应的写权限

命令:

find . -perm -7 -print | xargs chmod o-w

输出:

[root@localhost test]# ll
总计 312
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root      0 11-12 22:25 log2013.log
-rw-r--r-- 1 root root      0 11-12 22:25 log2014.log
[root@localhost test]# find . -perm -7 -print | xargs chmod o-w
[root@localhost test]# ll
总计 312
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root      0 11-12 22:25 log2013.log
-rw-r--r-- 1 root root      0 11-12 22:25 log2014.log

说明:执行命令后,文件夹scf、test3和test4的权限都发生改变。

实例4:用grep命令在所有的普通文件中搜索hostname这个词

命令:

find . -type f -print | xargs grep "hostname"

输出:

[root@localhost test]# find . -type f -print | xargs grep "hostname"
./log2013.log:hostnamebaidu=baidu.com
./log2013.log:hostnamesina=sina.com
./log2013.log:hostnames=true

实例5:用grep命令在当前目录下的所有普通文件中搜索hostnames这个词

命令:

find . -name \* -type f -print | xargs grep "hostnames"

输出:

[root@peida test]# find . -name \* -type f -print | xargs grep "hostnames"
./log2013.log:hostnamesina=sina.com
./log2013.log:hostnames=true

说明:注意,在上面的例子中, \用来取消find命令中的*在shell中的特殊含义。  

实例6:使用xargs执行mv 

命令:

find . -name "*.log" | xargs -i mv {} test4

输出:

[root@localhost test]# ll
总计 316
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root     61 11-12 22:44 log2013.log
-rw-r--r-- 1 root root      0 11-12 22:25 log2014.log
drwxr-xr-x 6 root root   4096 10-27 01:58 scf
drwxrwxr-x 2 root root   4096 11-12 22:54 test3
drwxrwxr-x 2 root root   4096 11-12 19:32 test4
[root@localhost test]# cd test4/
[root@localhost test4]# ll
总计 0[root@localhost test4]# cd ..
[root@localhost test]# find . -name "*.log" | xargs -i mv {} test4
[root@localhost test]# ll
总计 12drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-13 05:50 test3
drwxrwxr-x 2 root root 4096 11-13 05:50 test4
[root@localhost test]# cd test4/
[root@localhost test4]# ll
总计 304
-rw-r--r-- 1 root root 302108 11-12 22:54 log2012.log
-rw-r--r-- 1 root root     61 11-12 22:54 log2013.log
-rw-r--r-- 1 root root      0 11-12 22:54 log2014.log

实例7:find后执行xargs提示xargs: argument line too long解决方法:

命令:

find . -type f -atime +0 -print0 | xargs -0 -l1 -t rm -f

说明:

-l1是一次处理一个;-t是处理之前打印出命令;-0把特殊字元当成一般字符

实例8:使用-i参数默认的前面输出用{}代替,-I参数可以指定其他代替字符,如例子中的[] 

[root@localhost test]# ll
总计 12drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-13 05:50 test3
drwxrwxr-x 2 root root 4096 11-13 05:50 test4
[root@localhost test]# cd test4
[root@localhost test4]# find . -name "file" | xargs -I [] cp [] ..
[root@localhost test4]# ll
总计 304
-rw-r--r-- 1 root root 302108 11-12 22:54 log2012.log
-rw-r--r-- 1 root root     61 11-12 22:54 log2013.log
-rw-r--r-- 1 root root      0 11-12 22:54 log2014.log
[root@localhost test4]# cd ..
[root@localhost test]# ll
总计 316
-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log
-rw-r--r-- 1 root root     61 11-13 06:03 log2013.log
-rw-r--r-- 1 root root      0 11-13 06:03 log2014.log
drwxr-xr-x 6 root root   4096 10-27 01:58 scf
drwxrwxr-x 2 root root   4096 11-13 05:50 test3
drwxrwxr-x 2 root root   4096 11-13 05:50 test4

说明:使用-i参数默认的前面输出用{}代替,-I参数可以指定其他代替字符,如例子中的[] 

实例9:xargs的-p参数的使用 

命令:

find . -name "*.log" | xargs -p -i mv {} ..

输出:

[root@localhost test3]# ll
总计 0
-rw-r--r-- 1 root root 0 11-13 06:06 log2015.log
[root@localhost test3]# cd ..
[root@localhost test]# ll
总计 316
-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log
-rw-r--r-- 1 root root     61 11-13 06:03 log2013.log
-rw-r--r-- 1 root root      0 11-13 06:03 log2014.log
drwxr-xr-x 6 root root   4096 10-27 01:58 scf
drwxrwxr-x 2 root root   4096 11-13 06:06 test3
drwxrwxr-x 2 root root   4096 11-13 05:50 test4
[root@localhost test]# cd test3
[root@localhost test3]#  find . -name "*.log" | xargs -p -i mv {} ..
mv ./log2015.log .. ?...y
[root@localhost test3]# ll
总计 0[root@localhost test3]# cd ..
[root@localhost test]# ll
总计 316
-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log
-rw-r--r-- 1 root root     61 11-13 06:03 log2013.log
-rw-r--r-- 1 root root      0 11-13 06:03 log2014.log
-rw-r--r-- 1 root root      0 11-13 06:06 log2015.log
drwxr-xr-x 6 root root   4096 10-27 01:58 scf
drwxrwxr-x 2 root root   4096 11-13 06:08 test3
drwxrwxr-x 2 root root   4096 11-13 05:50 test4

说明:-p参数会提示让你确认是否执行后面的命令,y执行,n不执行。

实例10:SVN提交更新代码

若使用-exec提交每个文件,会导致每一个文件提交日志都一样,最好用xargs一起一次提交记录

#svn st | grep '^[AMD]' | cut -c 9- | xargs svn ci -m "commit"