Welcome to Day 9 of the 30 Days of Linux Challenge. Today’s focus is on something every system administrator or DevOps engineer needs: compressing and archiving files efficiently using the terminal in Red Hat-based systems (RHEL, CentOS, Rocky Linux, AlmaLinux).
Why? Because whether you're backing up logs, preparing files for transfer, or reducing storage space, file compression is a must-have skill — and Red Hat provides a full suite of powerful CLI tools to do just that.
📚 Table of Contents
- Why Compression and Archiving Matter
- Archiving with tar
- Compression with gzip, bzip2, and xz
- Combining tar with Compression
- Extracting Compressed Archives
- Try It Yourself
- Why This Matters in the Real World
Why Compression and Archiving Matter
Compression and archiving are core parts of system management:
- 📦 Reduce large files to save disk space
- 🔄 Group multiple files into a single archive (tarball)
- 📁 Create backup packages for configuration directories
- 📤 Optimize file transfers over slow or limited networks
- 📜 Bundle and preserve logs, reports, or build artifacts
These are especially important in cloud deployments, remote system management, and disaster recovery planning.
Archiving with tar
The tar
(tape archive) utility is used to group multiple files or directories into a single uncompressed .tar
archive.
Create a tar archive:
tar -cvf archive.tar file1 file2 directory/
c: create
v: verbose (list files during archive creation)
f: file name of the archive
Extract a tar archive:
- tar -xvf archive.tar
View contents of a tar file:
- tar -tvf archive.tar
Tar doesn’t compress by default — it’s just a packaging tool. To compress, we pair it with gzip, bzip2, or xz.
Compression with gzip, bzip2, and xz
These tools compress single files, each with different strengths:
Tool Extension Speed
gzip .gz Fast Medium
bzip2 .bz2 Slower Better
xz .xz Slowest Best
Compress a single file:
- gzip file.txt
- bzip2 file.txt
- xz file.txt
Decompress:
- gunzip file.txt.gz
- bunzip2 file.txt.bz2
- unxz file.txt.xz
Note: These tools replace the original file by default.
Combining tar with Compression
You can combine tar and compression in one command to create compressed archives.
.tar.gz (gzip):
tar -czvf archive.tar.gz /path/to/dir
.tar.bz2 (bzip2):
tar -cjvf archive.tar.bz2 /path/to/dir
.tar.xz (xz):
tar -cJvf archive.tar.xz /path/to/dir
Use uppercase J for xz, lowercase j for bzip2, and z for gzip.
Extracting Compressed Archives
Depending on the compression format, use:
Gzip:
tar -xzvf archive.tar.gz
Bzip2:
tar -xjvf archive.tar.bz2
XZ:
tar -xJvf archive.tar.xz
Try It Yourself
Here’s a hands-on exercise for Day 9:
Create sample files
mkdir ~/compression_test && cd ~/compression_test
echo "sample 1" > one.txt
echo "sample 2" > two.txt
Archive and compress
tar -czvf logs.tar.gz one.txt two.txt
Extract
tar -xzvf logs.tar.gz
Compress a single file with xz
xz one.txt
ls -lh
unxz one.txt.xz
Why This Matters in the Real World
- 🚀 Developers use tar.gz to distribute source code
- 🧳 Sysadmins compress logs for storage and transfers
- 🛡️ Backup scripts often use .tar.xz to minimize file size
- ☁️ DevOps engineers use these formats in CI/CD pipelines
Whether you're troubleshooting, automating tasks, or deploying applications — archiving and compression are daily tools in your Linux toolkit.
Top comments (0)