Absolute recursive tar without parent directories?

Jake Wilson asked:

I have a directory structure that looks something like this:


I want to tar the website directory recursively, but I don’t want to include the parent structure. If I do this:

tar -zcvf /tmp/mytar.tar.gz /var/www/website/*

Then all the files have the entire /var/www/website/ parent structure in the tar file. The only way I can do what I want is to:

cd /var/www/website
tar -zcvf /tmp/mytar.tar.gz *

That way, there is no parent directory structure in the tar file.

Is it possible accomplish what I need without having to cd into the directory first?

My answer:

GNU tar has a -C option for this.

-C, --directory=DIR
    change to directory DIR

So you could do something like:

tar -C /var/www/website -zcvf /tmp/mytar.tar.gz .

Untarring (you will do that eventually) is the same:

tar -C /var/www/website -zxvf /tmp/mytar.tar.gz

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.