LinuxCommandLibrary
GitHubF-DroidGoogle Play Store

Compression & Archiving

Getting Started

On Linux, archiving and compression are two separate steps: tar bundles many files into one archive, while compressors like gzip, xz, and zstd shrink a single data stream. That is why the typical format is a compressed tar archive such as .tar.gz or .tar.zst. The zip and 7z formats do both jobs in one tool.

Tar Archives

The first option tells tar what to do: c creates, x extracts, t lists. Add f for the archive file name and v to print each file as it is processed.
OptionDescription
cCreate an archive
xExtract an archive
tList archive contents
fArchive file name (must come right before it)
vVerbose: show files being processed
zgzip compression (.tar.gz)
jbzip2 compression (.tar.bz2)
Jxz compression (.tar.xz)
--zstdzstd compression (.tar.zst)
-C dirChange to directory before extracting
--exclude=patternSkip files matching a pattern
$ tar czf archive.tar.gz [files]
copy
$ tar cJf archive.tar.xz [files]
copy
$ tar --zstd -cf archive.tar.zst [files]
copy
$ tar czf backup.tar.gz --exclude="*.log" [directory]
copy
When extracting, modern GNU tar detects the compression automatically, so plain xf works for every format.
$ tar xf archive.tar.gz
copy
$ tar xf archive.tar.zst -C [directory]
copy
List the contents before extracting an archive from an untrusted source, and extract single files by naming them.
$ tar tf archive.tar.gz
copy
$ tar xf archive.tar.gz path/inside/archive.txt
copy
A well-behaved archive contains a single top-level directory. If tar tf shows loose files instead, extract into a fresh directory with -C to avoid littering your current one.

Choosing a Compressor

All compressors trade speed against ratio. As a rule of thumb: zstd is the modern default, gzip is the universal lowest common denominator, xz squeezes hardest when time does not matter.
ToolDescription
gzipFast, supported everywhere, moderate ratio
bzip2Better ratio than gzip but slow, mostly legacy
xzHighest ratio, slow to compress, common for releases
zstdVery fast with ratios near xz, best general choice
lz4Extremely fast, lower ratio, good for live pipes

Compressing Single Files

The classic compressors share one interface: they compress a file in place, replace it, and append their extension. -k keeps the original, -d decompresses, and -1 to -9 trade speed for ratio.
$ gzip [file]
copy
$ gzip -k -9 [file]
copy
$ bzip2 [file]
copy
$ xz [file]
copy
$ zstd [file]
copy
$ zstd -19 [file]
copy
Each has a matching decompressor.
$ gunzip [file].gz
copy
$ bunzip2 [file].bz2
copy
$ unxz [file].xz
copy
$ unzstd [file].zst
copy
These tools compress single files only. To compress a directory, tar it first or use zip.On multi-core machines, xz -T0 and zstd -T0 use all cores, and pigz is a parallel drop-in for gzip.
$ xz -T0 [file]
copy
$ pigz [file]
copy

Working with Compressed Files

Read, search, and compare compressed text files without unpacking them.
$ zcat [file].gz
copy
$ zless [file].gz
copy
$ zgrep "pattern" [file].gz
copy
$ zdiff [file1].gz [file2].gz
copy
The other formats have their own cat tools.
$ bzcat [file].bz2
copy
$ xzcat [file].xz
copy
$ zstdcat [file].zst
copy

Zip Archives

zip is the standard interchange format with Windows and macOS. Use -r to include directories recursively and -e to encrypt with a password.
$ zip -r archive.zip [directory]
copy
$ zip -e -r secret.zip [directory]
copy
Extract with unzip, into a specific directory with -d, or just inspect the contents first.
$ unzip archive.zip
copy
$ unzip archive.zip -d [directory]
copy
$ unzip -l archive.zip
copy
$ zipinfo archive.zip
copy
Zip does not preserve full Unix ownership and permissions. For system backups, stick to tar.

7-Zip Archives

7z offers very high compression ratios and strong AES-256 encryption. a adds to an archive, x extracts with full paths, l lists.
$ 7z a archive.7z [files]
copy
$ 7z a -p archive.7z [files]
copy
$ 7z x archive.7z
copy
$ 7z l archive.7z
copy
Use x to extract, not e: the e command flattens all files into the current directory, discarding their paths.

Rar Archives

Rar is a proprietary format; unrar extracts it.
$ unrar x archive.rar
copy
$ unrar l archive.rar
copy

Universal Extractors

Tools that detect the format for you, handy when you do not want to remember per-format flags.
$ atool -x archive.tar.gz
copy
$ unp archive.rar
copy
$ dtrx archive.zip
copy
$ ouch decompress archive.tar.zst
copy

cpio & ar

cpio reads file lists from stdin; it is the format behind initramfs images and RPM packages.
$ find . | cpio -o > archive.cpio
copy
$ cpio -id < archive.cpio
copy
ar creates the archives behind static libraries (.a) and Debian packages (.deb).
$ ar rcs libfoo.a [object-files]
copy
$ ar t package.deb
copy
Copied to clipboard
Kai