2011-10-28 12 views
3

tar -czvf subdir.tgz subdirectory/*を使用してサーバAでtarアーカイブを作成したとします。私がこのtarballをコピーしたサーバーB上で並列構造を持っていて、そのサブディレクトリ内のtarアーカイブに存在しないファイルを削除することを含めて、同じサブディレクトリをクローバーしたい場合 - cd intoサーバーBの親ディレクトリ、rm -rf subdirectory、次にtar -xzvf subdir.tgzです。tarアーカイブを展開するときにディレクトリ構造を削除します

最初にrmを実行しなくてもこれを行う方法はありますか?

テストとして私はサーバBの中にsubdirectory/dummy/のダミーディレクトリを置き、tar -xzvf subdir.tgz --recursive-unlinkを実行しましたが、驚いたことに、subdirectory/dummy/はまだそこにありました。 --recursive-unlinkは何を誤解しているのでしょうか?

編集========== ここに例を示します。私は2つの異なるサーバー上の2つの "ミラーリング"ディレクトリを表す2つのディレクトリを作成します(サーバー 'a'とサーバー 'b'と考える)。コピー先のディレクトリ( 'b')では、tarアーカイブに存在しない追加のディレクトリといくつかのファイルを作成します。これらは私が詰まって取り除かれることを期待しています。基本的に、私がディレクトリ 'a'からのファイルです。しかし、例からわかるように、--unlink-first--recursive-unlinkにもかかわらず、余分なディレクトリとファイルはuntarの後にまだそこにあります。

-bash-3.2$ mkdir tartest 
-bash-3.2$ cd tartest 
-bash-3.2$ mkdir a 
-bash-3.2$ mkdir b 
-bash-3.2$ cd a 
-bash-3.2$ mkdir xyz 
-bash-3.2$ cd xyz 
-bash-3.2$ touch test.1 
-bash-3.2$ touch test.2 
-bash-3.2$ cd ../ 
-bash-3.2$ touch test.3 
-bash-3.2$ cd ../b 
-bash-3.2$ mkdir abc 
-bash-3.2$ cd abc 
-bash-3.2$ touch test.4 
-bash-3.2$ cd ../../a 
-bash-3.2$ tar cvf ../archive.tar ./* 
./test.3 
./xyz/ 
./xyz/test.2 
./xyz/test.1 
-bash-3.2$ cd ../b 
-bash-3.2$ tar -xf ../archive.tar --unlink-first --recursive-unlink 
-bash-3.2$ ls 
total 16K 
drwxr-xr-x 4 iponly168797 iponly168797 4.0K Jan 27 13:58 . 
drwxr-xr-x 4 iponly168797 iponly168797 4.0K Jan 27 13:57 .. 
drwxr-xr-x 2 iponly168797 iponly168797 4.0K Jan 27 13:56 abc 
-rw-r--r-- 1 iponly168797 iponly168797 0 Jan 27 13:56 test.3 
drwxr-xr-x 2 iponly168797 iponly168797 4.0K Jan 27 13:56 xyz 
-bash-3.2$ ls xyz 
total 8.0K 
drwxr-xr-x 2 iponly168797 iponly168797 4.0K Jan 27 13:56 . 
drwxr-xr-x 4 iponly168797 iponly168797 4.0K Jan 27 13:58 .. 
-rw-r--r-- 1 iponly168797 iponly168797 0 Jan 27 13:56 test.1 
-rw-r--r-- 1 iponly168797 iponly168797 0 Jan 27 13:56 test.2 

答えて

1

--recursive-unlinkそれは新しいファイルを抽出してからあなたを維持した場合、ファイルとディレクトリのみを削除します。あなたの目的のために、--unlink-firstを追加してください。両方のオプションを使用すると、新しいtarのファイルとディレクトリnotが削除されます。

これは私のためにどのように動作するかです:

[email protected]:~/tmp/tar $ tar cvf a.tar a 
a/ 
a/test.txt 
a/b/ 
a/c/ 
a/a/ 
[email protected]:~/tmp/tar $ cd b 
[email protected]:~/tmp/tar/b $ tar xvf ../a.tar 
a/ 
a/test.txt 
a/b/ 
a/c/ 
a/a/ 
[email protected]:~/tmp/tar/b $ cd ../a 
[email protected]:~/tmp/tar/a $ rmdir b 
[email protected]:~/tmp/tar/a $ rm test.txt 
rm: reguläre leere Datei »test.txt« entfernen? y 
[email protected]:~/tmp/tar/a $ cd .. 
[email protected]:~/tmp/tar $ tar cvf a.tar a 
a/ 
a/c/ 
a/a/ 
[email protected]:~/tmp/tar $ cd b 
[email protected]:~/tmp/tar/b $ tar xvf ../a.tar --unlink-first --recursive-unlink 
a/ 
a/c/ 
a/a/ 
[email protected]:~/tmp/tar/b $ ls -lR 
.: 
insgesamt 8 
drwxr-xr-x 4 ott ott 4096 31. Okt 19:21 a 

./a: 
insgesamt 16 
drwxr-xr-x 2 ott ott 4096 28. Okt 21:31 a 
drwxr-xr-x 2 ott ott 4096 28. Okt 21:31 c 

./a/a: 
insgesamt 0 

./a/c: 
insgesamt 0 
[email protected]:~/tmp/tar/b $ tar --version 
tar (GNU tar) 1.20 

はたぶん、あなたは別のタールがありますか?

+0

私はテスト/サブディレクトリを作成し、メインサブディレクトリのtest1.txtファイルに触れてから--recursive-unlinkと--unlink-firstを使い、test /ディレクトリとtest1.txtファイルの両方をそのディレクトリ構造にはSTILLがあります。だから私はこの答えが正しいとは思わない。 –

+0

@TomAuger:返信を更新しました。 –

+0

私はTAR(1)を使用しています。 –