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.
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).
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 usegzip
to automatically create a compressed archive if given appropriate flags. For example:This creates (
-c
) a gzip-compressed (-z
) archive calledpublic_html.tar.gz
containing the contents of the specifiedpublic_html
directory.Just run
gunzip
on all the files. E.g:Note that you can also simply use the
zip
command, which will create a single compressed archive.gzip will not combine files.. It is not like zip. You need to use tar with gzip
or
gzip
is just a compressor, not a file archiver. I know, it's weird when you're coming from the world ofpkzip
, 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:Which will give you an uncompressed archive. You can then compress that with
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:
and you can uncompress with
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-betterxz
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.