gzip
Overview
gzip compresses or decompresses files using the DEFLATE algorithm (.gz). It is the most universal compression tool on Unix-like systems — logs, tarballs (tar.gz / .tgz), and HTTP content-encoding all speak gzip. Default behavior replaces the input file with file.gz (use -k / --keep to retain the original on modern gzip).
For better speed/ratio tradeoffs on new pipelines, consider zstd. For maximum ratio on cold archives, consider xz. Keep gzip for compatibility.
Syntax
gzip [options] [file...]
gunzip [options] [file...]
gzcat / zcat [file...] # decompress to stdoutCommon Options
| Option | Description |
|---|---|
-c, --stdout |
Write to stdout; keep inputs |
-d, --decompress |
Decompress (same as gunzip) |
-k, --keep |
Keep input files |
-f, --force |
Force overwrite / compress links |
-n / -N |
Don’t save / do save original name & time |
-q |
Quiet |
-r |
Recursive directories |
-t |
Test integrity |
-v |
Verbose |
-1 … -9 |
Fastest … best compression (default 6) |
-l |
List compressed size / ratio for .gz files |
Examples with Explanations
Compress / decompress
gzip file.txt # creates file.txt.gz, removes file.txt
gzip -k file.txt # keep original
gunzip file.txt.gz
gzip -d file.txt.gz # samestdout pipelines
gzip -c file.txt > file.txt.gz
gunzip -c file.txt.gz > file.txt
tar cf - dir | gzip -9 > dir.tar.gz
gzip -dc dir.tar.gz | tar xf -Level tradeoffs
gzip -1 fast.log # CPU cheap
gzip -9 archive.dat # smaller, slowerTest and list
gzip -t file.txt.gz
gzip -l *.gzRecursive logs
gzip -r /var/log/old/ # careful: compresses in place
find /var/log -name '*.log' -mtime +7 -exec gzip -9 {} +View without extracting
zcat file.txt.gz | less
zgrep 'ERROR' app.log.gz
zless app.log.gzIntegrity in scripts
if gzip -t backup.gz 2>/dev/null; then
echo ok
fiNotes / Pitfalls
- Without
-k, the source file is removed after successful compression. - Compressing already-compressed data (jpeg, mp4,
.gz) wastes CPU and may grow size. gzip -ris easy to over-apply on system directories — scope withfind.- Concatenated
.gzmembers are valid; some tools only read the first. - Sparse files and special files: stick to regular files.
2026-relevant notes
- Still the interchange default for tarballs and many APIs.
- Prefer
zstdfor local high-volume logs when all consumers support it. pigzprovides parallel gzip when you need multi-core compression of large streams.
Additional Resources
man gzip