| name | archive |
| description | > Use when this capability is needed. |
Archive and Compression Operations
Quick Reference
| Action | Command |
|---|
| Create tar.gz | tar czf archive.tar.gz dir/ |
| Extract tar.gz | tar xzf archive.tar.gz |
| List tar.gz | tar tzf archive.tar.gz |
| Create zip | zip -r archive.zip dir/ |
| Extract zip | unzip archive.zip |
| List zip | unzip -l archive.zip |
| Compress file (gzip) | gzip file |
| Decompress gzip | gunzip file.gz |
tar — Tape Archive
Core Flags
| Flag | Purpose |
|---|
c | Create archive |
x | Extract archive |
t | List contents |
f | Archive filename (must be last before filename) |
v | Verbose (show files being processed) |
z | Compress/decompress with gzip (.tar.gz, .tgz) |
j | Compress/decompress with bzip2 (.tar.bz2) |
J | Compress/decompress with xz (.tar.xz) |
--zstd | Compress/decompress with zstd (.tar.zst) |
r | Append files to archive (uncompressed tar only) |
u | Update: append files newer than in archive |
Create Archives
tar czf archive.tar.gz dir/
tar czf archive.tar.gz file1.txt file2.txt dir/
tar cjf archive.tar.bz2 dir/
tar cJf archive.tar.xz dir/
tar --zstd -cf archive.tar.zst dir/
tar cf archive.tar dir/
tar czvf archive.tar.gz dir/
tar czf archive.tar.gz --exclude='*.log' --exclude='node_modules' dir/
tar czf archive.tar.gz --exclude-vcs dir/
tar czf archive.tar.gz -X exclude-list.txt dir/
tar czf code.tar.gz dir/ --include='*.rs' --include='*.toml'
tar czf archive.tar.gz --preserve-permissions dir/
tar czf "backup-$(date +%Y%m%d-%H%M%S).tar.gz" dir/
tar czfh archive.tar.gz dir/
tar czf archive.tar.gz -C / home/user/data
Extract Archives
tar xzf archive.tar.gz
tar xzf archive.tar.gz -C /destination/
tar xjf archive.tar.bz2
tar xJf archive.tar.xz
tar --zstd -xf archive.tar.zst
tar xzf archive.tar.gz path/to/file.txt
tar xzf archive.tar.gz --wildcards '*.rs'
tar xzvf archive.tar.gz
tar xzf archive.tar.gz --overwrite
tar xzf archive.tar.gz --keep-old-files
tar xzf archive.tar.gz --strip-components=1
tar xf archive.tar.gz
tar xf archive.tar.bz2
tar xf archive.tar.xz
List Contents
tar tzf archive.tar.gz
tar tzvf archive.tar.gz
tar tzf archive.tar.gz --wildcards '*.rs'
tar tzf archive.tar.gz | wc -l
Append and Update (uncompressed tar only)
tar rf archive.tar newfile.txt
tar uf archive.tar dir/
tar Af archive1.tar archive2.tar
gzip / gunzip
gzip file.txt
gzip -k file.txt
gzip -9 file.txt
gunzip file.txt.gz
gzip -d file.txt.gz
gunzip -k file.txt.gz
zcat file.txt.gz
zless file.txt.gz
zgrep "pattern" file.txt.gz
gzip -t file.txt.gz
gzip -l file.txt.gz
gzip file1.txt file2.txt file3.txt
gzip -r dir/
bzip2 / bunzip2
bzip2 file.txt
bzip2 -k file.txt
bunzip2 file.txt.bz2
bzip2 -d file.txt.bz2
bzcat file.txt.bz2
bzip2 -t file.txt.bz2
xz / unxz
xz file.txt
xz -k file.txt
xz -9 file.txt
xz -T0 file.txt
unxz file.txt.xz
xz -d file.txt.xz
xzcat file.txt.xz
xz -t file.txt.xz
xz -l file.txt.xz
zstd — Zstandard
zstd file.txt
zstd -k file.txt
zstd -19 file.txt
zstd --ultra -22 file.txt
zstd -T0 file.txt
zstd -d file.txt.zst
unzstd file.txt.zst
zstdcat file.txt.zst
zstd -t file.txt.zst
cat large.log | zstd > large.log.zst
zstd -d < large.log.zst > large.log
zip / unzip
Create Zip Archives
zip -r archive.zip dir/
zip archive.zip file1.txt file2.txt
zip archive.zip newfile.txt
zip -9 -r archive.zip dir/
zip -r archive.zip dir/ -x "*.log" -x "*.tmp"
zip -r archive.zip dir/ -x "dir/cache/*"
zip -e -r archive.zip dir/
zip -r -s 100m archive.zip dir/
zip -0 -r archive.zip dir/
zip -u archive.zip dir/*
Extract Zip Archives
unzip archive.zip
unzip archive.zip -d /destination/
unzip archive.zip "path/to/file.txt"
unzip archive.zip "*.rs"
unzip -l archive.zip
unzip -v archive.zip
unzip -t archive.zip
unzip -o archive.zip
unzip -n archive.zip
unzip -Z1 archive.zip
unzip -P password archive.zip
7z — 7-Zip
7z a archive.7z dir/
7z a -mx=9 archive.7z dir/
7z x archive.7z
7z x archive.7z -o/destination/
7z l archive.7z
7z t archive.7z
7z a -p archive.7z dir/
7z a -pMyPassword archive.7z dir/
7z a -p -mhe=on archive.7z dir/
7z a -sfx archive.exe dir/
7z a -v100m archive.7z dir/
7z a archive.7z dir/ -x!*.log -x!node_modules
Archive Format Comparison
| Format | Extension | Compression | Speed | Features |
|---|
| gzip | .gz | Good | Fast | Ubiquitous, single file |
| bzip2 | .bz2 | Better | Slow | Better ratio than gzip |
| xz | .xz | Best | Slowest | Best ratio, multi-thread |
| zstd | .zst | Very good | Very fast | Best speed/ratio tradeoff |
| zip | .zip | Good | Fast | Cross-platform, per-file compression |
| 7z | .7z | Excellent | Moderate | Strong encryption, best ratio |
Common Workflows
Archive Conversion
gunzip -c archive.tar.gz | xz > archive.tar.xz
gunzip -c archive.tar.gz | zstd > archive.tar.zst
mkdir tmp && unzip archive.zip -d tmp/ && tar czf archive.tar.gz -C tmp . && rm -rf tmp
Inspect Without Extracting
tar xzf archive.tar.gz -O path/to/file.txt
tar xzf archive.tar.gz -O | grep "pattern"
unzip -p archive.zip path/to/file.txt
Incremental Backups
tar czf backup-full.tar.gz --listed-incremental=snapshot.snar dir/
tar czf backup-inc1.tar.gz --listed-incremental=snapshot.snar dir/
Important Notes
tar czf — the f flag must come last, immediately before the filename
- gzip, bzip2, and xz replace the original file by default; use
-k to keep it
- zstd is the modern choice: nearly as fast as gzip with compression ratios close to xz
- zip stores compression per-file; tar compresses the entire stream (generally better ratio)
- For cross-platform sharing, zip is the safest choice (native support on Windows, macOS, Linux)
- Password-protected zip uses weak encryption (ZipCrypto); use 7z with AES-256 for security
- Split archives require all parts present for extraction
- When extracting untrusted archives, use
--strip-components=1 or extract to a subdirectory to avoid overwriting files in the current directory
- GNU tar auto-detects compression format on extraction (
tar xf works for .gz, .bz2, .xz, .zst)
Source: bug-ops/zeph — distributed by TomeVault.