1.题目

使用find命令的时候|xargs(管道xargs)与-exec有什么区别?

2.解决

一般来说这两个参数或命令是一样的。可是在一些情况下尤其是打包压缩的时候差别就很大了。

find /oldboy -type f |xargs  find命令找到的文件一次性都给 |xargs 处理

find /oldboy -type f -exec   -exec find命令找到一个文件 就传递给 -exec 处理一次

测试方法—-通过打包压缩tar命令进行测试

测试环境

[root@show ~]# find /oldboy -type f /oldboy/stu02.txt

/oldboy/stu00.txt

/oldboy/stu...txt

/oldboy/stu10.txt

测试|xargs

[root@show ~]# find /oldboy -type f |xargs tar zcf /tmp/oldboy-xargs.tar.gz tar: Removing leading `/' from member names

#检查结果

[root@show ~]# tar tf /tmp/oldboy-xargs.tar.gz 

oldboy/stu00.txt

oldboy/stu...txt

oldboy/stu10.txt

结果正常,我们要压缩的文件都在。

测试-exec

[root@show ~]# find /oldboy -type f -exec tar zcf /tmp/oldboy-exec.tar.gz {} \;tar: Removing leading `/' from member names

tar: Removing leading `/' from member names

tar: Removing leading `/' from member names

tar: Removing leading `/' from member names

[root@show ~]# tar tf /tmp/oldboy-exec.tar.gz 

oldboy/stu00.txt

奇怪为何会这样呢?压缩包中只有一个文件了。

解释:

find /oldboy -type f |xargs tar zcf /tmp/oldboy-xargs.tar.gz

相当于

tar zcf /tmp/oldboy-xargs.tar.gz /oldboy/stu02.txt /oldboy/stu10.txt /oldboy/stu08.txt /oldboy/stu01.txt /oldboy/stu07.txt /oldboy/stu03.txt /oldboy/stu09.txt /oldboy/stu05.txt /oldboy/stu04.txt /oldboy/stu06.txt /oldboy/stu00.txt

find /oldboy -type f -exec tar zcf /tmp/oldboy-exec.tar.gz {} \;

相当于

tar zcf /tmp/oldboy-exec.tar.gz /oldboy/stu00.txt

tar zcf /tmp/oldboy-exec.tar.gz /oldboy/stu...txt

tar zcf /tmp/oldboy-exec.tar.gz /oldboy/stu10.txt

相当于执行了很多次,打包压缩的命令而且压缩包名字都一样,所以会不断的覆盖。

3.统计信息

转载自