Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
    
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 Aug 18 '10 at 19:27
    
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 Aug 18 '10 at 20:22

2 Answers 2

up vote 1 down vote accepted

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
share|improve this answer
    
@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 Aug 18 '10 at 19:36
    
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 Aug 18 '10 at 20:05
    
@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 Aug 18 '10 at 20:32
    
@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 Aug 18 '10 at 20:33
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 Aug 18 '10 at 20:34

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.