$_ CLI In Depth
Learning progress 0%
lesson disks mounts archives

Compression: gzip & xz

6 min read Module 12: Disks, Mounts & Archives Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:18

Lossless compression on a stream

A general compressor looks for repeated patterns in its input and encodes them more compactly. The output can be decoded to exactly the input, byte for byte, which is why these tools are called lossless. Text, logs, and source code compress very well because they repeat words and structures. Data that is already compressed, such as JPEG images or encrypted files, barely shrinks at all.

gzip uses the DEFLATE method, which combines a sliding window of recent data with Huffman coding. xz uses the LZMA2 method, which searches a larger window and produces a more compact encoding. The same input often becomes smaller with xz, and the price is more CPU time while compressing and more memory.

gzip basics

By default, gzip replaces the file you give it with a file that has a .gz suffix. The -k flag keeps the original, which is useful while you are learning. The -c flag writes the result to standard output instead, and it is the form you use in pipelines.

BASH3 lines
seq 1 1000 > numbers.txt
gzip -k numbers.txt
ls -l numbers.txt numbers.txt.gz

The listing shows both files. The compressed one is much smaller than the original, because the numbers in the file repeat digits and line endings in a regular pattern. To check the result without writing a copy of the file, use the test mode -t, which decodes the stream and verifies its internal checksum.

BASH1 lines
gzip -t numbers.txt.gz && echo intact

Decompressing and comparing

Use -d to decompress, or call gunzip, which is the same program under another name. To prove that a round trip is exact, decompress to standard output and compare it with the original using cmp, which prints nothing when the files are identical.

BASH1 lines
gzip -dc numbers.txt.gz | cmp - numbers.txt && echo identical

The dash tells cmp to read its first file from standard input. The round trip is the strongest test you can run, because a compressor that loses even one byte would fail it.

xz and its levels

xz has the same basic options. The -k flag keeps the original, -d decompresses, and -c writes to standard output. It also takes a compression level from -0 to -9, where a higher number uses more memory and time for a smaller result. The default is -6. On this lab machine, level 9 needs more memory than the guest has, so the examples below use level 6, and a run at level 9 here stops with an out-of-memory error. The level affects the size of the output, not whether the output decompresses correctly.

BASH3 lines
xz -k -6 numbers.txt
xz -t numbers.txt.xz && echo intact
ls -l numbers.txt.gz numbers.txt.xz

Compare the sizes of the two compressed files. For a file of 1000 short lines, the difference between the two methods can be small, and in a few cases gzip wins. Measure the actual files instead of trusting a rule of thumb, and remember that the test that matters is the round trip.

Tip

Use stat -c %s FILE when you want only the size in bytes, which is easier to compare in a script than the long listing from ls -l.

Compression and tar

A tar archive is already a stream, so it is simple to compress it as a whole. The command tar -cf - project | gzip -c writes the archive to standard output and pipes it through gzip. The GNU tar options -z for gzip and -J for xz do the same thing in one step, and the archive then has a name such as project.tar.gz.

BASH2 lines
tar -czf project.tar.gz project
tar -tzf project.tar.gz

The listing mode needs the same compression flag as the create mode, because tar cannot know the format of an archive without being told. Reading a compressed archive with the wrong flag produces an error that looks like a corrupt file.

Choosing a level and a format

The level you pick is a trade-off between time, memory, and size. A high xz level can need a lot of memory to decompress as well as to compress, which matters on small machines. For a one-time archive that is read rarely, a high level is worth the cost. For a file that is rewritten often, such as a log rotated every hour, a low level keeps each run cheap.

The format matters too. gzip is available almost everywhere and is the default in many protocols that carry compressed content. xz is common for source tarballs and packages, where the smaller download repays the extra CPU time. Choose a format your reader can open, and use xz when size is the priority and both ends support it.

Compression never replaces integrity checks. A compressed file that is damaged on disk may decompress to garbage or fail partway through. Both formats carry internal checks, so gzip -t and xz -t catch many errors. For a file you must keep for years, a separate digest from the checksum lesson is the stronger test.

