ποΈ Linux/Unix: Compression Tools β tar, gzip, zip, bzip2, xz, 7z Explained with Examples
π§² Introduction β Why Learn Compression in Linux?
Compression tools are essential in Linux for reducing file sizes, archiving data, sharing via networks, and backups. Linux offers powerful utilities like tar, gzip, zip, bzip2, xz, and 7zβeach with different compression algorithms, speeds, and use cases.
π― In this guide, youβll learn:
- How to compress and extract files using popular Linux tools
- Key differences between formats like .tar.gz,.zip,.bz2,.xz, and.7z
- Practical commands with output and explanations
π¦ 1. tar β Archive Multiple Files Together
β
 What is tar?
tar (tape archive) combines multiple files into a single archive. It doesnβt compress by default, but itβs often used with gzip, bzip2, or xz.
π οΈ Syntax:
tar [options] archive.tar file...
πΉ Common Options:
| Option | Description | 
|---|---|
| -c | Create archive | 
| -x | Extract archive | 
| -v | Verbose (list files) | 
| -f | Specify filename | 
| -z | Use gzip | 
| -j | Use bzip2 | 
| -J | Use xz | 
π§ͺ Example 1: Create a .tar.gz archive
tar -czvf archive.tar.gz folder/
π€ Output:
folder/
folder/file1.txt
folder/file2.log
π§ͺ Example 2: Extract a .tar.gz archive
tar -xzvf archive.tar.gz
π§ Extensions:
- .tarβ uncompressed archive
- .tar.gzor- .tgzβ gzip-compressed
- .tar.bz2β bzip2-compressed
- .tar.xzβ xz-compressed
π 2. gzip β Compress Single Files Fast
β
 What is gzip?
gzip compresses a single file, replacing the original with .gz. It’s fast and commonly used with tar.
π οΈ Syntax:
gzip [options] file
gunzip file.gz  # to decompress
π§ͺ Example:
gzip logfile.log
π€ Output:
logfile.log.gz
To decompress:
gunzip logfile.log.gz
π§  gzip is fast, but not as compact as bzip2 or xz.
π 3. zip β Compress with Windows Compatibility
β
 What is zip?
zip compresses and archives in one step. It works well for cross-platform use (Linux β Windows).
π οΈ Syntax:
zip archive.zip file1 file2
unzip archive.zip
π§ͺ Example:
zip myfiles.zip file1.txt file2.txt
π€ Output:
  adding: file1.txt (deflated 60%)
  adding: file2.txt (deflated 40%)
π§  Supports individual file compression, unlike tar.
𧬠4. bzip2 β Higher Compression Ratio than gzip
β
 What is bzip2?
bzip2 compresses single files with better compression than gzip, but it’s slower.
π οΈ Syntax:
bzip2 file
bunzip2 file.bz2
π§ͺ Example:
bzip2 backup.sql
π€ Output:
backup.sql.bz2
To decompress:
bunzip2 backup.sql.bz2
π§  Use tar -j to create .tar.bz2 archives.
βοΈ 5. xz β Best Compression Ratio (Slow)
β
 What is xz?
xz compresses single files with high compression ratio, better than gzip/bzip2, but takes more time.
π οΈ Syntax:
xz file
unxz file.xz
π§ͺ Example:
xz large.log
π€ Output:
large.log.xz
To decompress:
unxz large.log.xz
π§  Use tar -J to create .tar.xz archives.
π 6. 7z β High-Ratio Compression with AES Encryption
β
 What is 7z?
7z uses the LZMA algorithm, supports strong encryption, and offers high compression ratios. Great for backups and large archives.
π¦ Install:
sudo apt install p7zip-full
π οΈ Syntax:
7z a archive.7z file1 file2
7z x archive.7z
π§ͺ Example 1: Create a 7z archive
7z a secure.7z secrets.txt
π§ͺ Example 2: Extract a 7z archive
7z x secure.7z
π§  Add -p for password:
7z a -pMySecret secure.7z secrets.txt
π§ Comparison Table of Compression Tools
| Tool | Compresses | Archive Support | Speed | Ratio | Encryption | Ideal Use Case | 
|---|---|---|---|---|---|---|
| tar | β (archive only) | β (with gzip, etc) | Fast | β | β | Combining files | 
| gzip | β | β | Fast | Moderate | β | Logs, single file compression | 
| zip | β | β | Medium | Moderate | β (basic) | Cross-platform sharing | 
| bzip2 | β | β | Slow | High | β | Backups, smaller files | 
| xz | β | β | Very Slow | Very High | β | Archiving large files | 
| 7z | β | β | Medium | Highest | β (AES) | Encrypted backups | 
π Summary β Recap & Next Steps
Linux offers a tool for every compression needβfrom fast logs to encrypted archives. Whether you’re scripting backups, archiving files, or sending compressed content, these tools offer flexibility, speed, and control.
π Key Takeaways:
- Use tar+gzipfor common archives.
- Use zipfor cross-platform compatibility.
- Use xzor7zfor best compression ratio.
- Use bzip2for balance between speed and space.
β FAQs
β How do I compress a directory into .tar.gz?
β
 Use:
tar -czvf archive.tar.gz dirname/
β Which format offers best compression?
β
 xz and 7z offer the best ratios but take more time.
β Can I extract .tar.gz files with unzip?
β No. Use:
tar -xzvf archive.tar.gz
β Can I password-protect with zip or 7z?
β
 Yes. Use:
zip -e secure.zip file.txt
7z a -p password archive.7z
β How to list contents of a tar archive without extracting?
β
 Use:
tar -tvf archive.tar.gz
Share Now :
