Linux File Compression and Archiving: tar, gzip, bzip2, xz, zstd, zip, and 7z
Archiving bundles multiple files and directories into a single container file, while compression reduces the physical byte size of files. This guide covers standard Linux archiving tools (tar), single-file compressors (gzip, bzip2, xz, zstd), multi-format utilities (zip, 7z), and integrity verification.
⚡ Quick Dive
Compression Tools Comparison Matrix
| Format | Tool | Speed | Compression Ratio | Best Use Case |
|---|---|---|---|---|
.tar |
tar |
Instant (no compression) | 1:1 (Bundled) | Tape archives, bundling before compression |
.tar.gz |
tar -czf / gzip |
⚡ Fast | Good (~60-70%) | Web assets, general server backups, software distribution |
.tar.bz2 |
tar -cjf / bzip2 |
Moderate | High (~70-80%) | Linux kernel/source archives (legacy standard) |
.tar.xz |
tar -cJf / xz |
Slow | 🏆 Highest (~80-90%) | OS installation images, distribution packages |
.tar.zst |
tar --zstd / zstd |
⚡⚡ Ultra Fast | High (tunable 1-19) | Modern container layers, real-time log archiving |
.zip |
zip / unzip |
Fast | Moderate | Cross-platform sharing (Windows/macOS/Linux) |
.7z |
7z |
Moderate/Slow | 🏆 Very High | Password-protected encrypted archives |
Essential One-Liners Cheat Sheet
# 1. Create a compressed .tar.gz archive
tar -czvf archive.tar.gz /path/to/folder
# 2. Extract any tar archive into a specific target folder
tar -xvf archive.tar.gz -C /target/destination/
# 3. Create high-compression xz archive
tar -cJvf backup.tar.xz /data/
# 4. Ultra-fast modern Zstandard compression
tar --zstd -cvf backup.tar.zst /var/log/
# 5. Extract a zip file without overwriting existing files
unzip -n archive.zip -d /extracted/
# 6. Test archive integrity without extracting to disk
tar -tvf archive.tar.gz > /dev/null && echo "Archive OK"
📖 Extended Guide
1. tar — Tape Archive Utility
tar bundles directory hierarchies into a single stream or file while preserving permissions and directory trees.
Common Flags Breakdown
-c: Create a new archive.-x: Extract files from an archive.-t: List contents of an archive without unpacking.-v: Verbose output (shows processed filenames).-f <file>: Specify the archive filename (must immediately precede the archive name).-C <dir>: Switch to target directory before extracting/creating.-p: Preserve original permissions and file modes.
Integrated Compression Flags:
-z: Filter archive throughgzip(.tar.gz,.tgz).-j: Filter archive throughbzip2(.tar.bz2,.tbz2).-J: Filter archive throughxz(.tar.xz,.txz).--zstd: Filter archive throughzstandard(.tar.zst).
Advanced Real-World tar Examples
# 1. Exclude unwanted folders (e.g., git, cache, logs) during creation
tar -czvf project_backup.tar.gz \
--exclude=".git" \
--exclude="node_modules" \
--exclude="*.log" \
/var/www/myproject
# 2. Extract a single file or directory from an archive
tar -xvf backup.tar.gz path/to/specific_file.txt
# 3. Stream directory transfer directly over SSH without saving tar to disk
tar -czf - /var/www | ssh user@backup-server "tar -xzf - -C /mnt/backups/www"
2. Single-File Compressors: gzip, bzip2, xz, and zstd
These tools compress single files in-place and replace the original file unless instructed otherwise.
gzip / gunzip (.gz)
Standard across all UNIX/Linux platforms:
# Compress file (replaces file.txt with file.txt.gz)
gzip file.txt
# Keep original file (-k) and set maximum compression level (-9)
gzip -k9 database.sql
# Decompress .gz file
gunzip database.sql.gz
# OR
gzip -d database.sql.gz
bzip2 / bunzip2 (.bz2)
Uses Burrows-Wheeler block sorting for higher compression at the cost of CPU time:
# Compress keeping original
bzip2 -k file.txt
# Decompress
bunzip2 -d file.txt.bz2
xz / unxz (.xz)
Uses LZMA2 algorithm to deliver industry-leading compression ratios:
# Compress with multi-threaded support (-T0 uses all CPU cores)
xz -T0 -k large_dump.sql
# Decompress
unxz large_dump.sql.xz
zstd / unzstd (.zst)
Facebook's modern Zstandard algorithm provides real-time compression speeds with ratios rivaling xz:
# Fast compression keeping original
zstd -k server.log
# High compression level 19 with multi-threading
zstd -19 -T0 --keep database.dump
# Decompress
unzstd database.dump.zst
3. Multi-Format Utilities: zip and 7z
zip / unzip (Cross-Platform)
# Recursively zip a directory with password encryption (-e)
zip -r -e secret_backup.zip /path/to/docs
# Update an existing zip archive with newer/modified files (-u)
zip -u archive.zip updated_file.txt
# Inspect zip archive contents without extracting
unzip -l archive.zip
# Extract to destination directory
unzip archive.zip -d /opt/extracted_app/
7z (p7zip)
Known for high-ratio LZMA/LZMA2 compression and strong AES-256 encryption:
# Create encrypted 7z archive with hidden filenames (-mhe=on)
7z a -pSecurePass123! -mhe=on secure.7z /sensitive/data/
# Extract with full directory tree preserved
7z x secure.7z -o/destination/path/
4. Parallel Acceleration with pigz
pigz (Parallel Implementation of GZip) utilizes all available CPU cores to accelerate gzip compression by orders of magnitude:
# Compress tar archive using all CPU cores with pigz
tar -I pigz -cvf backup.tar.gz /large/directory
# Direct file compression with pigz
pigz -k -p 8 massive_file.csv
5. Integrity Verification and Checksums
Always verify archive integrity before deleting source files:
# Test integrity of a .tar.gz archive
gzip -t archive.tar.gz
# Test integrity of a .zip archive
unzip -t archive.zip
# Generate and verify cryptographic SHA-256 checksums
sha256sum backup.tar.gz > backup.tar.gz.sha256
sha256sum -c backup.tar.gz.sha256