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
| Flag | Meaning |
|---|---|
-c | create |
-x | extract |
-t | list |
-f file | archive filename |
-z | gzip |
-j | bzip2 |
-J | xz |
-a | auto-detect from extension |
-v | verbose |
-C dir | change to directory |
-p | preserve permissions |
--exclude | exclude 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
| Format | Ratio | Speed | Use case |
|---|---|---|---|
| gzip | good | fast | universal compatibility |
| bzip2 | better | slow | replacing with xz/zstd |
| xz | best | slowest | distribution archives, packages |
| zstd | good-best | very fast | streaming, backups, build artifacts |
| zip | ok | fast | cross-platform, Windows interop |
| lz4 | lower | fastest | real-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"