老男孩教育每日一题-2017年5月19日-使用find命令的时候 |xargs(管道xargs)与-exe有什么区别?
一般来说这两个参数或命令是一样的。可是在一些情况下尤其是打包压缩的时候差别就很大了。
find /oldboy -type f |xargs find命令找到的文件一次性都给 |xargs 处理
find /oldboy -type f -exec -exec find命令找到一个文件 就传递给 -exec 处理一次
[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
[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 结果正常,我们要压缩的文件都在。
[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 相当于执行了很多次,打包压缩的命令而且压缩包名字都一样,所以会不断的覆盖。
今天是每日一题陪伴大家的第54天,期待你的进步。
对于题目和答案的任何疑问,请在博客评论区留言。
往期题目索引
http://lidao.blog.51cto.com/3388056/1914205
本文出自 “李导的博客” 博客,请务必保留此出处http://lidao.blog.51cto.com/3388056/1927694
老男孩教育每日一题-2017年5月19日-使用find命令的时候 |xargs(管道xargs)与-exe有什么区别?
原文地址:http://lidao.blog.51cto.com/3388056/1927694