redhat で tar 形式のアーカイブファイルを作成する




redhat で tar 形式のアーカイブファイルを作成する方法は次の通り。


○ tar ファイルを作成する

> tar cvf [作成する tar ファイル名] [アーカイブに含めるファイル1] [アーカイブに含めるファイル2] ...


○ tar ファイル作成の実行例

# tar cvf test.tar test-file1 test-file2 test-file3
test-file1
test-file2
test-file3

このコマンドでできた test.tar ファイルはどこで展開しても
test-file1, test-file2, test-file3 がカレントディレクトリに作成される。

次のようにファイル指定した場合を見てみる。

# tar cvf test.tar /tmp/test-file1 /tmp/test-file2 /tmp/test-file3
tar: メンバ名から先頭の '/' を取り除きます
/tmp/test-file1
/tmp/test-file2
/tmp/test-file3

アーカイブ内のファイルの先頭に 「/」 が付いていると、
tar ファイルを展開した時に、どこで展開しても
/tmp に test-file1, test-file2, test-file3 が作成されることになる。
これは気を付けないと他のファイルを上書きする危険があるため、
自動的に除去してくれたみたい。 ( Cent OS 5 )

逆に、「/」 を取り除かずに絶対パスを保持したままアーカイブしたい場合は、
-P オプションを使用する。

# tar cvf test.tar -P /tmp/test-file1 /tmp/test-file2 /tmp/test-file3
/tmp/test-file1
/tmp/test-file2
/tmp/test-file3

ファイルの指定には「 * 」などのワイルドカードも使用可能。

展開する時のことを考えて通常はアーカイブしたいファイルをディレクトリに集めて、
そのディレクトリをまるごとアーカイブしたりする。

その場合は次のようにする。

# tar cvf test.tar -P test-dir
test-dir/
test-dir/test-file4
test-dir/test-file5

こうしておけば、test.tar を展開するとカレントディレクトリに
test-dir が作成されて、その中に test-file4、test-file5 が作成されるので、
アーカイブに入っていたものが何かは test-dir を見れば分かって使い易い。

redhat で tar コマンドを使う