gzip Command Cheat Sheet
gzip (GNU zip) reduces the size of named files using Lempel-Ziv coding (LZ77). Included in almost every Linux distribution, it is the standard for single-file compression.
Synopsis
gzip [options] [file...]
gunzip [file...]
Basic Usage
Compress a File
gzip bigfile.txt
bigfile.txt -> bigfile.txt.gz.
- Replaces the original file.
- Preserves timestamp, mode, and ownership.
Decompress a File
gzip -d bigfile.txt.gz
# OR
gunzip bigfile.txt.gz
bigfile.txt.gz -> bigfile.txt.
- Removes the .gz file.
Keep Original File (-k)
If you don't want to delete the source file.
gzip -k bigfile.txt
bigfile.txt AND bigfile.txt.gz both exist.
Compression Levels
Trade off between speed and size.
| Flag | Meaning | Speed | Ratio |
|---|---|---|---|
-1 (--fast) |
Fastest compression | Very Fast | Lower |
-6 |
Default | Balanced | Good |
-9 (--best) |
Best compression | Slow | Best |
gzip -9 backup.iso
Advanced Usage
Recursive Compression (-r)
⚠️ Gotcha Alert: gzip -r folder does NOT create a single folder.gz archive (like zip or tar). Instead, it recursively descends into the directory and compresses every single file individually.
gzip -r my_logs/
my_logs/access.log.gz, my_logs/error.log.gz, etc.
To create a single archive, use tar first:
tar -czf my_logs.tar.gz my_logs/
Verbose Output (-v)
See how much space you saved.
gzip -v file.txt
# Output: file.txt: 65.4% -- replaced with file.txt.gz
Test Integrity (-t)
Check if a compressed file is corrupted.
gzip -t archive.gz
View Details (-l)
List compression info without decompressing.
gzip -l archive.gz
compressed uncompressed ratio uncompressed_name
1024 4096 75.0% archive
Piping and Redirection
gzip works great with standard streams (stdin / stdout).
Compress Stream to File
cat database.sql | gzip > database.sql.gz
Decompress Stream to Pipe
gunzip -c log.gz | grep "Error"
Force Output to Stdout (-c)
Useful for keeping the original file without using -k (on older systems) or redirecting elsewhere.
gzip -c file.txt > /backup/file.txt.gz
The "z" Tools using zlib
You don't always need to decompress files to read them.
| Command | Usage |
|---|---|
zcat |
Print contents of gz (like cat) |
zless / zmore |
Page through gz (like less) |
zgrep |
Search inside gz (like grep) |
zdiff |
Compare gz files |
# Search logs without extracting
zgrep "404 Not Found" access.log.gz
Concatenation
Gzip files can be concatenated.
gzip -c part1 > full.gz
gzip -c part2 >> full.gz
gunzip full.gz, it decompresses part1 followed immediately by part2.
Comparison
| Tool | Algorithm | Speed | Ratio | Use Case |
|---|---|---|---|---|
gzip |
DEFLATE | Fast | Good | General, Logs, Web |
bzip2 |
Burrows-Wheeler | Slow | Better | Archived code |
xz |
LZMA2 | Very Slow | Best | Distribution packages |
pigz |
Parallel Gzip | Very Fast | Good | Multi-core systems |
Tip: If you have a multi-core CPU and compress large files, install pigz and use it exactly like gzip.
Exit Status
| Code | Meaning |
|---|---|
| 0 | Success |
| 1 | Error |
| 2 | Warning |
Notes
- Suffix: Default is
.gz.gziprefuses to compress files that already have this suffix. - Force: Use
-fto overwrite existing output files or compress symlinks.