Smart Systems for Home

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

Compress Files with zstd on Linux: A Practical Guide

· 4 min read · Home Tech

In this article
  1. Background: What Is Zstandard?
  2. How to Install zstd
  3. Basic Compression and Decompression
  4. Choosing the Right Compression Level
  5. Compressing Directories and Multiple Files
  6. Common Options and Tips
  7. Testing Integrity and Comparing with Other Tools
  8. Frequently Asked Questions

Yes, you can compress files with zstd on Linux using the zstd command-line tool, which offers fast compression and decompression with high ratios. This article explains how to use zstd, choose the right compression level, and handle common tasks like compressing directories and verifying integrity.

Background: What Is Zstandard?

Zstandard (zstd) is a lossless data compression algorithm developed by Yann Collet at Facebook (now Meta) and released in 2016. It was designed to offer a better trade-off between compression speed and ratio compared to older algorithms like gzip and bzip2. The zstd command-line tool is included in most Linux distributions by default or available via package managers. It supports a wide range of compression levels (from 1 to 19, plus ultra levels 20-22), and its decompression speed is consistently fast regardless of the compression level used.

How to Install zstd

Most modern distributions come with zstd pre-installed. If not, install it using your package manager:

  • Debian/Ubuntu: sudo apt install zstd
  • Fedora/RHEL: sudo dnf install zstd
  • Arch: sudo pacman -S zstd

After installation, verify with zstd --version.

Basic Compression and Decompression

To compress a single file, use:

zstd file.txt

This creates file.txt.zst and removes the original (unless you use -k to keep it). To decompress:

zstd -d file.txt.zst

Or use unzstd as a shortcut. You can also pipe data directly, which is useful for streaming:

cat bigfile | zstd > bigfile.zst

Choosing the Right Compression Level

zstd offers levels 1-19, with higher levels giving better compression but taking longer. Level 3 is the default and balances speed and ratio well. For faster compression, use level 1. For maximum ratio, try levels 19 or ultra levels 20-22 (which require more memory). Use --ultra -22 for the highest ratio. You can also set a target compression ratio with --adapt for automatic adjustment. For most users, the default level 3 is recommended.

Compressing Directories and Multiple Files

zstd does not compress directories by itself. Use tar to bundle files first, then compress with zstd:

tar --zstd -cf archive.tar.zst /path/to/directory

To extract:

tar --zstd -xf archive.tar.zst

Alternatively, use tar -I 'zstd' to specify the compressor. This method is standard for backups and distribution.

Common Options and Tips

  • -k keep the original file after compression.
  • -v verbose output showing progress.
  • -f force overwrite existing files.
  • --rm remove source files after successful compression (default behavior).
  • -T0 use all CPU cores for parallel compression.

For example, zstd -k -T0 -v file.txt compresses with multiple threads while keeping the original.

Testing Integrity and Comparing with Other Tools

To check a compressed file for corruption, use zstd -t file.zst. This tests the integrity without decompressing. For comparison, gzip uses levels 1-9 and is generally slower than zstd at the same ratio. bzip2 offers better ratio but is much slower. xz provides higher ratio but is slower still. zstd often outperforms gzip in both speed and ratio, making it a popular choice for modern systems. However, compatibility might be an issue if you need to share files with systems that don't have zstd installed.

Frequently Asked Questions

How do I compress a file but keep the original?

Use the -k option: zstd -k file.txt. This leaves file.txt untouched and creates file.txt.zst.

What is the best compression level for zstd?

It depends on your needs. Default level 3 is a good balance. For faster backups, use level 1. For archival where size matters, try level 19 or ultra 22. Higher levels use more memory and CPU time.

Can zstd compress multiple files at once?

No, zstd works on single files. Use tar with --zstd to bundle multiple files or directories into one compressed archive.

Is zstd compatible with gzip?

No, they are different formats. You cannot decompress a .zst file with gzip. However, many tools like tar support both, and zstd can be used with tar via --zstd.

How do I check if a .zst file is corrupted?

Run zstd -t file.zst. It will verify the integrity and report any errors. This is useful before extracting important archives.

Does zstd use multiple CPU cores?

Yes, by default zstd uses a single thread, but you can enable multi-threading with -T0 to use all cores, which speeds up compression significantly on multi-core systems.