Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


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


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

This will produce:


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:

share|improve this question
up vote 15 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
He wants to strip the path from some leading components, not change the output directory. See below for the correct answer. – aymericbeaumet Jul 28 '15 at 13:50

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

       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
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 '14 at 8:39
This worked like a charm! Thanks! – Marcello de Sales Oct 7 '14 at 18:16
I'm pleased to report this works both in GNU tar and BSD tar. – Nathan Osman Jun 20 at 22:07

Your Answer


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.