Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Linux File Compression Tools: Beginner's Guide

· 6 min read · Home Tech

In this article
  1. Why Compression Matters on a Server
  2. The Main Tools and Their Trade-offs
  3. How to Compress and Decompress Files
  4. Compressing Directories with tar
  5. How to Choose the Right Tool
  6. Common Problems and Misconceptions
  7. Safety and Best Practices
  8. Alternatives and Advanced Tools
  9. Frequently Asked Questions

File compression tools on Linux servers let you shrink files and folders to save disk space and speed up transfers. This guide explains the most common commands—gzip, bzip2, xz, and zip—and shows you exactly when to use each one.

Why Compression Matters on a Server

Servers accumulate logs, backups, database dumps, and uploaded files. Uncompressed, these can fill a disk quickly and make copying data to another machine painfully slow. Compression reduces storage footprint and network transfer time, which is especially important when you pay for bandwidth or have limited disk space. It also groups many files into a single archive, making backups and distribution simpler.

On a Linux server you typically work from the command line, so knowing a few compression commands is a core skill. The tools are already installed on most distributions, and they follow a similar pattern: compress a file, decompress it, and combine with tar for directories.

The Main Tools and Their Trade-offs

Each tool uses a different algorithm, balancing speed, compression ratio, and compatibility. Here are the ones you’ll encounter most:

  • gzip (files end in .gz) – Fast and widely supported. Best for everyday use, especially logs and text files. Compression ratio is moderate.
  • bzip2 (files end in .bz2) – Slower than gzip but compresses better, particularly for text and source code. Uses the Burrows-Wheeler algorithm.
  • xz (files end in .xz) – Highest compression ratio among the common tools, but slowest. Great for archives you store long-term, like backups.
  • zip (files end in .zip) – Not a Linux-native format, but universally compatible with Windows and macOS. Includes multiple files in one archive without needing tar.

How to Compress and Decompress Files

The basic commands are simple. To compress a single file with gzip:

gzip access.log

This replaces access.log with access.log.gz. To reverse it:

gunzip access.log.gz

For bzip2 and xz, the pattern is identical—just change the command name:

bzip2 backup.sql
xz archive.tar

And decompress with bunzip2 and unxz (or xz -d).

To keep the original file, use the -k option (e.g., gzip -k file.txt). To see the compression result, use ls -lh to compare file sizes.

Compressing Directories with tar

Single-file compression is fine for logs, but directories need to be bundled first. The tar command creates an archive, and you can pipe it through a compressor. The most common forms:

tar -czvf archive.tar.gz /path/to/dir
tar -cjvf archive.tar.bz2 /path/to/dir
tar -cJvf archive.tar.xz /path/to/dir

The options break down as:

  • -c – create an archive
  • -z – compress with gzip
  • -j – compress with bzip2
  • -J – compress with xz
  • -v – verbose (list files as they’re added)
  • -f – specify the archive filename

To extract, use -x instead of -c:

tar -xzvf archive.tar.gz

You can also use the -a option to let tar guess the compressor from the file extension:

tar -cavf archive.tar.xz /path/to/dir

How to Choose the Right Tool

There’s no single “best” tool—it depends on your priorities. Here’s a quick decision guide:

  • Speed matters most (e.g., compressing on the fly for a transfer): use gzip.
  • Best compression ratio (e.g., long-term backups): use xz.
  • Need to share with Windows users: use zip.
  • Compressing source code or text: bzip2 often beats gzip without being as slow as xz.

You can test on your own data. For example:

time gzip -k bigfile
time bzip2 -k bigfile
time xz -k bigfile

Then compare the resulting file sizes. The time command shows how long each took. On a modern server, gzip might finish in seconds, while xz could take minutes for the same file—but the xz output might be 20–30% smaller.

Common Problems and Misconceptions

“gzip compresses directories” – No, gzip only works on a single file. You must use tar first to bundle the directory, then compress the resulting archive. That’s why you see .tar.gz files everywhere.

“Decompressing a .gz file gives me the original folder” – Not unless you used tar. A plain .gz file decompresses to one file. If you have a .tar.gz, you need tar -xzf to extract the whole structure.

“Higher compression is always better” – Not if it takes 10 minutes to compress a file you’ll delete next week. Compression time and CPU usage are real costs, especially on busy servers.

“I can edit a compressed file directly” – You can’t edit a compressed file in place. You must decompress, edit, and recompress. Some tools like gzip -d then gzip are the standard workflow.

“Zip and gzip are the same” – They’re different formats. Zip includes its own file listing and can compress multiple files without tar. Gzip is a stream compressor that works on one file and relies on tar for archives.

Safety and Best Practices

Compression commands overwrite files by default. If you run gzip file.txt, the original file.txt is removed. To avoid data loss, use -k to keep the original, or work on a copy. When extracting archives, be careful of “zip slip” attacks—malicious archives can write files outside the target directory. Always extract with tar -xzf in a safe, dedicated folder, and inspect the file list first with tar -tzf archive.tar.gz.

Also, compression is not encryption. Anyone who can read the file can decompress it. If you need confidentiality, use gpg or openssl to encrypt the archive after compression.

Alternatives and Advanced Tools

Beyond the basics, Linux offers more specialized tools:

  • zstd (Zstandard) – A modern compressor from Facebook that offers gzip-like speed with better ratios. Files end in .zst. It’s becoming common in package managers and log rotation.
  • 7z (p7zip) – Supports the 7z format with very high compression, but requires installing the p7zip package.
  • lz4 – Extremely fast compression, but low ratio. Useful for real-time compression of network traffic or temporary files.
  • pigz – A parallel gzip that uses multiple CPU cores to compress faster.

For log rotation, tools like logrotate can automatically compress old logs with gzip or zstd, saving you manual work.

Frequently Asked Questions

How do I compress a folder in Linux?

Use tar with a compression flag. For example, tar -czvf folder.tar.gz folder/ creates a gzipped tarball of the folder. To extract it, use tar -xzvf folder.tar.gz.

What is the difference between .tar.gz and .gz?

A .gz file is a single file compressed with gzip. A .tar.gz is a tar archive (which can contain many files and folders) that has been compressed with gzip. You need tar to extract the archive, and gzip to decompress the stream.

Which compression tool gives the smallest file size?

Among the common tools, xz generally gives the smallest file size, followed by bzip2, then gzip. However, xz is also the slowest. For a balance of speed and size, many users now prefer zstd.

Can I compress a file without losing the original?

Yes, use the -k option with gzip, bzip2, or xz. For example, gzip -k file.txt keeps file.txt and creates file.txt.gz.

How do I check the integrity of a compressed file?

For gzip, use gzip -t file.gz to test. For bzip2, bzip2 -t file.bz2. For xz, xz -t file.xz. For tar archives, you can also use tar -tzf file.tar.gz to list contents and verify the archive is readable.

Why does my .gz file take so long to decompress?

Decompression time depends on the compression level used when creating the file. Higher compression levels (e.g., gzip -9) produce smaller files but take longer to decompress. Also, large files naturally take more time. If speed is critical, use a lower compression level or a faster tool like lz4.