LFCS 20 ๐ง Compression โ gzip, bzip2, xz
Compression reduces the size of a file by encoding its contents more efficiently. On Linux, three compressors dominate: gzip, bzip2, and xz. Each is a general-purpose compressor with a different trade-off between speed and compression ratio. gzip is the fastest and the most widely supported. bzip2 compresses better but is slower. xz compresses the best but is the slowest. The LFCS exam tests compression as part of the “Create and manage archives” objective, alongside tar, and the three compressors are the standard tools .
The compressors share a common design. Each takes a single file as input, produces a compressed file as output, and by default removes the original. The compressed file has a standard extension: .gz for gzip, .bz2 for bzip2, and .xz for xz. Each compressor has a corresponding decompressor: gunzip, bunzip2, and unxz. And each has options for controlling the compression level, for writing to standard output, and for keeping the original file .
The relationship between compression and archiving is important. gzip, bzip2, and xz compress a single file. They do not bundle multiple files into one archive. To compress a directory or a set of files, the standard pattern is to create a tar archive first, then compress it. This is why the common file extensions are .tar.gz, .tar.bz2, and .tar.xz: the .tar indicates the archive, and the second extension indicates the compression. The tar command integrates both steps with the -z, -j, and -J options, but the underlying compressors are the same ones used directly .
This chapter covers three areas. First, why three compressors exist and how they differ โ the speed-versus-ratio trade-off and the use cases for each. Second, how to compress and decompress files โ the basic commands, the default behavior of removing the original, and the options for keeping it, controlling the level, and writing to standard output. Third, how to inspect and verify compressed files โ listing without decompressing, testing integrity, and the interaction with tar. The chapter ends with a complete example session, a quick reference, best practices, common pitfalls, real-world examples, and diagrams showing the compression workflow.
Key point: gzip, bzip2, and xz compress a single file and remove the original by default. gunzip, bunzip2, and unxz decompress. The -k option keeps the original, -c writes to standard output, and -d decompresses. Compression is separate from archiving; use tar to bundle multiple files before compressing .
Why three compressors exist
The speed-versus-ratio trade-off. Compression is a trade-off between how long it takes and how small the result is. gzip uses the DEFLATE algorithm, which is fast and produces good compression. bzip2 uses the Burrows-Wheeler transform, which is slower but produces better compression. xz uses the LZMA2 algorithm, which is the slowest but produces the best compression. The three tools exist because different situations call for different points on the curve. A backup that runs nightly can afford the time for xz. A log rotation that runs hourly should use gzip. A one-time archive of a large dataset might use bzip2 as a middle ground .
The compatibility problem. gzip is the most widely supported. It is available on every Unix-like system, and its format is understood by browsers, web servers, and almost every piece of software that handles compressed data. bzip2 is less universal but still common. xz is the newest and the least widely supported, though it is standard on modern Linux distributions. The choice of compressor is often determined by what the recipient can decompress. A file destined for a system that only has gzip cannot be compressed with xz .
The single-file problem. All three compressors operate on a single file. They do not have a concept of multiple files or directory structure. To compress a directory, the standard pattern is to create a tar archive first, then compress the archive. The tar command automates this with -z, -j, and -J, but the underlying compressors are the same. This separation of concerns โ tar for archiving, compressors for compression โ is why the file extensions are compound: .tar.gz, .tar.bz2, .tar.xz .
The original-removal problem. By default, each compressor removes the original file after compressing it. This is a space-saving behavior: the compressed file replaces the original. But it can be surprising. A user who compresses a file and expects to keep the original will find it gone. The -k option prevents this, and it is the option to remember when the original must be preserved .
The streaming problem. All three compressors can read from standard input and write to standard output. This makes them composable in pipelines. tar -cf - directory/ | gzip > backup.tar.gz creates a gzipped tar archive without an intermediate file. The compressors are designed for streaming, which is why they can be used in pipes with ssh, dd, and other tools .
The trade-off. The three compressors are not interchangeable in every context. gzip is fast but compresses less. xz compresses more but is slow and memory-intensive. bzip2 is a middle ground that is rarely the best choice but is never the worst. The trade-off is between the time to compress, the time to decompress, the size of the result, and the compatibility of the format. For most Linux administration tasks, gzip is the default, bzip2 is chosen when a smaller file is needed, and xz is chosen when the size matters most and the time is available.
a. gzip โ the standard compressor
gzip is the most common compressor on Linux. It replaces a file with a compressed version that has a .gz extension .
gzip file.txt
This creates file.txt.gz and removes file.txt. The original is gone unless -k is used:
gzip -k file.txt
This creates file.txt.gz and keeps file.txt.
The -d option decompresses:
gzip -d file.txt.gz
This restores file.txt and removes file.txt.gz. The gunzip command is equivalent to gzip -d.
The -c option writes to standard output and leaves the original file untouched:
gzip -c file.txt > file.txt.gz
This is the pattern for streaming and for redirecting the output to a different name.
The compression level is controlled with -1 through -9. The default is -6. -1 is the fastest and compresses least. -9 is the slowest and compresses most .
gzip -9 file.txt # maximum compression
gzip -1 file.txt # fastest compression
The -l option lists the compression ratio of a gzipped file without decompressing it:
gzip -l file.txt.gz
The -t option tests the integrity of a gzipped file:
gzip -t file.txt.gz
The -v option adds verbose output, showing the compression ratio.
b. bzip2 โ better compression, slower
bzip2 compresses better than gzip at the cost of speed. It replaces a file with a .bz2 extension .
bzip2 file.txt
This creates file.txt.bz2 and removes file.txt. The -k option keeps the original, the -d option decompresses, and the -c option writes to standard output. The bunzip2 command is equivalent to bzip2 -d.
bzip2 -k file.txt # keep original
bzip2 -d file.txt.bz2 # decompress
bunzip2 file.txt.bz2 # equivalent
The compression level is controlled with -1 through -9. The default is -9, which is the maximum. Unlike gzip, bzip2 defaults to the highest level.
The -t option tests integrity, and the -v option adds verbose output.
c. xz โ best compression, slowest
xz compresses better than both gzip and bzip2, but it is the slowest and uses the most memory. It replaces a file with a .xz extension .
xz file.txt
This creates file.txt.xz and removes file.txt. The -k option keeps the original, the -d option decompresses, and the -c option writes to standard output. The unxz command is equivalent to xz -d.
xz -k file.txt # keep original
xz -d file.txt.xz # decompress
unxz file.txt.xz # equivalent
The compression level is controlled with -1 through -9. The default is -6. The -e option uses a slower, more thorough compression. The -T option enables multithreading, which speeds up compression on multi-core systems .
xz -9 file.txt # maximum compression
xz -T 4 file.txt # 4 threads
xz -e file.txt # extra thorough
The -l option lists information about an xz file, and the -t option tests integrity.
Complete Example Session
# ============================================
# PART 1: BASIC GZIP
# ============================================
gzip file.txt
# Creates file.txt.gz, removes file.txt
gzip -k file.txt
# Creates file.txt.gz, keeps file.txt
# ============================================
# PART 2: DECOMPRESS WITH GZIP
# ============================================
gzip -d file.txt.gz
# Restores file.txt, removes file.txt.gz
gunzip file.txt.gz
# Equivalent
# ============================================
# PART 3: COMPRESSION LEVELS
# ============================================
gzip -9 file.txt # maximum compression, slowest
gzip -1 file.txt # fastest, least compression
gzip -6 file.txt # default
# ============================================
# PART 4: STDOUT AND STREAMING
# ============================================
gzip -c file.txt > file.txt.gz
# Writes to stdout, keeps original
tar -cf - directory/ | gzip > backup.tar.gz
# Streams a tar archive through gzip
# ============================================
# PART 5: BZIP2
# ============================================
bzip2 file.txt
# Creates file.txt.bz2, removes file.txt
bzip2 -k file.txt
# Keeps original
bunzip2 file.txt.bz2
# Decompresses
# ============================================
# PART 6: XZ
# ============================================
xz file.txt
# Creates file.txt.xz, removes file.txt
xz -k file.txt
# Keeps original
unxz file.txt.xz
# Decompresses
xz -T 4 file.txt
# Multithreaded compression
# ============================================
# PART 7: LIST COMPRESSION INFO
# ============================================
gzip -l file.txt.gz
# Shows compressed size, uncompressed size, ratio
xz -l file.txt.xz
# Shows similar info for xz
# ============================================
# PART 8: TEST INTEGRITY
# ============================================
gzip -t file.txt.gz
bzip2 -t file.txt.bz2
xz -t file.txt.xz
# All exit with status 0 if the file is intact
# ============================================
# PART 9: COMPRESSING MULTIPLE FILES
# ============================================
# gzip can compress multiple files, but each becomes
# a separate .gz file. It does not create an archive.
gzip file1.txt file2.txt
# Creates file1.txt.gz and file2.txt.gz
# To bundle multiple files into one compressed file,
# use tar first:
tar -czf archive.tar.gz file1.txt file2.txt
# ============================================
# PART 10: THE LFCS PIPELINE
# ============================================
# Compress a log file, keeping the original
gzip -k /var/log/syslog
# Decompress to stdout without creating a file
gzip -dc /var/log/syslog.gz | less
# Create a compressed tar archive
tar -czf /backup/etc-$(date +%F).tar.gz /etc
# Test the archive
gzip -t /backup/etc-$(date +%F).tar.gz
# List the contents without extracting
tar -tzf /backup/etc-$(date +%F).tar.gz | head
# Compare compression sizes
gzip -k bigfile
bzip2 -k bigfile
xz -k bigfile
ls -lh bigfile.*
The ten parts show basic gzip, decompress with gzip, compression levels, stdout and streaming, bzip2, xz, listing compression info, testing integrity, compressing multiple files, and the LFCS pipeline scenarios.
Quick Reference
The Three Compressors
| Tool | Extension | Decompress | Algorithm | Speed | Ratio |
|---|---|---|---|---|---|
gzip | .gz | gunzip | DEFLATE | Fast | Good |
bzip2 | .bz2 | bunzip2 | Burrows-Wheeler | Slow | Better |
xz | .xz | unxz | LZMA2 | Slowest | Best |
Common Options
| Option | gzip | bzip2 | xz | Meaning |
|---|---|---|---|---|
-k | โ | โ | โ | Keep original |
-d | โ | โ | โ | Decompress |
-c | โ | โ | โ | Write to stdout |
-1…-9 | โ | โ | โ | Compression level |
-t | โ | โ | โ | Test integrity |
-v | โ | โ | โ | Verbose |
-l | โ | โ | โ | List info |
-T | โ | โ | โ | Multithreading |
Compression Levels
| Level | gzip | bzip2 | xz |
|---|---|---|---|
| Fastest | -1 | -1 | -1 |
| Default | -6 | -9 | -6 |
| Max | -9 | -9 | -9 |
| Extra | โ | โ | -e |
Decompression Commands
| Compressed file | Command |
|---|---|
file.gz | gunzip file.gz or gzip -d file.gz |
file.bz2 | bunzip2 file.bz2 or bzip2 -d file.bz2 |
file.xz | unxz file.xz or xz -d file.xz |
| Any | tar -xf file (auto-detects) |
Best Practices
โ Do This:
# Use -k to keep the original
gzip -k file.txt # โ
# Use -c for streaming
gzip -c file.txt > file.txt.gz # โ
# Use tar for multiple files
tar -czf archive.tar.gz file1 file2 dir/ # โ
# Test integrity before relying on the archive
gzip -t file.txt.gz # โ
# Use -dc to decompress to stdout
gzip -dc file.txt.gz | less # โ
# Use -T for xz on multi-core systems
xz -T 4 bigfile # โ
โ Don’t Do This:
# Don't compress already compressed files
gzip file.jpg # no benefit, wastes time # โ
# Don't forget -k when the original is needed
gzip file.txt # removes file.txt # โ
# Don't use gzip on multiple files expecting an archive
gzip file1 file2 # creates two separate .gz files # โ
# Don't use xz for small files where speed matters
xz smallfile # gzip is faster for the same result # โ
# Don't rely on .gz extension alone
# Verify the file type with file(1) # โ
Common Pitfalls
| Pitfall | Why It Happens | Fix |
|---|---|---|
| Original file deleted | Default behavior | Use -k |
| Multiple files become separate archives | gzip works on single files | Use tar first |
| Decompressed file has wrong name | Using -c with redirect | Use -d or the right command |
| Slow compression with xz | High compression level | Use lower level or -T |
| File not actually compressed | Compressing already compressed data | Check with file |
| Integrity test fails | Corrupted file | Re-download or re-create |
| Wrong decompressor used | Extension mismatch | Match the tool to the extension |
Real-World Examples
1. Compress with gzip
gzip file.txt
2. Compress and Keep Original
gzip -k file.txt
3. Decompress
gunzip file.txt.gz
4. Decompress to Stdout
gzip -dc file.txt.gz | less
5. Maximum Compression
gzip -9 file.txt
6. bzip2 Compress
bzip2 file.txt
7. xz Compress
xz file.txt
8. xz Multithreaded
xz -T 4 file.txt
9. Test Integrity
gzip -t file.txt.gz
10. Compressed Tar Archive
tar -czf archive.tar.gz directory/
Visual
The Three Compressors
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ THE THREE COMPRESSORS โ
โ โ
โ Speed: โ
โ gzip โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Fastest โ
โ bzip2 โโโโโโโโโโโโโโโโ Slower โ
โ xz โโโโโโโโ Slowest โ
โ โ
โ Ratio: โ
โ gzip โโโโโโโโโโโโ Good โ
โ bzip2 โโโโโโโโโโโโโโโโโโโโ Better โ
โ xz โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Best โ
โ โ
โ Compatibility: โ
โ gzip โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Universal โ
โ bzip2 โโโโโโโโโโโโโโโโโโโโ Common โ
โ xz โโโโโโโโโโโโ Modern Linux โ
โ โ
โ Choose gzip for speed and compatibility. โ
โ Choose bzip2 for a middle ground. โ
โ Choose xz when size matters most. โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Compression Workflow
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ COMPRESSION WORKFLOW โ
โ โ
โ Single file: โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ file.txt โ โ
โ โ โ โ โ
โ โ โผ โ โ
โ โ gzip file.txt โ โ
โ โ โ โ โ
โ โ โผ โ โ
โ โ file.txt.gz โ โ
โ โ (file.txt removed) โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ Multiple files: โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ file1.txt file2.txt dir/ โ โ
โ โ โ โ โ โ โ
โ โ โโโโโโโโโโโโผโโโโโโโโโโ โ โ
โ โ โผ โ โ
โ โ tar -cf archive.tar file1.txt file2.txt dir/ โ โ
โ โ โ โ โ
โ โ โผ โ โ
โ โ archive.tar โ โ
โ โ โ โ โ
โ โ โผ โ โ
โ โ gzip archive.tar โ โ
โ โ โ โ โ
โ โ โผ โ โ
โ โ archive.tar.gz โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ Or in one step: โ
โ tar -czf archive.tar.gz file1.txt file2.txt dir/ โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
The -k Option
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ THE -k OPTION โ
โ โ
โ WITHOUT -k: โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ gzip file.txt โ โ
โ โ โ โ
โ โ Before: file.txt โ โ
โ โ After: file.txt.gz โ โ
โ โ (file.txt is GONE) โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ WITH -k: โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ gzip -k file.txt โ โ
โ โ โ โ
โ โ Before: file.txt โ โ
โ โ After: file.txt.gz โ โ
โ โ file.txt (still there) โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โ The -k option is available in gzip, bzip2, and xz. โ
โ It is the first thing to check when a file disappears. โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Streaming Through a Pipe
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ STREAMING THROUGH A PIPE โ
โ โ
โ tar -cf - directory/ | gzip > backup.tar.gz โ
โ โ
โ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โ
โ โ tar โ โ gzip โ โ file โ โ
โ โ โโโโโบโ โโโโโบโ โ โ
โ โ creates โ โ compresses โ โ backup.tar.gzโ โ
โ โ archive on โ โ the stream โ โ โ โ
โ โ stdout โ โ โ โ โ โ
โ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โ
โ โ
โ No intermediate file. The archive is created and โ
โ compressed in a single pass. โ
โ โ
โ Remote copy: โ
โ tar -czf - directory/ | ssh remote 'cat > backup.tar.gz' โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Summary
| Item | Value |
|---|---|
gzip | Fastest, .gz, gunzip |
bzip2 | Middle, .bz2, bunzip2 |
xz | Best ratio, .xz, unxz |
| Default behavior | Removes original |
-k | Keeps original |
-c | Writes to stdout |
-d | Decompresses |
-1…-9 | Compression level |
-t | Tests integrity |
-l | Lists info |
Key takeaways:
- Three compressors exist for three points on the speed-versus-ratio curve.
gzipis fast and universally supported.bzip2compresses better but is slower.xzcompresses best but is the slowest and most memory-intensive. The choice depends on whether speed, size, or compatibility matters most . - All three compress a single file and remove the original by default. The
-koption keeps the original, and it is the first thing to check when a file disappears after compression. The-coption writes to standard output, which is the pattern for streaming and for redirecting the output . - Compression is separate from archiving. The compressors do not bundle multiple files. To compress a directory or a set of files, create a tar archive first and then compress it. The
tarcommand automates this with-z,-j, and-J. - The compression level is controlled with
-1through-9.gzipdefaults to-6,bzip2defaults to-9, andxzdefaults to-6. Higher levels compress more but take longer.xzalso has the-eoption for extra thorough compression and-Tfor multithreading . - The
-doption decompresses, and the dedicated commands are equivalent.gunzipisgzip -d,bunzip2isbzip2 -d, andunxzisxz -d. The dedicated commands are the conventional names, but the options are the same . - The
-toption tests integrity, and the-loption lists compression info. Testing is the way to verify that a compressed file is not corrupted. Listing shows the compressed size, the uncompressed size, and the ratio . - Compressed files are usually recognized by their extensions.
.gzfor gzip,.bz2for bzip2, and.xzfor xz. The compound extensions.tar.gz,.tar.bz2, and.tar.xzindicate a compressed tar archive.tarauto-detects the compression when extracting in most modern versions . - Compressing already-compressed data is usually a waste. Images, videos, and other compressed formats do not benefit from further compression. The
filecommand can identify the type before compressing .
Remember: Compression reduces file size, and Linux provides three standard compressors for the job. gzip is the fast, universal choice. bzip2 is the middle ground. xz is the best-ratio choice. All three compress a single file and remove the original by default; the -k option keeps it. Compression is separate from archiving, so the standard pattern for compressing multiple files is tar followed by a compressor, or tar with the -z, -j, or -J option. For the LFCS exam, the key patterns are compressing a file with each tool, decompressing, keeping the original with -k, testing integrity with -t, and creating a compressed tar archive. The tools are simple, but the default behavior of removing the original is the detail that matters most.
Stop using slow, ad-bloated tool sites! ๐คฎ
๐ Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
โ
Finance (Mortgage, Interest, Inflation)
โ
Tech (Base64, JSON, Dev Suite, IP)
โ
Health (BMI, BMR, TDEE)
โ
Productivity (Timer, Workspace, QR)
โก๏ธ Fast & Private
๐ No data leaves your device
๐ 100% Free
๐ Use it now: https://tools.kandz.me
๐ Bookmark itโyouโll need it later!