Streams, pipes, and standard input

Both tools read standard input when no file is named, and both write to standard output with -c. That lets you compress data as it is produced, without a temporary file on disk. The pipeline below compresses and decompresses a stream and counts its lines, which is the same mechanism log tools use to inspect large compressed files.

BASH1 lines
seq 1 500 | gzip -c | gzip -dc | wc -l

The output is 500. The data was compressed, decompressed, and counted without touching the disk, and the same pattern lets a backup pipeline avoid intermediate copies entirely.

Hands-on: compare two compressors

Create a text file with 1000 lines, compress it with both tools, verify both results, and record which one is smaller. The check is that each compressed file decodes to the original and that you can say which is smaller with evidence.

BASH4 lines
seq 1 1000 > log.txt
gzip -k -9 log.txt
xz -k -6 log.txt
stat -c "%n %s" log.txt.gz log.txt.xz

The last command prints the name and size of each compressed file. Pick the smaller one, then confirm the round trip with cmp for that method. A size comparison is only useful when the output decodes correctly.

Inside a tar archive, and what gzip wraps

Visualizer

A tar file is a run of 512-byte blocks: a header, then the data padded to 512 bytes, and two zero blocks at the end. Compression wraps the whole stream, not each file.

1 / 5
The archive, in 512-byte blocks
0header block: name, mode, size, mtime, checksum
1data block: hi and newline, padded with zeros to 512
2zero block
3zero block: end of archive

A member is a 512-byte header followed by its data. The data is padded with zero bytes to a multiple of 512, so the 3-byte file still occupies one whole block.

printf 'hi\n' > /tmp/h.txt && tar -cf /tmp/h.tar -C /tmp h.txt && wc -c < /tmp/h.tar && od -A d -c -j 512 -N 3 /tmp/h.tar
Terminal lab · 3 steps · real Linux in your browser

Compress and verify with gzip and xz

Create a numbered file, compress it two ways, verify each result, and pick the smaller one.

01
Compress with gzip
Create numbers.txt with 1000 lines from seq. Compress it with gzip -k so the original stays. Then decompress to standard output and compare it with the original using cmp. ``bash seq 1 1000 > numbers.txt gzip -k -f numbers.txt gzip -dc numbers.txt.gz | cmp - numbers.txt && echo identical ``
Show hint for step 1
-k keeps the original. cmp prints nothing when the two inputs are identical.
02
Compress with xz
Compress the same numbers.txt with xz at its default level, 6, keeping the original. Level 9 needs more memory than this guest has, so do not use it here. Verify the result with xz -t and compare the decompressed output with the original. ``bash xz -k -6 -f numbers.txt xz -t numbers.txt.xz && xz -dc numbers.txt.xz | cmp - numbers.txt && echo identical ``
Show hint for step 2
xz uses the same -k flag as gzip. xz -dc decompresses to standard output.
03
Record the smaller result
Compare the byte sizes of numbers.txt.gz and numbers.txt.xz with stat. Write the name of the smaller file into smaller.txt. Use the actual sizes, not a guess. ``bash if [ "$(stat -c %s numbers.txt.gz)" -lt "$(stat -c %s numbers.txt.xz)" ]; then echo numbers.txt.gz else echo numbers.txt.xz fi > smaller.txt ``
Show hint for step 3
stat -c %s prints a size in bytes. Compare the two numbers with -lt inside [ ].
Check yourself Question 1 of 4
Concept Check Keeping the original file recall

Which gzip flag keeps the original file when it compresses a file?

Recap & Key Takeaways

gzip (DEFLATE) and xz (LZMA2) are lossless: decompressing the output gives back the exact input bytes.
gzip -k keeps the original and gzip -t tests a stream without writing it out; xz accepts the same flags and levels from -0 to -9.
Verify a compressed file with a round trip, for example gzip -dc FILE.gz | cmp - FILE, before you delete the original.
tar -z and tar -J compress an archive in one step, and the listing or extraction command must use the same compression flag.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue