在使用 find命令的-exec选项处理匹配到的文件时, find命令将全部匹配到的文件一块儿传递给exec执行。但有些系统对可以传递给exec的命令长度有限制,这样在find命令运行几分钟以后,就会出现溢出错误。错误信息一般是“参数列太长”或“参数列溢出”。这就是xargs命令的用处所在,特别是与find命令一块儿使用。python
find命令把匹配到的文件传递给xargs命令,而xargs命令每次只获取一部分文件而不是所有,不像-exec选项那样。这样它能够先处理最早获取的一部分文件,而后是下一批,并如此继续下去。python2.7
在有些系统中,使用-exec选项会为处理每个匹配到的文件而发起一个相应的进程,并不是将匹配到的文件所有做为参数一次执行;这样在有些状况下就会出现进程过多,系统性能降低的问题,于是效率不高; 而使用xargs命令则只有一个进程。另外,在使用xargs命令时,到底是一次获取全部的参数,仍是分批取得参数,以及每一次获取参数的数目都会根据该命令的选项及系统内核中相应的可调参数来肯定。性能
命令:测试
find . -type f -print |xargs file
输出:日志
[root@localhost test]# ls dir1 log1 log2 [root@localhost test]# find . -type f -print |xargs file ./log1: empty ./log2: ASCII text
命令:code
find / -name "core" -print | xargs > /tmp/core.log
输出:xml
[root@localhost tmp]# ls xmlXPathIniteihlTv.c xmlXPathInitLrmz_p.c xmlXPathInitpywFgf.c xmlXPathInitv76QxM.c yum_save_tx.2018-11-15.18-23.5nqJ3w.yumtx yum_save_tx.2018-11-16.23-54.cMoa46.yumtx [root@localhost tmp]# find / -name 'core' -print |xargs > /tmp/core.log [root@localhost tmp]# ls core.log xmlXPathIniteihlTv.c xmlXPathInitLrmz_p.c xmlXPathInitpywFgf.c xmlXPathInitv76QxM.c yum_save_tx.2018-11-15.18-23.5nqJ3w.yumtx yum_save_tx.2018-11-16.23-54.cMoa46.yumtx [root@localhost tmp]# cat core.log /dev/core /proc/sys/net/core /usr/lib/python2.7/site-packages/firewall/core /usr/lib/modules/3.10.0-693.el7.x86_64/kernel/drivers/infiniband/core /usr/lib/modules/3.10.0-693.el7.x86_64/kernel/drivers/memstick/core /usr/lib/modules/3.10.0-693.el7.x86_64/kernel/drivers/mmc/core /usr/lib/modules/3.10.0-693.el7.x86_64/kernel/drivers/net/ethernet/mellanox/mlx5/core /usr/lib/modules/3.10.0-693.el7.x86_64/kernel/drivers/usb/core /usr/lib/modules/3.10.0-693.el7.x86_64/kernel/net/core /usr/lib/modules/3.10.0-693.el7.x86_64/kernel/sound/core /usr/lib/modules/3.10.0-862.14.4.el7.x86_64/kernel/drivers/infiniband/core /usr/lib/modules/3.10.0-862.14.4.el7.x86_64/kernel/drivers/memstick/core /usr/lib/modules/3.10.0-862.14.4.el7.x86_64/kernel/drivers/mmc/core /usr/lib/modules/3.10.0-862.14.4.el7.x86_64/kernel/drivers/net/ethernet/mellanox/mlx5/core /usr/lib/modules/3.10.0-862.14.4.el7.x86_64/kernel/drivers/usb/core /usr/lib/modules/3.10.0-862.14.4.el7.x86_64/kernel/net/core /usr/lib/modules/3.10.0-862.14.4.el7.x86_64/kernel/sound/core
说明:进程
> 是定向输出到文件,若是文件不存在,就建立文件;若是文件存在,就将其清空;通常咱们备份清理日志文件的时候,就是这种方法:先备份日志,再用>,将日志文件清空(文件大小变成0字节); >> 这个是将输出内容追加到目标文件中。若是文件不存在,就建立文件;若是文件存在,则将新的内容追加到那个文件的末尾,该文件中的原有内容不受影响。
命令:字符串
find . -perm -7 -print | xargs chmod o-w
输出:it
[root@localhost test]# ll total 4 drwxr-xr-x. 2 root root 6 Nov 20 18:28 dir1 -rwxrwxrwx. 1 root root 0 Nov 20 18:28 log1 -rw-r--r--. 1 root root 4 Nov 20 18:29 log2 [root@localhost test]# find . -perm -7 -print | xargs chmod o-w [root@localhost test]# ll total 4 drwxr-xr-x. 2 root root 6 Nov 20 18:28 dir1 -rwxrwxr-x. 1 root root 0 Nov 20 18:28 log1 -rw-r--r--. 1 root root 4 Nov 20 18:29 log2
说明:
能够看到,执行命令前 log1文件,所属用户 所属组 其余用户均有读、写、执行权限,执行命令后,其余用户没有了写权限,其余权限都还在
命令:
find . -type f -print | xargs grep "hostname"
输出:
[root@localhost test]# ls dir1 log1 log2 [root@localhost test]# cat log1 [root@localhost test]# cat log2 我是log2 hostnamesina=sina.com 哈哈 第三行 [root@localhost test]# find . -type f -print | xargs grep "hostname" ./log2:hostnamesina=sina.com 哈哈
说明:
Linux grep命令用于查找文件里符合条件的字符串。
grep指令用于查找内容包含指定的范本样式的文件,若是发现某文件的内容符合所指定的范本样式,预设grep指令会把含有范本样式的那一行显示出来
命令:
find . -name 'log*' | xargs -i mv {} dir1
输出:
[root@localhost test]# ls dir1 log1 log2 [root@localhost test]# find . -name 'log*' | xargs -i mv {} dir1 [root@localhost test]# ls dir1 [root@localhost test]# cd dir1/ [root@localhost dir1]# ls log1 log2
说明:
{} 花括号表明前面find查找出来的文件名。
命令:
find . -name "log*" | xargs -p -i mv {} ..
输出:
[root@localhost test]# ls dir1 [root@localhost test]# cd dir1/ [root@localhost dir1]# ls log1 log2 log3 [root@localhost dir1]# find . -name "log*" | xargs -p -i mv {} .. mv ./log1 .. ?...y mv ./log2 .. ?...y mv ./log3 .. ?...n [root@localhost dir1]# ls log3 [root@localhost dir1]# cd .. [root@localhost test]# ls dir1 log1 log2
说明:
-p参数会提示让你确认是否执行后面的命令,y执行,n不执行。
命令:
find . -type f -atime +0 -print0 | xargs -0 -l1 -t rm -f
输出:
[root@localhost dir1]# find . -type f -atime +0 -print0 | xargs -0 -l1 -t rm -f rm -f [root@localhost dir1]#
说明:
-l1 是指一次处理一个 -t 是指处理以前打印出的命令 -print 在每个输出后会添加一个回车换行符,而-print0则不会。