8

I have a directory structure that looks something like this:

/var/www/website/index.php
/var/www/website/home.php
/var/www/website/whatever.text
/var/www/website/.htaccess
/var/www/website/images/
/var/www/website/images/image1.jpg
/var/www/website/images/image2.jpg

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?

1 Answer 1

20

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
1
  • 1
    Ah I've used the -C option for untarring in the past. Didn't think to try it with tarring as well. Thanks! Commented Aug 9, 2012 at 17:11

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.