2

I tried to gzip a directory with the following command:

gzip -r /home/path/to/backups/mydirectory.gz /home/site/public_html/

I was expecting to see a .gz file show up inside ...backups containing all the files found in ...public_html. This isn't what happened. Instead, every file was (recursively) renamed to [filename].gz within public_html. This, obviously, brought an entire site down.

alt text

Two questions: 1) What did I do wrong with the syntax? and 2) How can I revert all of the filenames back to what they were before (exactly what they are now, minus the .gz extension).

1
  • More worryingly, if anyone got a directory index and noticed you'd just gzip'd every file individually, they would have downloaded your wp-config.php.gz and will merrily have a copy of your DB credentials. You should change your passwords, just to be on the safe side of things.
    – Jay
    Oct 5, 2012 at 21:55

3 Answers 3

9

Two questions: 1) What did I do wrong with the syntax?

Gzip only compresses individual files; it's not an archiving tool. It's usually used in combination with something like tar. In fact, some versions of tar will use gzip to automatically create a compressed archive if given appropriate flags. For example:

tar -cvz -f public_html.tar.gz /home/site/public_html/

This creates (-c) a gzip-compressed (-z) archive called public_html.tar.gz containing the contents of the specified public_html directory.

2) How can I revert all of the filenames back to what they were before (exactly what they are now, minus the .gz extension).

Just run gunzip on all the files. E.g:

gunzip -r /home/site/public_html/

Note that you can also simply use the zip command, which will create a single compressed archive.

3
  • zip doesn't appear to be available on my server. Other than that, your answer was thorough and spot-on. Thank you very much. I suppose I misread the instructions on this site regarding gzip: "The above command compresses the contents of the /school directory and places them in a file named filename.gz."
    – Sampson
    Jan 21, 2011 at 20:43
  • Nope, that document is just wrong.
    – larsks
    Jan 21, 2011 at 20:50
  • 1
    You mean there's wrong stuff on the internet!? Hehe. Thank you for clearing up the confusion.
    – Sampson
    Jan 21, 2011 at 20:52
1

gzip will not combine files.. It is not like zip. You need to use tar with gzip

tar cf file.tar dir
gzip file.tar

or

tar zcf file.tar.gz dir
0
1

gzip is just a compressor, not a file archiver. I know, it's weird when you're coming from the world of pkzip, which does both.

What you want to use is tar, which does recursive archiving of files into one big file, which you can then compress. Generally, you run tar from the parent of the directory you want to archive, something like this:

tar -cvf archive-name.tar  directory-to-store/

Which will give you an uncompressed archive. You can then compress that with

gzip archive-name.tar

Which will give you archive-name.tar.gz.

If you are using GNU tar (and you probably are), you can combine these into one step by adding the z flag:

tar -czvf archive-name.tar.gz directory-to-store/

and you can uncompress with

tar -xzvf archive-name.tar.gz

And in fact, with modern versions of GNU tar, no need to give the -z for decompression -- it will auto-detect. This is handy, because GNU tar also supports -j for (better than gzip) bzip2 compression, and (if you have a new enough version) -J for even-better xz compression. Normally, you'd give files made that way .tar.bz2 or .tar.xz extensions, but then it's irritating to have to choose the right flag to uncompress, especially in scripts.

You must log in to answer this question.

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