πŸ’Ύ Linux/Unix: Storage, Archiving & Compression
Estimated reading: 4 minutes 461 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
gzipFastModerateLogs, single file compression
zipMediumModerate (basic)Cross-platform sharing
bzip2SlowHighBackups, smaller files
xzVery SlowVery HighArchiving large files
7zMediumHighest (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 :
Share

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

Or Copy Link

CONTENTS
Scroll to Top