Linux has many compression tools, and the right choice depends on what you care about: speed, compression ratio, memory, or compatibility. For backups that run every night, speed matters. For archives you’ll keep for years, ratio matters. Here’s how the common tools compare, and when to use each.

At a glance

Tool Speed (compress) Ratio Decompression speed Multi-threaded Typical extension
lz4 Fastest Lowest Fastest Yes (-T) .lz4
gzip Fast Good Fast pigz .gz
zstd Very fast (tunable) Very good Very fast Yes (-T0) .zst
bzip2 Slow Better than gzip Slow pbzip2 .bz2
xz Slowest Best (with 7z) Moderate Yes (-T0) .xz
zip Fast Like gzip Fast No .zip
7z Slow Best Moderate Yes .7z

Exact numbers depend on the data: text and logs compress hugely, while already-compressed files (JPEG, MP4, zip) barely shrink.

Recommendations

Use Recommended
Nightly backups, database dumps zstd (fast, good ratio), or gzip for maximum compatibility
Long-term archives xz, or zstd at high levels (-19)
Log rotation gzip (logrotate default) or zstd
Fast transfers over a network zstd or lz4
Sharing with Windows users zip (or 7z)
Maximum compatibility (old systems) gzip

zstd has become the best all-rounder: it compresses about as well as gzip at much higher speed, or close to xz at higher levels, and decompresses very fast.

Commands

gzip:

gzip file.sql                  # → file.sql.gz (removes original)
gzip -k -9 file.sql            # keep original, max compression
gunzip file.sql.gz
zcat file.sql.gz | mysql db    # stream without unpacking

zstd:

sudo apt install zstd
zstd -T0 -10 file.sql          # all cores, level 10 → file.sql.zst
zstd -d file.sql.zst
zstdcat file.sql.zst | mysql db

xz:

xz -T0 -9 file.tar             # → file.tar.xz
xz -d file.tar.xz

Multi-threaded gzip: sudo apt install pigz, then pigz -p 4 file.

With tar

tar bundles files. Compression is applied on top:

tar -czf site.tar.gz /var/www/site           # gzip
tar -cJf site.tar.xz /var/www/site           # xz
tar --zstd -cf site.tar.zst /var/www/site    # zstd (GNU tar 1.31+)
tar -I 'zstd -T0 -10' -cf site.tar.zst /var/www/site   # custom options
tar -xf site.tar.zst                         # tar auto-detects on extraction

See tar command examples.

Streaming dumps straight into compression

Avoid writing an uncompressed dump to disk first:

mysqldump --single-transaction mydb | zstd -T0 > mydb-$(date +%F).sql.zst
pg_dump mydb | gzip > mydb.sql.gz

See mysqldump.

zip and 7z (for Windows compatibility)

zip -r site.zip /var/www/site
unzip site.zip -d /tmp/site
7z a -mx=9 archive.7z folder/      # sudo apt install p7zip-full (or 7zip)

See zip and unzip on Linux and 7-Zip on Linux.

CPU vs time trade-off on a VPS

High compression levels use a lot of CPU. On a busy server, run backups with a lower priority:

nice -n 19 ionice -c3 tar --zstd -cf /backups/site.tar.zst /var/www/site

See process priority with nice.

Quick reference

Goal Command
Fast, compatible gzip / pigz
Best all-round zstd -T0 -10
Smallest archives xz -T0 -9
Windows-friendly zip -r
tar + zstd tar --zstd -cf a.tar.zst dir

Related: Linux split large files and backup strategy.

  • Linux
  • Compression
  • Backup