Centos – Attempted gzip of directory applied gzip to each file individually

centosgzip

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).

Best Answer

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.