Tarball Contains... itself?

Whether you are using Linux on the desktop or as a server, it's still good that you're using Linux. Linux related questions go here.

Moderator: General Moderators

Post Reply
RCA86
Forum Commoner
Posts: 32
Joined: Thu Mar 10, 2011 1:03 pm

Tarball Contains... itself?

Post by RCA86 »

Hi guys,

I'm the furthest thing you can imagine from a Linux pro, so this may be a bit of a noob question, but I'm running some backups on one of our servers (we don't have an actual SA), and I've recently encountered an annoying problem. When I run the following command to zip the entire current directory (which is normally httpdocs), it seems to include itself in the tarball: (run in httpdocs directory)

Code: Select all

tar -zcvf backup.tar.gz .
I think this has only started happening since I've been using the '.' at the end, instead of the an '*', as that wouldn't include .htaccess.

Any ideas? Alternatively, and this is a total noob question, can I run it from outside the directory like so:

Code: Select all

tar -zcvf backup.tar.gz httpdocs/.
Would that work?

EDIT: I'm guessing actually just tar -zcvf httpdocs would do the trick...?

Thanks
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Tarball Contains... itself?

Post by requinix »

- The file is created while tar is going about its business. Naturally it'll find that file when it's searching the directory.
- Using * means the "searching" is done by the shell instead. Happens before tar has a chance to create the file and thus it doesn't show up.
- If you specify the directory like that then the tar will contain the httpdocs folder too - not just what it contains.

There are a handful of options but my preferences lean towards what you had before with the wildcard. If you want hidden files then include ".*" in the list.

See also: tar(1)
RCA86
Forum Commoner
Posts: 32
Joined: Thu Mar 10, 2011 1:03 pm

Re: Tarball Contains... itself?

Post by RCA86 »

That was super fast, cheers!

I wasn't sure what you meant, so I tried the following command:

Code: Select all

 tar -zcvf test.tar.gz .*
This started going a bit crazy and adding a lot of files I didn't recognise though, so I'm not sure how exactly that works. Did I misunderstand what you meant?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Tarball Contains... itself?

Post by requinix »

I just realized that .* includes .. too. Try

Code: Select all

.[^.]*
There's something else I'm forgetting that's better but, of course, I'm forgetting it. Maybe piping a file list out from find?
Doug G
Forum Contributor
Posts: 282
Joined: Sun Sep 09, 2007 6:27 pm

Re: Tarball Contains... itself?

Post by Doug G »

With Fedora's tar you can use --exclude=backup.tar.gz to prevent the tarball from including itself.
RCA86
Forum Commoner
Posts: 32
Joined: Thu Mar 10, 2011 1:03 pm

Re: Tarball Contains... itself?

Post by RCA86 »

Thanks again for the replies.

@Requinix: hmm, I think I see what you mean, but that one doesn't work on my system.

@Doug: dang, that would be perfect, but I'm running CentOS, and the exclude command isn't working.

I'll keep fiddling with it, and see if I can't get some solution. I might even just use '*' to select the files, and add .htaccess files to the tarball afterwards. That should work... I think. I'm such a noob. :D
Doug G
Forum Contributor
Posts: 282
Joined: Sun Sep 09, 2007 6:27 pm

Re: Tarball Contains... itself?

Post by Doug G »

From a terminal do man tar to get the exact syntax. CentOS and Fedora are very similar and should support the same syntax for --exclude.
Post Reply