linux · difficulty ◆
gzip — compress and decompress files
Make files smaller, save space, ship them faster.
Almost every file you have ever downloaded traveled in gzip form — yet gzip is also the tool that once shipped a 300% speedup by deleting a single line of code.
$ gzipWhat it does
gzip compresses a single file using the DEFLATE algorithm (LZ77 plus Huffman coding). It replaces the file with a compressed copy ending in .gz — you usually pair it with tar to compress whole directory trees.
Why it matters
Almost every download you grab is gzip-compressed at some layer: .tar.gz source archives, HTTP Content-Encoding, and system logs that rotate into .gz files. Knowing gzip means you can inspect and shrink files anywhere on a Unix box.
Example
$ gzip notes.txt$ ls
notes.txt.gz
$ gzip -l notes.txt.gz
compressed uncompressed ratio uncompressed_name
420 1024 59.0% notes.txtCompresses notes.txt in place, leaving notes.txt.gz. -l lists the compression ratio.
$ gzip -d notes.txt.gz$ ls
notes.txt
$ cat notes.txt
hello from the terminal-d decompresses, restoring the original file. You can also use gunzip.
Common flags
- -d
- decompress instead of compress (alias: gunzip)
- -k
- keep the original file after compressing/decompressing
- -c
- write to stdout instead of a file, so you can pipe it
- -l
- list compression ratio and sizes for a .gz file
- -t
- test the integrity of a compressed file
- -1 .. -9
- set compression level, -1 fastest, -9 best ratio (default -6)
History
Origin
gzip was released by Jean-loup Gailly and Mark Adler in 1992, replacing the older compress tool whose LZW compression was patented. Its DEFLATE format became the foundation for the PNG image format and zlib.
Fun fact
The name stands for 'GNU zip'. GNU tar can pipe straight into gzip, which is why '.tar.gz' archives are sometimes whimsically called 'tarballs'.
Fun facts
Pros & cons
pros
- + Fast compression and near-universal decompression support
- + Lossless — original bytes are always recoverable exactly
- + The default everywhere: HTTP Content-Encoding, .tar.gz, rotated logs
- + Levels let you trade speed for ratio on a per-file basis
cons
- − Weakest ratio of the common modern compressors (xz, zstd, bzip2 beat it)
- − Compresses a single file only — you must pair with tar for trees
- − Old LZW-derived tools had patent issues that gzip was built to escape
Takeaways
- 1Compress: `gzip -k file.txt` keeps the original; plain `gzip` replaces it
- 2Decompress: `gunzip file.gz` or `gzip -d`
- 3Inspect a .gz without extracting: `gzip -l file.gz` shows the ratio
- 4Pipe compressed output: `cat file | gzip -c > file.gz`
- 5Reach for xz or zstd when you need a smaller file and speed matters less