Linux Cheatsheet

Archives and Compression

Use this Linux reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.

tar — Tape Archive

tar is the standard tool for bundling files. Compression is layered on top.

Creating Archives

tar -cf archive.tar dir/            # create uncompressed
tar -czf archive.tar.gz dir/        # create + gzip compress
tar -cjf archive.tar.bz2 dir/       # create + bzip2 compress
tar -cJf archive.tar.xz dir/        # create + xz compress (best ratio)
tar -caf archive.tar.zst dir/       # create + zstd compress (fast + small)
tar -cf archive.tar file1 file2 dir/ # multiple sources
tar -czf archive.tar.gz --exclude="*.log" dir/  # exclude pattern
tar -czf archive.tar.gz --exclude-vcs dir/       # exclude .git etc.

Extracting Archives

tar -xf archive.tar                 # extract (auto-detect compression)
tar -xf archive.tar.gz              # extract gzip
tar -xf archive.tar.gz -C /tmp/    # extract to specific directory
tar -xf archive.tar file.txt        # extract single file
tar --strip-components=1 -xf a.tar # strip top-level directory

Inspecting Archives

tar -tf archive.tar.gz              # list contents
tar -tvf archive.tar.gz             # list with details
tar -tf archive.tar | grep "\.conf" # filter listing

tar Flag Reference

FlagMeaning
-ccreate
-xextract
-tlist
-f filearchive filename
-zgzip
-jbzip2
-Jxz
-aauto-detect from extension
-vverbose
-C dirchange to directory
-ppreserve permissions
--excludeexclude pattern

gzip / gunzip

gzip file.txt               # compress → file.txt.gz (removes original)
gzip -k file.txt            # keep original
gzip -d file.txt.gz         # decompress (same as gunzip)
gunzip file.txt.gz          # decompress
gzip -9 file.txt            # max compression
gzip -1 file.txt            # fastest compression
gzip -l file.txt.gz         # show compression ratio
zcat file.txt.gz            # view without decompressing
zgrep "pattern" file.gz     # grep inside gz
zless file.gz               # page through gz file

bzip2 / bunzip2

bzip2 file.txt              # compress → file.txt.bz2
bzip2 -k file.txt           # keep original
bzip2 -d file.txt.bz2       # decompress
bunzip2 file.txt.bz2        # decompress
bzcat file.bz2              # view without decompressing
bzgrep "pattern" file.bz2   # grep inside bz2

xz

xz file.txt                 # compress → file.txt.xz (high ratio, slow)
xz -k file.txt              # keep original
xz -d file.txt.xz           # decompress
unxz file.txt.xz            # decompress
xz -l file.txt.xz           # show ratio
xzcat file.txt.xz           # view without decompressing
xz -T4 file.txt             # use 4 threads
xz -0 file.txt              # fast (level 0); -9 = max

zstd — Fast Modern Compression

zstd file.txt               # compress → file.txt.zst
zstd -d file.txt.zst        # decompress
zstd -k file.txt            # keep original
zstd -19 file.txt           # high ratio (max 22)
zstd -1 file.txt            # fastest
zstd -T4 file.txt           # multithreaded
zstdcat file.txt.zst        # view without decompressing

zip / unzip

zip archive.zip file1 file2         # create zip
zip -r archive.zip dir/             # recursive
zip -e archive.zip file.txt         # encrypt with password
zip -u archive.zip newfile.txt      # update existing archive
zip -d archive.zip file.txt         # delete entry from zip
unzip archive.zip                   # extract to current dir
unzip archive.zip -d /tmp/          # extract to specific dir
unzip -l archive.zip                # list contents
unzip -p archive.zip file.txt       # extract to stdout

Compression Algorithm Comparison

FormatRatioSpeedUse case
gzipgoodfastuniversal compatibility
bzip2betterslowreplacing with xz/zstd
xzbestslowestdistribution archives, packages
zstdgood-bestvery faststreaming, backups, build artifacts
zipokfastcross-platform, Windows interop
lz4lowerfastestreal-time compression

Working with Archives — Common Patterns

# Pipe output directly into archive
tar -czf - dir/ | ssh user@host "cat > /backup/dir.tar.gz"

# Extract and inspect without writing to disk
tar -xzf archive.tar.gz --to-stdout file.txt

# Create dated backup
tar -czf "backup-$(date +%Y%m%d).tar.gz" /etc/

# Diff two tar archives
diff <(tar -tzf a.tar.gz | sort) <(tar -tzf b.tar.gz | sort)

# Convert between formats (e.g. bz2 → xz)
tar -xjf archive.tar.bz2 --to-stdout | xz > archive.tar.xz

# Extract only certain files
tar -xf archive.tar --wildcards "*.conf"