gzip, xz and zstd: compress files

Updated November 2026

gzip compresses single files. Its relatives xz, zstd and bzip2 work the same way with different trade-offs between speed and size. To compress a whole directory, put it in an archive with tar first.

gzip [options] file ... # file -> file.gz gunzip file.gz # file.gz -> file

Examples

gzip access.log                 # replaces access.log with access.log.gz
gzip -k access.log              # keep the original
gzip -9 dump.sql                # best compression (slowest)
gzip -1 big.img                 # fastest
gunzip access.log.gz            # decompress (same as gzip -d)
gzip -l access.log.gz           # compressed and uncompressed size
gzip -t backup.gz               # test integrity
gzip -c report.txt > report.txt.gz   # write to standard output, keep the original

Read compressed files without unpacking

zcat access.log.2.gz | head
zless access.log.2.gz
zgrep "404" access.log.*.gz
xzcat file.xz ; zstdcat file.zst

Log rotation compresses old logs with gzip, so zgrep and zless are everyday tools for administrators.

Which tool?

ToolExtensionStrengths
gzip.gzAvailable everywhere, fast enough, the safe default for exchange
zstd.zstMuch faster than gzip at better ratios; multithreaded with -T0; good for backups
xz.xzSmallest files, slow to compress; common for software releases
bzip2.bz2Older; still found in archives
zip.zipArchive and compression in one, for exchange with Windows users
zstd -T0 -19 backup.tar          # zstd, all CPU cores, high compression
zstd -d backup.tar.zst
xz -T0 -k image.raw              # xz with all cores, keep the original
unxz image.raw.xz
zip -r site.zip site/            # zip a directory
unzip -l site.zip ; unzip site.zip -d /tmp/site

Compressing already-compressed data (JPEG, MP4, ZIP files) gains almost nothing. For parallel gzip on many cores, install pigz.