Smart Systems for Home

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

Compress Files Faster in Linux: 7 Ways

· 7 min read · Home Tech

In this article
  1. Why Compression Speed Matters
  2. How Parallel Compression Works
  3. Fastest Tools and Algorithms
  4. How to Use Parallel Tools
  5. Choosing the Right Compression Level
  6. Common Problems and Solutions
  7. Comparison: When to Use What
  8. Costs and Trade-offs
  9. Frequently Asked Questions

To compress files faster in Linux, use a parallel compression tool like pigz (parallel gzip) or pbzip2 (parallel bzip2), which split the work across multiple CPU cores, often cutting compression time by 50-80% on multi-core systems. You can also speed things up by choosing a faster compression algorithm (like lz4 or zstd with a low level) or by adjusting the compression level of the tool you already use. This guide explains the practical options, how they work, and when each one makes sense for your specific files and hardware.

Why Compression Speed Matters

Compression speed affects everything from daily backups to moving large datasets between servers. The default tools on most Linux distributions—gzip, bzip2, and xz—are single-threaded, meaning they use only one CPU core. On modern multi-core processors (even a modest 4-core laptop), that leaves most of your CPU idle while you wait. If you compress large files regularly, the time wasted adds up quickly. For example, compressing a 10 GB log file with default gzip might take 5 minutes on a single core, but with pigz using all 8 cores, the same job could finish in under a minute.

How Parallel Compression Works

Parallel compression tools divide the input data into blocks, compress each block independently on a separate CPU core, and then combine the compressed blocks into a single output file. This is possible because most compression algorithms (like DEFLATE used by gzip, or Burrows-Wheeler used by bzip2) can be applied to independent chunks of data. The output is not byte-for-byte identical to the single-threaded version, but it is fully compatible: you can decompress a pigz-created .gz file with the standard gunzip. The main trade-off is that the compressed file may be slightly larger (usually 1-3%) because each block has its own header and the algorithm can't exploit patterns that span across block boundaries.

Fastest Tools and Algorithms

If raw speed is your priority, consider these options (ordered from fastest to slowest, with typical compression ratios in parentheses):

  • lz4 – extremely fast, but low compression ratio (around 2:1 for text). Use when you need to compress/decompress in real-time, like for network transfer or RAM compression.
  • zstd (Zstandard) – near-lz4 speed at low levels, but with much better ratio (3-5:1). Level 1-3 is very fast; level 19 is slow but highly compressed.
  • pigz (parallel gzip) – uses all cores with DEFLATE, giving gzip-compatible output at near-lz4 speeds when you have many cores.
  • pbzip2 (parallel bzip2) – slower than pigz but often better compression ratio (4-6:1 on text).
  • xz with multi-threading (via xz -T0) – best ratio (6-8:1) but even with all cores, it's slower than the others.

For most users, zstd offers the best speed-to-ratio balance. It's included in most modern Linux distributions (package name zstd).

How to Use Parallel Tools

Using these tools is straightforward. Here are common examples:

  1. Install the tool (if not already present): sudo apt install pigz pbzip2 zstd (Debian/Ubuntu) or sudo dnf install pigz pbzip2 zstd (Fedora).
  2. Compress a file with pigz: pigz -k -9 file.txt – creates file.txt.gz, keep original with -k, use level 9 (best ratio). To use all cores, just run it; by default it uses all available cores.
  3. Compress a directory with tar and pigz: tar -cf - mydir/ | pigz > mydir.tar.gz – this pipes the tar output into pigz for parallel compression.
  4. Compress with zstd at high speed: zstd -3 -o file.zst file.txt – level 3 is fast, level 19 is slow but smaller.
  5. Compress with xz using multiple threads: xz -T0 -9 file.txt – -T0 tells xz to use all cores.

Remember, decompression is also faster with these tools: unpigz (or just pigz -d) for gzip files, zstd -d for zstd, and xz -d -T0 for xz.

Choosing the Right Compression Level

