9

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.

3 Answers 3

21

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)

3
  • What's the point of gzip -r then?
    – chaimp
    Nov 24, 2009 at 18:14
  • @jeffp: gzip -r will descend into directories and gzip all files in them. it won't create a single gzip file containing the contents of those directories. eg, if you have "dir1" containing "foo" and "bar", and "dir2" containing "baz", and do gzip -r dir1 dir2, you'll end up with: "dir1/foo.gz" "dir1/bar.gz" "dir2/baz.gz" Nov 24, 2009 at 18:51
  • @jeffp: note the results of your command on my example: gzip -cvr --no-names dir* > new.gz ... now "new.gz" contains the same thing that cat dir1/foo.gz dir1/bar.gz dir2/baz.gz > new.gz. it's just 3 files worth of compressed data clumped together. there's nothing to tell the decompressor about individual files. use tar. Nov 24, 2009 at 18:59
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.

1

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

You must log in to answer this question.

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