tarコマンドの "--keep-newer-files"オプションを必ず使用する必要があるのはいつですか?

tarコマンドの

tarコマンド情報

紹介する

たとえば、

source
 numbers
  001.txt # with the 111 content
  002.txt # with the 222 content
  003.txt # with the 333 content

numbers.tar.gzcommandでファイルを作成すると、tar -czf numbers.tar.gz numbers次のような結果が表示されます。

source
 numbers.tar.gz
 numbers
  001.txt # with the 111 content
  002.txt # with the 222 content
  003.txt # with the 333 content

mv numbers.tar.gz targetコマンドが実行されているかどうかを考慮して、次のようにします。

target
 numbers.tar.gz

tar -xzf numbers.tar.gzコマンドが実行されると、

target
 numbers.tar.gz
 numbers
  001.txt # with the 111 content
  002.txt # with the 222 content
  003.txt # with the 333 content

したがって、一般的な概要は次のとおりです。

source
 numbers.tar.gz
 numbers
  001.txt # with the 111 content
  002.txt # with the 222 content
  003.txt # with the 333 content
target
 numbers.tar.gz
 numbers
  001.txt # with the 111 content
  002.txt # with the 222 content
  003.txt # with the 333 content

優先順位制御

次の簡単なアップデートを考えてみましょう。

target
 numbers.tar.gz
 numbers
  001.txt # with the 111 content
  002.txt # with the 222222 content <--- updated
  003.txt # with the 333 content

tar -xzf numbers.tar.gzディレクトリからコマンドを実行すると、target002.txtファイルは次のようになります。覆われたしたがって、222222の内容は222として返されます。今まで大丈夫です。

新しいデータを安全に保ち、不要な上書きを防ぐには、次の手順に従って--keep-old-filesオプションを使用できます。--skip-old-filestar(1) - Linux のマニュアルページ、これは次のことを意味します。

-k, --keep-old-files
don't replace existing files when extracting, treat them as errors
--skip-old-files
don't replace existing files when extracting, silently skip over them

したがって、次の2つのコマンドを実行するには、次の手順を実行します。

tar --keep-old-files -xzf numbers.tar.gz
tar --skip-old-files -xzf numbers.tar.gz

次のようなことが発生します。

  • 前者は常にtar: numbers/222.txt: Cannot open: File existsエラーメッセージを表示し、データは安全に保たれます(222222のまま)。
  • 後者は何も表示しません。例外は、vこのオプションを使用する場合ですtar: numbers/222.txt: skipping existing file。メッセージが表示され、データは安全に保持されます(222222として予約されています)。スクリプトの目的に役立ちます。

今まではそんなに良くなった。

--keep-newer-files調査後、オプションが見つかり、再び従いました。tar(1) - Linux のマニュアルページ、これは次のことを意味します。

--keep-newer-files
don't replace existing files that are newer than their archive copies

したがって、次のコマンドを実行するには:

tar --keep-newer-files -xzf numbers.tar.gz

次のようなことが発生します。

  • メッセージが表示され、tar: Current ‘numbers/222.txt’ is newer or same ageデータは安全に保たれます(222222のまま)。

実際、このオプションは上書きを防ぐのと--keep-newer-files同じことを行います。--skip-old-filesしかし、さまざまなメッセージを表示します。

質問

  • オプションの代わりにコマンド--keep-newer-filesのオプションを使用する必要があるのはいつですか?tar--keep-old-files--skip-old-files

このオプションが必須である特定のシナリオが何であるかを知りたいです。

ベストアンサー1

--keep-newer-filesターゲットへの変更を保存したい場合に便利です。後ろにソースファイルは最後に変更され、ターゲットの以前のバージョンはソースの最新バージョンに置き換えられます。

これら2つのオプションの違いを説明するには、各ファイルのタイムスタンプという別の情報が必要です。以下を考慮してください。

source
  numbers
    001.txt # timestamp t1, contents 1
    002.txt # timestamp t1, contents 222
    003.txt # timestamp t1, contents 333

targetタイムスタンプを保存するには、このファイルをコピーしてください。

source
  numbers
    001.txt # timestamp t1, contents 1
    002.txt # timestamp t1, contents 222
    003.txt # timestamp t1, contents 333
target
  numbers
    001.txt # timestamp t1, contents 1
    002.txt # timestamp t1, contents 222
    003.txt # timestamp t1, contents 333

今あなたが001.txt直して源泉(観察t2部分):

source
  numbers
    001.txt # timestamp t2, contents 111
    002.txt # timestamp t1, contents 222
    003.txt # timestamp t1, contents 333
target
  numbers
    001.txt # timestamp t1, contents 1
    002.txt # timestamp t1, contents 222
    003.txt # timestamp t1, contents 333

また002.txt編集済みターゲット(観察t3部分):

source
  numbers
    001.txt # timestamp t2, contents 111
    002.txt # timestamp t1, contents 222
    003.txt # timestamp t1, contents 333
target
  numbers
    001.txt # timestamp t1, contents 1
    002.txt # timestamp t3, contents 22222
    003.txt # timestamp t1, contents 333

新しいアーカイブを作成し、ターゲットから抽出します。

  • オプションがないと、すべてのファイルが抽出されるため、ターゲットはソースの同じコンテンツで終わり、ターゲットへの変更は001.txt失われます。002.txt002.txt
  • --keep-old-files001.txtおよび002.txtは抽出されず、ターゲットは古いバージョンを維持します001.txt
  • を使用すると、--keep-newer-files既存001.txtのファイルがアーカイブ内のファイルより古いため、既存のターゲットファイルを抽出して上書きしますが、既存のファイルはアーカイブ内のファイル002.txtより最新であるため抽出されません。

おすすめ記事