find命令-xargs参数

find命令-xargs参数

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

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

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

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

命令:

find . -type f -print | xargs file

演示执行及输出:

[zyiz@localhost ~]$ ll
total 0
-rw-rw-r--. 1 zyiz zyiz 0 Feb 15 20:43 log1.log
-rw-rw-r--. 1 zyiz zyiz 0 Feb 15 20:43 log2018.log
-rw-rw-r--. 1 zyiz zyiz 0 Feb 15 20:43 log2019.log
-rw-rw-r--. 1 zyiz zyiz 0 Feb 15 20:43 log2020.log
-rw-rw-r--. 1 zyiz zyiz 0 Feb 15 20:43 log2.log
-rw-rw-r--. 1 zyiz zyiz 0 Feb 15 20:43 log3.log
-rw-rw-r--. 1 zyiz zyiz 0 Feb 15 20:43 log.log
[zyiz@localhost ~]$ find . -type f -print | xargs file
./.bash_logout:  ASCII text
./.bash_profile: ASCII text
./.bashrc:       ASCII text
./log.log:       empty
./log1.log:      empty
./log2.log:      empty
./log3.log:      empty
./log2018.log:   empty
./log2019.log:   empty
./log2020.log:   empty

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

命令:

find / -name "core" -print | xargs echo "" > /home/zyiz/tmpcore.log

演示执行及输出:

[root@localhost zyiz]# find / -name "core" -print | xargs echo "" > /home/zyiz/tmpcore.log
[root@localhost zyiz]# cat tmpcore.log
 /dev/core /proc/sys/net/core /usr/lib/python2.7/site-packages/firewall/core /usr/lib/modules/3.10.0-514.el7.x86_64/kernel/drivers/infiniband/core /usr/lib/modules/3.10.0-514.el7.x86_64/kernel/drivers/memstick/core /usr/lib/modules/3.10.0-514.el7.x86_64/kernel/drivers/mmc/core /usr/lib/modules/3.10.0-514.el7.x86_64/kernel/drivers/net/ethernet/mellanox/mlx5/core /usr/lib/modules/3.10.0-514.el7.x86_64/kernel/net/core /usr/lib/modules/3.10.0-514.el7.x86_64/kernel/sound/core /usr/java/jdk1.8.0_121/lib/visualvm/platform/core /usr/java/jdk1.8.0_121/lib/visualvm/visualvm/core
[root@localhost zyiz]#

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

命令:

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

演示执行及输出:

[zyiz@localhost ~]$ ll
total 4
-rw-rw-r--. 1 zyiz zyiz  0 Feb 15 20:43 log1.log
-rw-rw-r--. 1 zyiz zyiz  0 Feb 15 20:43 log2018.log
-rw-rw-r--. 1 zyiz zyiz  0 Feb 15 20:43 log2019.log
-rw-rw-r--. 1 zyiz zyiz  0 Feb 15 20:43 log2020.log
-rwxrwxrwx. 1 zyiz zyiz  0 Feb 15 20:43 log2.log
-rw-rw-r--. 1 zyiz zyiz  0 Feb 15 20:43 log3.log
-rwxrwxrwx. 1 zyiz zyiz  0 Feb 15 20:43 log.log
drwxrwxr-x. 7 zyiz zyiz 66 Feb 15 20:48 project
drwxrwxr-x. 2 zyiz zyiz  6 Feb 15 20:48 test3
drwxrwxr-x. 2 zyiz zyiz  6 Feb 15 20:48 test4
-rw-rw-r--. 1 zyiz zyiz  1 Feb 15 20:46 tmpcore.log
[zyiz@localhost ~]$ find . -perm -7 -print | xargs chmod o-w
[zyiz@localhost ~]$ ll
total 4
-rw-rw-r--. 1 zyiz zyiz  0 Feb 15 20:43 log1.log
-rw-rw-r--. 1 zyiz zyiz  0 Feb 15 20:43 log2018.log
-rw-rw-r--. 1 zyiz zyiz  0 Feb 15 20:43 log2019.log
-rw-rw-r--. 1 zyiz zyiz  0 Feb 15 20:43 log2020.log
-rwxrwxr-x. 1 zyiz zyiz  0 Feb 15 20:43 log2.log
-rw-rw-r--. 1 zyiz zyiz  0 Feb 15 20:43 log3.log
-rwxrwxr-x. 1 zyiz zyiz  0 Feb 15 20:43 log.log
drwxrwxr-x. 7 zyiz zyiz 66 Feb 15 20:48 project
drwxrwxr-x. 2 zyiz zyiz  6 Feb 15 20:48 test3
drwxrwxr-x. 2 zyiz zyiz  6 Feb 15 20:48 test4
-rw-rw-r--. 1 zyiz zyiz  1 Feb 15 20:46 tmpcore.log
[zyiz@localhost ~]$

说明:执行命令后,文件log.loglog1.log的权限都发生改变

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

命令:

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

演示执行及输出:

