opscape

Archives and compression cheat sheet

The most useful archives and compression commands on one printable page, every line checked by running it.

bzip2 Compress files to .bz2 with the Burrows-Wheeler algorithm, or decompress them with -d, bunzip2 or bzcat.

bzip2 -k access.log ls -l access.log*
Compress a file, keeping the original output
bunzip2 todo.txt.bz2 cat todo.txt
Decompress with -d or bunzip2 output
bzcat access.log.bz2 | grep -c POST
Read a compressed file without unpacking it output
tar -cjf src.tar.bz2 src tar -tjf src.tar.bz2
Make and read a .tar.bz2 output

gzip Compress files to .gz (replacing the original by default), or decompress them with -d or gunzip.

gzip access.log ls -l access.log*
Compress a file in place output
gzip -k access.log ls -l access.log*
Keep the original with -k output
ls notes* gzip -d notes.txt.gz ls notes*
Decompress with -d or gunzip output
gzip -c todo.txt > todo.txt.gz ls todo*
Write to standard output with -c output
gzip -l access.log.gz
Show compressed and original sizes output

tar Bundle files and directories into one archive, list it, or extract it; optionally compressed with gzip, bzip2, xz or zstd.

tar -czvf backup.tar.gz data logs ls -l backup.tar.gz
Create a gzip-compressed tarball output
tar -tf backup.tar.gz
List what is inside an archive output
mkdir restore tar -xzf backup.tar.gz -C restore ls restore restore/logs
Extract into another directory with -C output
tar -xzvf backup.tar.gz data/hosts.txt cat data/hosts.txt
Extract a single file output
mkdir app tar -xzf app-2.4.tar.gz -C app --strip-components=1 ls app
Drop leading directories when extracting output

unzip List, test or extract the files in a .zip archive.

unzip -l project.zip 'data/*.csv'
List an archive's contents output
unzip -q project.zip -d restore ls restore restore/src
Extract into a directory with -d output
unzip project.zip data/hosts.txt cat data/hosts.txt
Extract a single member output
echo '// local edit' >> src/main.c unzip -o project.zip src/main.c tail -n 2 src/main.c
Overwrite without asking in a script output

xz Compress files to .xz with LZMA2 for high ratios, or decompress them with -d, unxz or xzcat.

xz -k access.log ls -l access.log*
Compress a file, keeping the original output
unxz todo.txt.xz ls todo*
Decompress with -d or unxz output
xzcat access.log.xz | awk '{print $9}' | sort | uniq -c
Read a compressed file without unpacking it output
tar -cJf src.tar.xz src mkdir out && tar -xf src.tar.xz -C out ls out/src
Make and unpack a .tar.xz output

zcat Print the decompressed contents of gzip files to standard output, leaving the files compressed.

zcat todo.txt.gz
Print a compressed file output
zcat access.log.gz | grep -c ' 404 '
Search a compressed log without unpacking it output
zcat -f logs/app.log.1.gz logs/app.log | wc -l
Read current and rotated logs together output

zip Package and compress files into a .zip archive, the format every operating system opens natively.

zip notes.zip notes.txt todo.txt
Zip a few files output
zip -r src.zip src
Zip a directory with -r output
zip -rq data.zip data -x '*.json' '*.conf' unzip -l data.zip
Leave files out with -x output
zip -ru src.zip src
Refresh only changed files with -u output

zstd Compress files to .zst with Zstandard, fast at any ratio; decompress with -d, unzstd or zstdcat.

zstd access.log ls -l access.log*
Compress a file output
zstd -q --rm data/sales.csv ls data/sales*
Replace the original with --rm output
unzstd todo.txt.zst ls todo*
Decompress with -d or unzstd output
zstdcat access.log.zst | grep -c ' 200 '
Read a compressed file without unpacking it output
tar --zstd -cf src.tar.zst src tar -tf src.tar.zst | head -n 3
Make and read a .tar.zst output