有趣的tar用法...但是发生了什么事? [英] Interesting usage of tar... but what is happening?
问题描述
我在同事的Bash脚本中看到了tar的以下有趣用法:
I saw the following interesting usage of tar in a co-worker's Bash scripts:
`tar cf - * | (cd <dest> ; tar xf - )`
显然,它的工作原理与rsync -av相似,但速度更快.问题出现了,怎么办?
Apparently it works much like rsync -av does, but faster. The question arises, how?
-m
编辑:谁能解释为什么比以下解决方案更可取?
EDIT: Can anyone explain why should this solution be preferable over the following?
cp -rfp * dest
前者更快吗?
推荐答案
关于cp和tar之间的区别以复制目录层次结构,可以进行一个简单的实验来显示区别:
On the difference between cp and tar to copy the directory hierarchies, a simple experiment can be conducted to show the difference:
alastair box:~/hack/cptest [1134]% mkdir src
alastair box:~/hack/cptest [1135]% cd src
alastair box:~/hack/cptest/src [1136]% touch foo
alastair box:~/hack/cptest/src [1137]% ln -s foo foo-s
alastair box:~/hack/cptest/src [1138]% ln foo foo-h
alastair box:~/hack/cptest/src [1139]% ls -a
total 0
-rw-r--r-- 2 alastair alastair 0 Nov 25 14:59 foo
-rw-r--r-- 2 alastair alastair 0 Nov 25 14:59 foo-h
lrwxrwxrwx 1 alastair alastair 3 Nov 25 14:59 foo-s -> foo
alastair box:~/hack/cptest/src [1142]% mkdir ../cpdest
alastair box:~/hack/cptest/src [1143]% cp -rfp * ../cpdest
alastair box:~/hack/cptest/src [1144]% mkdir ../tardest
alastair box:~/hack/cptest/src [1145]% tar cf - * | (cd ../tardest ; tar xf - )
alastair box:~/hack/cptest/src [1146]% cd ..
alastair box:~/hack/cptest [1147]% ls -l cpdest
total 0
-rw-r--r-- 1 alastair alastair 0 Nov 25 14:59 foo
-rw-r--r-- 1 alastair alastair 0 Nov 25 14:59 foo-h
lrwxrwxrwx 1 alastair alastair 3 Nov 25 15:00 foo-s -> foo
alastair box:~/hack/cptest [1148]% ls -l tardest
total 0
-rw-r--r-- 2 alastair alastair 0 Nov 25 14:59 foo
-rw-r--r-- 2 alastair alastair 0 Nov 25 14:59 foo-h
lrwxrwxrwx 1 alastair alastair 3 Nov 25 15:00 foo-s -> foo
区别在于硬链接的文件.请注意,如何分别使用 cp
和 tar
复制硬链接的文件.为了使区别更加明显,请查看每个节点的索引节点:
The difference is in the hard-linked files. Notice how the hard-linked files are copied individually with cp
and together with tar
. To make the difference more obvious, have a look at the inodes for each:
alastair box:~/hack/cptest [1149]% ls -i cpdest
24690722 foo 24690723 foo-h 24690724 foo-s
alastair box:~/hack/cptest [1150]% ls -i tardest
24690801 foo 24690801 foo-h 24690802 foo-s
可能还有其他原因更喜欢tar,但这是一个很大的原因,至少在您具有广泛的硬链接文件的情况下.
There are probably other reasons to prefer tar, but this is one big one, at least if you have extensively hard-linked files.
这篇关于有趣的tar用法...但是发生了什么事?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!