| |

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

ToolExtensionDecompressAlgorithmSpeedRatio
gzip.gzgunzipDEFLATEFastGood
bzip2.bz2bunzip2Burrows-WheelerSlowBetter
xz.xzunxzLZMA2SlowestBest

Common Options

Optiongzipbzip2xzMeaning
-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

Levelgzipbzip2xz
Fastest-1-1-1
Default-6-9-6
Max-9-9-9
Extraโ€”โ€”-e

Decompression Commands

Compressed fileCommand
file.gzgunzip file.gz or gzip -d file.gz
file.bz2bunzip2 file.bz2 or bzip2 -d file.bz2
file.xzunxz file.xz or xz -d file.xz
Anytar -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

PitfallWhy It HappensFix
Original file deletedDefault behaviorUse -k
Multiple files become separate archivesgzip works on single filesUse tar first
Decompressed file has wrong nameUsing -c with redirectUse -d or the right command
Slow compression with xzHigh compression levelUse lower level or -T
File not actually compressedCompressing already compressed dataCheck with file
Integrity test failsCorrupted fileRe-download or re-create
Wrong decompressor usedExtension mismatchMatch 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

ItemValue
gzipFastest, .gz, gunzip
bzip2Middle, .bz2, bunzip2
xzBest ratio, .xz, unxz
Default behaviorRemoves original
-kKeeps original
-cWrites to stdout
-dDecompresses
-1…-9Compression level
-tTests integrity
-lLists info

Key takeaways:

  • Three compressors exist for three points on the speed-versus-ratio curve. gzip is fast and universally supported. bzip2 compresses better but is slower. xz compresses 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 -k option keeps the original, and it is the first thing to check when a file disappears after compression. The -c option 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 tar command automates this with -z, -j, and -J .
  • The compression level is controlled with -1 through -9. gzip defaults to -6, bzip2 defaults to -9, and xz defaults to -6. Higher levels compress more but take longer. xz also has the -e option for extra thorough compression and -T for multithreading .
  • The -d option decompresses, and the dedicated commands are equivalent. gunzip is gzip -d, bunzip2 is bzip2 -d, and unxz is xz -d. The dedicated commands are the conventional names, but the options are the same .
  • The -t option tests integrity, and the -l option 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. .gz for gzip, .bz2 for bzip2, and .xz for xz. The compound extensions .tar.gz, .tar.bz2, and .tar.xz indicate a compressed tar archive. tar auto-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 file command 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!