13

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.

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

3 Answers 3

9

Try

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.

5
  • 1
    Interesting. What does .??* do exactly?
    – Svish
    Dec 20, 2009 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, 2009 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, 2009 at 13:39
  • Ahaa. Good stuff. Seems to be working nicely :)
    – Svish
    Dec 20, 2009 at 14:01
  • 1
    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, 2017 at 20:35
7

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

2
  • 3
    A little explanation of what's going on here and how your answer differs from the others already offered will greatly improve this answer. Jun 6, 2017 at 23:37
  • 2
    That does not work when you create the file the first time (tested with tar 1.3 on both Debian and Ubuntu). So do: b=backup.tar.gz; touch $b; tar --exclude=$b -zcf $b .
    – rmuller
    Dec 12, 2020 at 10:21
1

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.

2
  • 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, 2009 at 13:17
  • the --exclude= option looks viable, as well.
    – mrduclaw
    Dec 20, 2009 at 13:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .