Take the 2-minute tour ×

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
add comment

2 Answers

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
add comment

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 at 8:39
add comment

Your Answer


