πŸ’Ύ Linux/Unix: Storage, Archiving & Compression
Estimated reading: 4 minutes 64 views

πŸ—œοΈ 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:

OptionDescription
-cCreate archive
-xExtract archive
-vVerbose (list files)
-fSpecify filename
-zUse gzip
-jUse bzip2
-JUse 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.gz or .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

ToolCompressesArchive SupportSpeedRatioEncryptionIdeal Use Case
tar❌ (archive only)βœ… (with gzip, etc)Fastβ€“βŒCombining files
gzipβœ…βŒFastModerate❌Logs, single file compression
zipβœ…βœ…MediumModerateβœ… (basic)Cross-platform sharing
bzip2βœ…βŒSlowHigh❌Backups, smaller files
xzβœ…βŒVery SlowVery High❌Archiving large files
7zβœ…βœ…MediumHighestβœ… (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 + gzip for common archives.
  • Use zip for cross-platform compatibility.
  • Use xz or 7z for best compression ratio.
  • Use bzip2 for 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

πŸ”΅ Linux/Unix: Compression Tools (tar, gzip, zip, bzip2, xz, 7z)

Or Copy Link

CONTENTS
Scroll to Top