Andrew Mercer
on this page

lz4

Format: .lz4 · Algorithm: byte-oriented LZ77, no entropy coding · First released: 2011 (Yann Collet)

lz4 is built for speed. Its ratio is roughly in gzip -1 territory or a little worse, but it compresses and decompresses at multiple gigabytes per second per core. It's used where CPU time matters more than bytes: in-memory compression (zram), filesystems (ZFS, Btrfs alternatives), kernel and initramfs images on fast-boot systems, databases, and fast network pipelines.

Its author later created zstd, whose negative --fast levels now compete with lz4 while offering far more range.

Basics

lz4 file                 # -> file.lz4, KEEPS file
lz4 --rm file            # remove original
lz4 -d file.lz4          # decompress (same as unlz4)
unlz4 file.lz4
lz4 -c file > out.lz4    # to stdout
lz4cat file.lz4          # print contents
lz4 -t file.lz4          # test integrity
lz4 -m file1 file2       # multiple files
lz4 -r dir/              # recursive (newer versions)

Like zstd, lz4 keeps the source by default. Unlike most tools, if you give it two filenames, the second is the output name: lz4 in out.lz4.

Levels

Level Mode
-1 (default) Standard lz4, fastest
--fast=N Acceleration: even faster, lower ratio
-3 … -12 LZ4-HC: much slower compression, better ratio, same fast decompression
lz4 -9 file              # HC mode
lz4 -12 file             # max HC level

With tar

tar -I lz4 -cf archive.tar.lz4 src_dir/
tar -I lz4 -xf archive.tar.lz4
tar caf archive.tar.lz4 src_dir/       # GNU tar picks lz4 from extension

Fast copies over the network

On fast LANs, heavier compressors become the bottleneck. lz4 usually saves bandwidth without slowing the transfer:

tar cf - dir/ | lz4 | ssh host 'lz4 -d | tar xf - -C /dest'

See pipelines.

Where you'll see it

  • zram compressed swap (lz4 or zstd)
  • ZFS compression=lz4 (long the recommended default)
  • Kernel and initramfs compression options
  • journald (older builds), Kafka, Cassandra, RocksDB and other storage engines

See also