Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extracting from a tar file with duplicate entry names

Tags:

bash

tar

If you add the same file to a tar file multiple times, and then extract that file, are you guaranteed that the extracted version is equivalent to the one which was last added?

~/tmp> echo hi > foo
~/tmp> tar -cf bar.tar foo
~/tmp> echo bye > foo
~/tmp> tar -uf bar.tar foo 
~/tmp> tar -tf bar.tar
foo
foo
~/tmp> rm foo
~/tmp> tar -xf bar.tar foo
~/tmp> cat foo
bye

It make sense that it would, but I can't find any documentation to that effect. I'm wondering if anyone in the know might know, or if someone knows a circumstance where this is not true?

like image 732
John Avatar asked Oct 27 '15 18:10

John


People also ask

How do I extract data from a tar file?

To extract (unzip) a tar.gz file simply right-click on the file you want to extract and select “Extract”. Windows users will need a tool named 7zip to extract tar.gz files. The -v option will make the tar command more visible and print the names of the files being extracted on the terminal.

How can I view the contents of a tar file without extracting it?

With the tar command, you can use -t to view the contents of tar. gz files with the list of details. The -t switch is used to list the contents of the tar. gz file without actually extracting it.


1 Answers

Some documentation for tar says this:

When you extract a file from the archive, only the version stored last will wind up in the file system. Because '--extract' ('-x') extracts files from an archive in sequence, and overwrites files with the same name in the file system, if a file name appears more than once in an archive the last version of the file will overwrite the previous versions which have just been extracted. You should avoid storing older versions of a file later in the archive.

My understanding is that, unless you use the --keep-old-files option, -x will always overwrite with the last file stored in the archive.

Edit: see also the GNU documentation for tar.

like image 65
miken32 Avatar answered Oct 11 '22 00:10

miken32