Every compression tool has a level setting (usually 1-9 or 1-19). Higher levels produce smaller files but take longer. If speed is your goal, don't use the highest level by default. For example:

  • gzip/pigz: level 1 is fastest (but poor ratio), level 6 is default, level 9 is slowest. For speed, use level 1-3.
  • zstd: level 3 is a good default, level 1 is ultra-fast (but still decent ratio).
  • xz: level 6 is default, level 9 is slow, but -0 through -3 are much faster (though ratio drops).

A common misconception is that higher level always saves a lot of space. In practice, the difference between level 6 and level 9 is often only 1-2% in file size, but the compression time can double or triple. For most purposes, using the default level is a good balance, and if you're in a hurry, drop the level by 2-3.

Common Problems and Solutions

Problem: The parallel tool isn't faster on my system. Check if your CPU has multiple cores (nproc). If you have only 1-2 cores, parallel tools won't help much. Also, if your disk is slow (e.g., a spinning HDD), the I/O might be the bottleneck, not the CPU. In that case, using a faster algorithm won't help; you'd need a faster disk or a different approach (like compressing to RAM first).

Problem: The compressed file is larger than with the single-threaded tool. This is normal for parallel compression because of block headers. If you need the absolute smallest file, use single-threaded xz -9 or zstd -19, but be prepared to wait.

Problem: I can't decompress a pigz file with gzip. Actually, you can. The format is fully gzip-compatible. If you have issues, make sure you're using a recent version of gzip (most are).

Problem: Memory usage is high. Parallel tools use more RAM because each thread has its own buffer. For example, xz -T0 can use several hundred MB for large files. If you're low on RAM, reduce the number of threads with -T4 (or a lower number).

Comparison: When to Use What

ScenarioRecommended ToolWhy
Quick backup of a large directorypigz or zstd -3Fast, good ratio, widely compatible
Archiving logs that compress wellxz -T0 -6Best ratio, multi-threaded
Real-time compression for network transferlz4Ultra-fast, minimal CPU overhead
Compressing a database dumpzstd -19High ratio, still faster than xz

If you need to share the archive with others, stick with gzip or zip format (pigz can create standard gzip files). If you're only using it yourself, zstd or xz are fine.

Costs and Trade-offs

Faster compression isn't free. The main costs are:

  • Compressed file size: Parallel compression often yields files 1-5% larger than the best single-threaded compression.
  • CPU usage: Using all cores for compression will make other tasks slower. If you're on a shared server, be considerate.
  • Memory: Each thread uses a few MB of RAM. For very large files, this can add up.
  • Compatibility: Some tools (like zstd) may not be installed on older systems. If you need to send the file to someone else, make sure they can decompress it.

In most cases, the time saved far outweighs these costs, especially if you compress files regularly.

Frequently Asked Questions

Is pigz faster than gzip?

Yes, on multi-core systems. pigz uses all available CPU cores, so it can be 2-8 times faster than gzip, depending on your core count. The output is a standard gzip file, so you can decompress it with gzip.

What is the fastest compression algorithm in Linux?

For raw speed, lz4 is the fastest, but it offers very low compression. If you need a balance of speed and ratio, zstd at level 1-3 is often the best choice. For gzip-compatible output, pigz at level 1 is the fastest.

How do I compress a folder with all cores?

Use tar with pigz: tar -cf - /path/to/folder | pigz > folder.tar.gz. This pipes the tar output into pigz, which uses all cores. Alternatively, use tar --use-compress-program=pigz -cf folder.tar.gz /path/to/folder.

Does compression level affect decompression speed?

Generally, no. Decompression speed is mostly independent of the compression level. Higher levels usually produce smaller files, which can decompress slightly faster because there's less data to read from disk. However, the difference is usually minimal.

Why is my compression slow even with pigz?

If your disk is slow (e.g., an HDD or a network drive), the I/O becomes the bottleneck. Try compressing a file from RAM (e.g., cat file | pigz > file.gz still reads from disk, but you can test with a small file). Also, check if your CPU is throttling due to heat or power limits.

Can I make xz use multiple cores?

Yes, use the -T flag. For example, xz -T0 file uses all cores. Note that xz's multi-threading is not as efficient as pigz's, but it still helps significantly on large files.