0

About the tar command

Introduction

Having for example:

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

If is created the numbers.tar.gz file through the tar -czf numbers.tar.gz numbers command, therefore now we have:

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

Consider if the mv numbers.tar.gz target command is executed, therefore we have:

target
 numbers.tar.gz

If the tar -xzf numbers.tar.gz command is executed therefore we have

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

Therefore as general overview we have:

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

Overriding control

Lets assume the following simple update:

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

If the tar -xzf numbers.tar.gz command at the target directory is executed therefore the 002.txt file is overridden, so its 222222 content returns to 222. Until here I am ok.

To keep the new data safe or to avoid a no desired override can be used the --keep-old-files and --skip-old-files options, according with the tar(1) - Linux man page, it indicates:

-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

Therefore for the execution of the two following commands:

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

happens the following:

  • the former always shows the tar: numbers/222.txt: Cannot open: File exists error message and the data is kept safe (remains with 222222)
  • the latter shows nothing - the exception is if the v option is used - it shows the tar: numbers/222.txt: skipping existing file message; and the data is kept safe (remains with 222222). Useful for script purposes.

Until here, all is fine.

After to do a research I found the --keep-newer-files option, and according again with the tar(1) - Linux man page, it indicates:

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

Therefore for the execution of the following command:

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

happens the following:

  • appears the tar: Current ‘numbers/222.txt’ is newer or same age message and the data is kept safe (remains with 222222)

Practically this --keep-newer-files option does the same than --skip-old-files about to avoid overriding but showing a different message.

Question

  • When is mandatory use the --keep-newer-files option for the tar command over the --keep-old-files and --skip-old-files options?

I want to know what is the specific scenario where this options is mandatory.

1
  • "Practically this --keep-newer-files option does the same than --skip-old-files" - no, it only skips files that are newer. --skip-old-files skips them irrespective of whether the existing file's modification date is newer than the one in the archive.
    – muru
    Nov 16, 2022 at 2:49

1 Answer 1

2

--keep-newer-files is useful if you want to keep changes made on the target after the source files were last modified, and replace anything older on the target with newer versions from the source.

To illustrate the difference between the two options, you need another piece of information, the timestamp of each file. Consider the following:

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

Those files are copied to target preserving their timestamp.

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

Now you fix 001.txt on the source (observe the t2 part):

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

Someone also edits 002.txt on the target (observe the t3 part):

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

You create a new archive, and extract it on the target:

  • without any option, all files are extracted, so the target ends up with the same contents in 001.txt and 002.txt as on the source, losing the changes made to 002.txt on the target;
  • with --keep-old-files, 001.txt and 002.txt aren’t extracted, and the target is left with its outdated version of 001.txt;
  • with --keep-newer-files, 001.txt is extracted and overwrites the existing target file, because the existing file is older than the file in the archive, but 002.txt is not extracted, because the existing file is newer than the file in the archive.
2
  • Huge thanks for the detailed explanation. Nov 16, 2022 at 12:58
  • When a file has or keeps the same timestamp, and therefore is not overridden, appears the is newer or same age message, it can be confuse, I think is better is same age. Nov 16, 2022 at 14:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .