gzip compression
Updated November 1, 2026
gzip compresses a single file, gunzip expands it. tar does not compress on its own, so the pair is joined into .tar.gz.
gzip -k file # compress, -k keeps the original
gunzip file.gz # expand
gzip -t a.tar.gz # check it is valid gzip
gzip -l a.tar.gz # sizes and compression ratioSample output
$ ls -l numbers.txt
-rw-r--r-- 1 student student 108894 Jul 26 03:48 numbers.txt
$ gzip numbers.txt
$ ls -l
total 44
-rw-r--r-- 1 student student 45016 Jul 26 03:48 numbers.txt.gz
$ gzip -l numbers.txt.gz
compressed uncompressed ratio uncompressed_name
45016 108894 58.7% numbers.txtgzip -t checks integrity against the checksum inside the archive. The ratio depends on the data: text compresses well, already-compressed data (images, video) barely at all.
Common mistakes
gzip will not take a directory: gzip: d is a directory -- ignored, exit code 2. The -r flag looks like the fix, but it compresses each file inside separately and leaves d/f.txt.gz, d/g.txt.gz instead of one archive. Only tar packs a directory into a single file, which is why the pair is written as tar czf.
gzip leaves an existing .gz alone: gzip: numbers.txt.gz already exists; not overwritten, exit code 2. You hit this right after gzip -k: the original is still there, and a second attempt to compress it runs into the archive that already exists. Either delete the old .gz or write to another name with gzip -c numbers.txt > other.gz.
gzip -t names the reason a file is broken. A truncated download gives gzip: broken.gz: unexpected end of file; a flipped byte in the middle gives gzip: c.gz: invalid compressed data--crc error. Both exit with 1. Neither ls -l nor the file size tells you anything about damage: the checksum sits inside the archive and only decompression reads it.
Used in
You need this in the lesson Archives: tar and gzip.