[zyiz@localhost ~]$ ll
total 16
-rw-rw-r--. 1 zyiz zyiz  464 Feb 15 20:58 log1.log
-rw-rw-r--. 1 zyiz zyiz    0 Feb 15 20:43 log2018.log
-rw-rw-r--. 1 zyiz zyiz    0 Feb 15 20:43 log2019.log
-rw-rw-r--. 1 zyiz zyiz    0 Feb 15 20:43 log2020.log
-rwxrwxr-x. 1 zyiz zyiz    0 Feb 15 20:43 log2.log
-rw-rw-r--. 1 zyiz zyiz  384 Feb 15 20:59 log3.log
-rwxrwxr-x. 1 zyiz zyiz 1197 Feb 15 20:58 log.log
drwxrwxr-x. 7 zyiz zyiz   66 Feb 15 20:48 project
drwxrwxr-x. 2 zyiz zyiz    6 Feb 15 20:48 test3
drwxrwxr-x. 2 zyiz zyiz    6 Feb 15 20:48 test4
-rw-rw-r--. 1 zyiz zyiz    1 Feb 15 20:46 tmpcore.log
[zyiz@localhost ~]$ find . -type f -print | xargs grep "Spring"
./log1.log:@SpringBootApplication
./log1.log:public class SpringBootConsoleApplication implements CommandLineRunner {
./log1.log:        SpringApplication.run(SpringBootConsoleApplication.class, args);
[zyiz@localhost ~]$

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

命令:

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

演示执行及输出:

[zyiz@localhost ~]$ find . -name \* -type f -print | xargs grep "import"
./log1.log:import org.springframework.boot.CommandLineRunner;
./log3.log:import org.springframework.beans.factory.annotation.Value;
./log3.log:import org.springframework.stereotype.Service;
[zyiz@localhost ~]$

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

实例6:使用xargs执行mv

命令:

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

演示执行及输出:

[zyiz@localhost ~]$ ll
total 20
-rw-rw-r--. 1 zyiz zyiz  464 Feb 15 20:58 log1.log
-rw-rw-r--. 1 zyiz zyiz    0 Feb 15 20:43 log2018.log
-rw-rw-r--. 1 zyiz zyiz    0 Feb 15 20:43 log2019.log
-rw-rw-r--. 1 zyiz zyiz    0 Feb 15 20:43 log2020.log
-rwxrwxr-x. 1 zyiz zyiz    0 Feb 15 20:43 log2.log
-rw-rw-r--. 1 zyiz zyiz  384 Feb 15 20:59 log3.log
-rwxrwxr-x. 1 zyiz zyiz 1197 Feb 15 20:58 log.log
drwxrwxr-x. 7 zyiz zyiz   66 Feb 15 20:48 project
drwxrwxr-x. 2 zyiz zyiz    6 Feb 15 20:48 test3
drwxrwxr-x. 2 zyiz zyiz    6 Feb 15 20:48 test4
-rw-rw-r--. 1 zyiz zyiz   19 Feb 15 21:04 text.txt
-rw-rw-r--. 1 zyiz zyiz    1 Feb 15 20:46 tmpcore.log
[zyiz@localhost ~]$ find . -name "*.txt" | xargs -i mv {} test4
[zyiz@localhost ~]$ ll test4/
total 4
-rw-rw-r--. 1 zyiz zyiz 19 Feb 15 21:04 text.txt
[zyiz@localhost ~]$

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

命令:

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

演示执行及输出:

[zyiz@localhost ~]$ cd test4/
[zyiz@localhost test4]$
[zyiz@localhost test4]$
[zyiz@localhost test4]$ ll
total 4
-rw-rw-r--. 1 zyiz zyiz 19 Feb 15 21:04 text.txt
[zyiz@localhost test4]$ find . -type f -atime +0 -print0 | xargs -0 -l1 -t rm -f
rm -f
[zyiz@localhost test4]$

说明:-l1是一次处理一个;-t是处理之前打印出命令

实例8:使用-i参数默认的前面输出用{}代替,-I参数可以指定其他代替字符,如例子中的[],把 *.txt 文件拷贝到上级目录中。

演示执行及输出:

[zyiz@localhost test4]$ ll
total 4
-rw-rw-r--. 1 zyiz zyiz 19 Feb 15 21:04 text.txt
[zyiz@localhost test4]$ find . -name "*.txt" | xargs -I [] cp [] ..
[zyiz@localhost test4]$ ll
total 4
-rw-rw-r--. 1 zyiz zyiz 19 Feb 15 21:04 text.txt
[zyiz@localhost test4]$ cd ..
[zyiz@localhost ~]$ ll
total 20
-rw-rw-r--. 1 zyiz zyiz  464 Feb 15 20:58 log1.log
-rw-rw-r--. 1 zyiz zyiz    0 Feb 15 20:43 log2018.log
-rw-rw-r--. 1 zyiz zyiz    0 Feb 15 20:43 log2019.log
-rw-rw-r--. 1 zyiz zyiz    0 Feb 15 20:43 log2020.log
-rwxrwxr-x. 1 zyiz zyiz    0 Feb 15 20:43 log2.log
-rw-rw-r--. 1 zyiz zyiz  384 Feb 15 20:59 log3.log
-rwxrwxr-x. 1 zyiz zyiz 1197 Feb 15 20:58 log.log
drwxrwxr-x. 7 zyiz zyiz   66 Feb 15 20:48 project
drwxrwxr-x. 2 zyiz zyiz    6 Feb 15 20:48 test3
drwxrwxr-x. 2 zyiz zyiz   22 Feb 15 21:04 test4
-rw-rw-r--. 1 zyiz zyiz   19 Feb 15 21:08 text.txt
-rw-rw-r--. 1 zyiz zyiz    1 Feb 15 20:46 tmpcore.log

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

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

命令:

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

演示执行及输出:

[zyiz@localhost ~]$ ll
total 20
-rw-rw-r--. 1 zyiz zyiz  464 Feb 15 20:58 log1.log
-rw-rw-r--. 1 zyiz zyiz    0 Feb 15 20:43 log2018.log
-rw-rw-r--. 1 zyiz zyiz    0 Feb 15 20:43 log2019.log
-rw-rw-r--. 1 zyiz zyiz    0 Feb 15 20:43 log2020.log
-rwxrwxr-x. 1 zyiz zyiz    0 Feb 15 20:43 log2.log
-rw-rw-r--. 1 zyiz zyiz  384 Feb 15 20:59 log3.log
-rwxrwxr-x. 1 zyiz zyiz 1197 Feb 15 20:58 log.log
drwxrwxr-x. 7 zyiz zyiz   66 Feb 15 20:48 project
drwxrwxr-x. 2 zyiz zyiz   23 Feb 15 21:10 test3
drwxrwxr-x. 2 zyiz zyiz   22 Feb 15 21:04 test4
-rw-rw-r--. 1 zyiz zyiz   19 Feb 15 21:08 text.txt
-rw-rw-r--. 1 zyiz zyiz    1 Feb 15 20:46 tmpcore.log
[zyiz@localhost ~]$ pwd
/home/zyiz
[zyiz@localhost ~]$ ll
total 20
-rw-rw-r--. 1 zyiz zyiz  464 Feb 15 20:58 log1.log
-rw-rw-r--. 1 zyiz zyiz    0 Feb 15 20:43 log2018.log
-rw-rw-r--. 1 zyiz zyiz    0 Feb 15 20:43 log2019.log
-rw-rw-r--. 1 zyiz zyiz    0 Feb 15 20:43 log2020.log
-rwxrwxr-x. 1 zyiz zyiz    0 Feb 15 20:43 log2.log
-rw-rw-r--. 1 zyiz zyiz  384 Feb 15 20:59 log3.log
-rwxrwxr-x. 1 zyiz zyiz 1197 Feb 15 20:58 log.log
drwxrwxr-x. 7 zyiz zyiz   66 Feb 15 20:48 project
drwxrwxr-x. 2 zyiz zyiz   23 Feb 15 21:10 test3
drwxrwxr-x. 2 zyiz zyiz   22 Feb 15 21:04 test4
-rw-rw-r--. 1 zyiz zyiz   19 Feb 15 21:08 text.txt
-rw-rw-r--. 1 zyiz zyiz    1 Feb 15 20:46 tmpcore.log
[zyiz@localhost ~]$ ll test3/
total 0
-rw-rw-r--. 1 zyiz zyiz 0 Feb 15 21:10 test3.log
[zyiz@localhost ~]$ cd test3/
[zyiz@localhost test3]$ find . -name "*.log" | xargs -p -i mv {} ..
mv ./test3.log .. ?...y
[zyiz@localhost test3]$ ll
total 0
[zyiz@localhost test3]$ cd ..
[zyiz@localhost ~]$ ll
total 20
-rw-rw-r--. 1 zyiz zyiz  464 Feb 15 20:58 log1.log
-rw-rw-r--. 1 zyiz zyiz    0 Feb 15 20:43 log2018.log
-rw-rw-r--. 1 zyiz zyiz    0 Feb 15 20:43 log2019.log
-rw-rw-r--. 1 zyiz zyiz    0 Feb 15 20:43 log2020.log
-rwxrwxr-x. 1 zyiz zyiz    0 Feb 15 20:43 log2.log
-rw-rw-r--. 1 zyiz zyiz  384 Feb 15 20:59 log3.log
-rwxrwxr-x. 1 zyiz zyiz 1197 Feb 15 20:58 log.log
drwxrwxr-x. 7 zyiz zyiz   66 Feb 15 20:48 project
drwxrwxr-x. 2 zyiz zyiz    6 Feb 15 21:12 test3
-rw-rw-r--. 1 zyiz zyiz    0 Feb 15 21:10 test3.log
drwxrwxr-x. 2 zyiz zyiz   22 Feb 15 21:04 test4
-rw-rw-r--. 1 zyiz zyiz   19 Feb 15 21:08 text.txt
-rw-rw-r--. 1 zyiz zyiz    1 Feb 15 20:46 tmpcore.log
[zyiz@localhost ~]$

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