ποΈ 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 :
