File Compression in Linux
Linux has multiple compression tools because different situations have different requirements: some prioritize compression ratio, others prioritize speed, and zip exists for cross-platform compatibility with Windows systems. This article covers the four tools you will actually use as a Ubuntu administrator and when to reach for each one.
Compression tools overview
| Tool | Extension | Speed | Ratio | Cross-platform | Best for |
|---|---|---|---|---|---|
| gzip | .gz | Fast | Good | Linux/Mac | Most Linux tasks (default) |
| bzip2 | .bz2 | Slow | Better | Linux/Mac | Smaller backups, text-heavy data |
| xz | .xz | Very slow | Best | Linux/Mac | Distribution packages, archives |
| zip | .zip | Fast | OK | Universal | Sending files to Windows users |
Important: gzip, bzip2, and xz compress single files. To compress a directory, use tar to bundle the directory first, then compress. zip handles directories directly.
gzip
gzip is the default compression tool on Linux. It is built into every Ubuntu installation and is what tar -z uses internally.
# Compress a file (replaces original with file.gz)
gzip largefile.log
# Compress and keep the original (do not delete source)
gzip -k largefile.log
# Set compression level: -1 (fastest) to -9 (best compression)
gzip -9 largefile.log # Maximum compression
gzip -1 largefile.log # Fastest compression
# Decompress a .gz file
gunzip largefile.log.gz
# or:
gzip -d largefile.log.gz
# View a compressed file without decompressing
zcat largefile.log.gz | head -20
zless largefile.log.gz # Scroll through
# Search inside a compressed file
zgrep "error" largefile.log.gz
# Check compression ratio and integrity
gzip -v largefile.log.gz # Shows compression percentage
Example output
largefile.log.gz: OK
largefile.log: 87.3% -- replaced with largefile.log.gz
bzip2
bzip2 produces smaller archives than gzip but takes longer. The command syntax is nearly identical to gzip.
# Compress a file
bzip2 database-dump.sql
# Compress and keep original
bzip2 -k database-dump.sql
# Decompress
bunzip2 database-dump.sql.bz2
# or:
bzip2 -d database-dump.sql.bz2
# View without decompressing
bzcat database-dump.sql.bz2 | head -5
# Search inside a bzip2 file
bzgrep "INSERT" database-dump.sql.bz2 | head -5
xz
xz provides the best compression ratio of the three tools. Ubuntu and many Linux distributions use xz for distributing packages and disk images because the smaller size matters more than the time to compress once.
# Compress a file
xz database-dump.sql
# Compress with maximum ratio (use for archives you only compress once)
xz -9 database-dump.sql
# Compress keeping original
xz -k database-dump.sql
# Decompress
unxz database-dump.sql.xz
# or:
xz -d database-dump.sql.xz
# View without decompressing
xzcat database-dump.sql.xz | head -5
# Show compression statistics
xz -v database-dump.sql
Example comparison on a 50 MB SQL dump
database-dump.sql 50.0 MB original
database-dump.sql.gz 12.3 MB gzip -6 (default)
database-dump.sql.bz2 10.1 MB bzip2
database-dump.sql.xz 8.7 MB xz
zip (cross-platform)
Use zip when you need to share files with Windows users or when the recipient might not have tar. zip handles directories directly without needing tar first.
# Install if not present
sudo apt install zip unzip
# Compress a directory into a zip archive
zip -r website-backup.zip /var/www/html/
# Add specific files to a zip
zip configs.zip /etc/nginx/nginx.conf /etc/nginx/sites-available/default
# Set compression level (-0 = no compression, -9 = max)
zip -9 -r website-backup.zip /var/www/html/
# List contents of a zip archive
unzip -l website-backup.zip
# Extract to current directory
unzip website-backup.zip
# Extract to specific directory
unzip website-backup.zip -d /tmp/restore/
# Extract a single file from the archive
unzip website-backup.zip var/www/html/index.php
Choosing the right tool
Need to compress a file or directory?
│
▼
Does it go to a Windows user?
YES ──────► zip -r archive.zip directory/
NO
│
▼
Is compression ratio the top priority?
YES ──────► xz -9 file (or tar -cJf archive.tar.xz dir/)
NO
│
▼
Is speed more important than size?
YES ──────► gzip -1 file (or tar -czf archive.tar.gz dir/)
NO
│
▼
Default: gzip (tar -czf archive.tar.gz dir/)In practice, gzip with tar covers 90% of Linux compression tasks. Use xz for long-term archives or distribution packages where you compress once and download many times. Use zip only when Windows compatibility is needed.
Conclusion
gzip is the default for speed and compatibility, bzip2 and xz trade time for better compression, and zip exists for Windows interoperability. For directories, always use tar with a compression flag (-z, -j, -J) rather than compressing with gzip/bzip2 directly. The z* variants (zcat, zgrep, bzcat, xzcat) let you work with compressed files directly without decompressing them first.
FAQ
Is File Compression in Linux important for Ubuntu administrators?+
Yes. It supports practical Ubuntu administration because it connects directly to server reliability, security, troubleshooting, or daily operations.
Should I practice this on a live server?+
Use a lab VM first. After you understand the command output and rollback path, apply the workflow carefully on real systems.
What should I do after reading this article?+
Run the practice commands, write down what each one shows, and continue to the next article in the Ubuntu roadmap.
Need help with Ubuntu administration?
Work directly with Muhammad Irfan Aslam for Ubuntu Server, Linux, cloud, Docker, DevOps, CI/CD, or infrastructure troubleshooting support.
Hire Me for Support