Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

How can you extract only the target dir and not the complete dir tree?

compress

tar cf /var/www/site.tar /var/www_bak/site

extract

tar xf /var/www/site.tar -C /tmp

This will produce:

/tmp/var/www/site

How is it possible to avoid the whole dir tree to be created when the file is extracted?

What I want it to extract to:

/tmp/site
share|improve this question

2 Answers 2

up vote 3 down vote accepted

Why not use -C option when creating:

$ tar cf /var/www/site.tar -C /var/www_bak/ site
share|improve this answer
    
TMTOWTDI. That would work too, using the same -C option ;) –  MikeyB Nov 11 '11 at 17:27

You want to use the --strip-components=NUMBER option of tar:

 --strip-components=NUMBER
       strip NUMBER leading components from file names on extraction

Your command would be:

tar xfz /var/www/site.gz --strip-components=2 -C /tmp
share|improve this answer
3  
This answer is better because you do not have to think about it before the tar creation. You can use it with an already created tar file. Thanks @MikeyB ! –  Fabien Quatravaux Mar 25 at 8:39
    
This worked like a charm! Thanks! –  Marcello de Sales Oct 7 at 18:16

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.