I was planning to do a quick backup of my website. So I tried to run the following command in my webroot:

tar cvfz backup.tar.gz .

That seemed to be working nicely for a while. Until I discovered it had started to backup backup.tar.gz...

Is there an easy way that I can get it to ignore the file I am archiving to?

Note: I would of course have put the archive file in the directory above it if I could. But I only have access to my home directory.

  • What about access to /tmp? That's always been world writable on every machine I've ever touched. – Doug Harris Dec 20 '09 at 20:43
  • That could be. But that is also readable by everyone, isn't it? – Svish Dec 21 '09 at 17:04


tar cvfz backup.tar.gz *

This way shell extends the *, vs the tar reading current directory. The difference is that the produced archive does not contain root folder.

If you also need to include hidden files (.files) you can try

tar cvfz backup.tar.gz * .??*

This includes .files and prevents inclusion of parent directories.

  • Interesting. What does .??* do exactly? – Svish Dec 20 '09 at 13:25
  • ? is a 'any character' when the * is 'any character sequence'. Shell does not normally include .files with *, so .?? is simply any file beginning with a dot and having two or more characters after the dot, thus preventing inclusion of . and .. – Ahe Dec 20 '09 at 13:35
  • 2
    .??* of course has a problem, it does not include .a and so on, but it is faster to write than .[^.]* and files with a dot and one character are rare. – Ahe Dec 20 '09 at 13:39
  • Ahaa. Good stuff. Seems to be working nicely :) – Svish Dec 20 '09 at 14:01
  • To expand on this a bit, add a ./ before the *. This makes it possible to add --exclude statements that are relative to the root path of the tar command. tar cvfz backup.tar.gz ./* --exclude "./relative/path" – kirkmadera Apr 18 '17 at 20:35

b=backup.tar.gz; tar --exclude=$b -zcf $b .

  • 2
    A little explanation of what's going on here and how your answer differs from the others already offered will greatly improve this answer. – music2myear Jun 6 '17 at 23:37

I'm sure there's a nicer way, but I always just do it from the a different directory.

tar cvfz backup.tar.gz /path/to/www

This link looks like it has the information you might want.

  • Yeah, but I can't do that since that directory is the only one I have access to. Added note about that to my question :) – Svish Dec 20 '09 at 13:17
  • the --exclude= option looks viable, as well. – mrduclaw Dec 20 '09 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.