Does tar -xvf remove existing files from directory?

10

I have a tar archive and I want to use tar -xvf.
In this tar archive I have a directory tree containing a couple of files. On my solaris 10 system this directory tree already exists and has several files in it.
If i unpack the archive is the entire directory tree overwritten or are only the files contained in the archive overwritten?
Will the files that do not have a correspondent in he archive get deleted?

Felicia

Posted 2010-08-18T19:14:57.567

Reputation: 103

You could always create a test .tar and a test directory and try it out... an answer you come up with yourself will last much longer in your mind than an answer given to you by anyone here. – Jarvin – 2010-08-18T19:27:07.117

The answer is no. If you think hard enough, you will realize that it doesn't make any sense that a program to extract files from an archive would delete completely unrelated files. – Fosco – 2010-08-18T20:22:41.367

Answers

5

Duplicate files that already exist may or may not be unpacked and overwritten depending on your system. However, it usually does by default. Files that are in the archive but not on your system will simply be added to new or existing directories on your system.

Dir /test

/1/a
c

Archive test.tar

/1/b
d

will probably be merged to:

/1/a
/1/b
c
d

Adding the -k flag to the tar command will make sure none of the files on your target directory will be overwritten:

tar -xvkf test.tar

BloodPhilia

Posted 2010-08-18T19:14:57.567

Reputation: 27 374

@BP: Overwrite may be shell dependent as csh (still default shell on Sun?) had a "noclobber" shell variable which when set prevents accidental overwrite behaviour. – hotei – 2010-08-18T19:36:18.373

Thanks BloodPhilia and hotei. I have made a test on my system and tar -xvf overwrites the files that already exist and adds the ones that do not exist. What I would like to know is if there is any possibility that an entire directory should be overwritten and thus lose the files that were on my system in that directory but were not in the archive? For example: I have in dir.tar dir/file2. On my system I have dir/file1, dir/file2 and dir/file3. Is it possible that if I perform tar -xvf dir.tar the entire dir will be overwritten and will no longer contain file1 and file3? – Felicia – 2010-08-18T20:05:42.280

@hotei: noclobber (which also exists in POSIX shells btw) only affects files redirected to with >foo in the shell. It has no effect on tar. – Gilles 'SO- stop being evil' – 2010-08-18T20:32:34.470

@hotei noclobber prevents you from overwriting files through redirection in the shell. So echo foo > bar.txt will not overwrite bar.txt if it exists. It has no effect on what a program, such as tar, will overwrite. – KeithB – 2010-08-18T20:33:54.633

1@Felicia: According to Single Unix, tar will not erase files that were not in the archive. It will overwrite existing files with the contents (but not the modes) from the archive. There may be non-compliant tars around, but I doubt any of them would overwrite unrelated files such as dir/file1. – Gilles 'SO- stop being evil' – 2010-08-18T20:34:55.183

@Gilles: Your comment is very helpful. Thanks a lot. – Felicia – 2010-08-18T21:04:42.873

0

Easiest way is to make a new, empty directory, cd to it, and extract the files there. You need to be careful though that the extracted files are relative (begin with ./) and not absolute (begin with /). Pipe the table of contents through less to see which is the case if you don't know (tar -tvf tar_filename | less)

You can find out if your tar version overwrites by default or not by looking at the man page. Tar will not delete files during extraction, which is another reason people usually start from a new, empty directory before doing the extract.

hotei

Posted 2010-08-18T19:14:57.567

Reputation: 3 645