首页 > 代码库 > 老男孩教育每日一题-2017年5月19日-使用find命令的时候 |xargs(管道xargs)与-exe有什么区别?
老男孩教育每日一题-2017年5月19日-使用find命令的时候 |xargs(管道xargs)与-exe有什么区别?
1.题目
老男孩教育每日一题-2017年5月19日-使用find命令的时候 |xargs(管道xargs)与-exe有什么区别?
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 相当于执行了很多次,打包压缩的命令而且压缩包名字都一样,所以会不断的覆盖。
3.统计信息
今天是每日一题陪伴大家的第54天,期待你的进步。
对于题目和答案的任何疑问,请在博客评论区留言。
往期题目索引
http://lidao.blog.51cto.com/3388056/1914205
本文出自 “李导的博客” 博客,请务必保留此出处http://lidao.blog.51cto.com/3388056/1927694
老男孩教育每日一题-2017年5月19日-使用find命令的时候 |xargs(管道xargs)与-exe有什么区别?
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。