焦油创建重命名目录
问题描述:
我有一个名为dir
与文件file1
file2
file3
在里面,我想创建一个gzip压缩的tar目录从它叫做dir.tar.gz
但与层次结构目录:焦油创建重命名目录
anotherdir/file1
anotherdir/file2
anotherdir/file3
总是有选择编写一个shell脚本,将dir
重命名为anotherdir
,然后打开它,但我希望有一个更优雅的解决方案。
答
在tarring过程中使用符号链接和取消引用。
$ mkdir dir && touch dir/file{1,2,3} && ln -sf dir anotherdir
$ tar -hcvpf a.tar anotherdir
anotherdir/
anotherdir/file1
anotherdir/file2
anotherdir/file3
$ tar -tf a.tar
anotherdir/
anotherdir/file1
anotherdir/file2
anotherdir/file3
答
你可以做一个符号链接,然后强制tar
遵循它:
ln -s dir anotherdir
tar -czf dir.tar.gz -L anotherdir
小心将也遵循树内的任何符号链接。
答
以下似乎工作正常。焦油不会跟随链接,但结果焦油将有额外的./为文件(我认为这不是一个大问题)。
mkdir dir another_dir && touch dir/file{1,2,3} another_dir/another_file{1,2,3}
tmp_dir=`mktemp -d /tmp/dXXXX` && ln -s `pwd`/dir $tmp_dir/another_dir &&
tar -C $tmp_dir -cvf result.tar another_dir/. &&
rm $tmp_dir/another_dir && rmdir $tmp_dir
问题是我的设置不典型,目录anotherdir已经存在。没有其他办法吗? – kaykun 2011-03-05 20:23:48
'anotherdir'可以在任何目录下。首先cd到/ tmp,然后创建符号链接。 – kojiro 2011-03-05 20:25:41