How do I gunzip a directory?

I have a gzipped directory called "new" which contains other directories and files, that I compressed in the following way

gzip -cvr --no-name /path-to-directory/new > new.gz

I have copied this >700MB to another location (on a different server) and would now like to unpack it. So, on the new server I go to the directory has "new" in it and I use

gzip new

The result is a single 880MB file - no directories or smaller files.

What did I do wrong and how can I correct this?

Thanks.


Solution 1:

Gzip compresses files, not directories. If you want to gzip a directory, you must first create a tarball. You can create a tarball and compress it in one step.

tar cvzf tarball.tar.gz directory/

(create, verbose, gZip, to this file)

to reverse the process

tar xvzf tarball.tar.gz

(eXtract)

Solution 2:

tar cf - directory | gzip -9c > foo.tar.gz

Then copy this as before, then do the reverse:

tar xzf foo.tar.gz

If you're using SSH to transfer the files, you can skip the intermediate step of having a file to store and transfer:

tar cf - directory | gzip -9c | ssh user@hostname "cd /destdir ; tar xzvf -"

This is especially helpful if your resulting tar file is very large and might not fit on the destination partition along with the uncompressed data. In this case, using rsync might be better anyway.

Solution 3:

It sounds like you should have used tar (with gzip option):

# -z  : pipe through gzip
# -cf : create file
tar -zcf new.tgz /path-to-directory

then with the resulting new.tgz file:

# z : gzip
# x : extract
# p : keep permissions
tar zxpf new.tgz

EDIT

What's the point of gzip -r then? – jeffp 37 mins ago

gzip is a stream compressor - it doesn't really look at the contents of what you are compressing beyond what it needs to to in order to make it smaller so it has no concept of directories.

Just as a side note, I find bzip2 to do a much better job of compression than gzip - albeit slower.

# -j  : pipe through bzip2
# -cf : create file
tar -jcf new.tar.bzip2 /path-to-directory

bunzip2 new.tar.bzip2