成功最有效的方法就是向有经验的人学习!

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

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/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

测试|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/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

结果正常,我们要压缩的文件都在。
测试-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
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
tar: Removing leading `/' from member names
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/stu02.txt
tar zcf /tmp/oldboy-exec.tar.gz /oldboy/stu10.txt
tar zcf /tmp/oldboy-exec.tar.gz /oldboy/stu08.txt
tar zcf /tmp/oldboy-exec.tar.gz /oldboy/stu01.txt
tar zcf /tmp/oldboy-exec.tar.gz /oldboy/stu07.txt
tar zcf /tmp/oldboy-exec.tar.gz /oldboy/stu03.txt
tar zcf /tmp/oldboy-exec.tar.gz /oldboy/stu09.txt
tar zcf /tmp/oldboy-exec.tar.gz /oldboy/stu05.txt
tar zcf /tmp/oldboy-exec.tar.gz /oldboy/stu04.txt
tar zcf /tmp/oldboy-exec.tar.gz /oldboy/stu06.txt
tar zcf /tmp/oldboy-exec.tar.gz /oldboy/stu00.txt

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

赞(0) 打赏
未经允许不得转载:陈桂林博客 » 使用find命令 |xargs(管道xargs)与-exec有什么区别?
分享到

大佬们的评论 抢沙发

全新“一站式”建站,高质量、高售后的一条龙服务

微信 抖音 支付宝 百度 头条 快手全平台打通信息流

橙子建站.极速智能建站8折购买虚拟主机

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续给力更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏

登录

找回密码

注册