tar a directory

6

I'm trying to tar my website files that are on my test server to be able to upload them to 'real' server.

tar -cf mysite.tar /var/www

It creates archive fine... but when I open that archive it has var directory, which contains www directory... How do I create archive that has /var/www content in its root, not in subfolders like it's right now?

mea

Posted 2010-02-01T04:36:11.693

Reputation:

Answers

2

I think you are wanting all the files and folders that are in the www folder. If I am misunderstanding you im sorry. If that is what you want you could cd into the www directory and run

tar -cvf backup.tar *

Justin S

Posted 2010-02-01T04:36:11.693

Reputation: 351

1Keep in mind that globbing like this will not get file's which name begins with a dot. – John T – 2010-02-01T05:34:49.703

How do I get 'dot' files included too? – None – 2010-02-01T05:55:20.093

Im not good enough with regular expression to figure this one out. I tried something like

tar -cvf backup.tar .??*

but that backs up only the hidden files. – Justin S – 2010-02-01T07:17:55.940

@mea, do you want to preserve the directory structure inside the /var/www folder? – John T – 2010-02-02T17:48:57.633

@mea. John T got it right. You're asking for trouble when using * with tar. Often it may not matter, but when it does, you probably won't even notice the missing "dot" files (and directories!) until much later. Consider accepting this instead: http://superuser.com/questions/103170/tar-a-directory/103177#103177

– Jonik – 2010-02-14T00:40:33.933

Use "." instead of "*". That gets the entire content of the current directory – mpez0 – 2010-02-14T01:48:00.660

Also, you can put the cd together with the tar: {cd /var/www; tar -cvf backup.tar .} – mpez0 – 2010-02-14T01:48:47.413

8

You can use the -C (capital c) option to make tar change to a directory before it begins it's work. This will effectively remove the directory parents from the archive.

Your other option is to simply run the command from inside the /var/www directory itself and avoid this altogether.

John T

Posted 2010-02-01T04:36:11.693

Reputation: 149 037