Welcome to the world of Linux file compression... where space is tight, operations are swift, and every byte counts.
Inspired by the tactical grit of the Extraction movies, this guide helps you compress and extract files in a high-stakes (and disk-space-saving) way. Whether you're stashing away mission files or prepping a system for transfer, these commands will have your back.
๐ฏ Target Audience: Linux beginners
๐ฅ Inspired by: Extraction & Extraction 2
๐ ๏ธ Tools:tar
,gzip
,bzip2
,xz
โจ Table of Contents
- ๐ ๏ธ Setting Up Your Local Linux Mission Base
- ๐ฆ Basic Mission Files Setup
- ๐ Check File Size with
du -sh
- ๐ฅ Operation TAR: Archive Like a Pro
- ๐จ Operation GZIP: Quick Compression
- ๐ข Operation BZIP2: Smaller, Slower Compression
- ๐ง Operation XZ: Maximum Compression
- ๐ฃ Combine
tar
with Compression - ๐งช Compare Compression: Test It Yourself
- ๐ Compression Tool Comparison
- ๐ง Wrap-Up Compression Tips
- ๐ฎ Debrief
๐ ๏ธ Setting Up Your Local Linux Mission Base
For this guide, we assume you're using RHEL 9 or a similar distribution (CentOS Stream, Fedora, Rocky, etc.).
๐ฆ Install Compression Tools
sudo dnf install tar gzip bzip2 xz -y
๐ Basic Mission Files Setup
Create a mission folder with intel to practice on:
mkdir mission_folder
cd mission_folder
echo "Tyler Rake mission report" > report1.txt
echo "Saju operations intel" > intel2.txt
echo "Nik extraction plan" > plan3.txt
Youโll use this folder to simulate real compression and extraction ops.
๐ง Want Larger Files? Simulate Data Bloat
fallocate -l 5M largefile1.txt
dd if=/dev/urandom of=largefile2.txt bs=1M count=10
yes "extraction" | head -c 2M > largefile3.txt
๐ Check File Size with du -sh
du -sh largefile1.txt
๐ฅ Operation TAR: Archive Like a Pro
What is tar
?
tar
bundles multiple files into one archive. It does not compress by default.
โ Create Archive:
sudo tar -cvf mission.tar mission_folder/
๐ View Contents:
tar -tvf mission.tar
๐ฏ Extract Archive:
tar -xvf mission.tar
๐ฏ Extract to Specific Location:
mkdir extracted_files
tar -xvf mission.tar -C extracted_files/
๐จ Operation GZIP: Quick Compression
โ Compress:
gzip report1.txt
๐ View Without Decompressing:
zcat report1.txt.gz
๐ฏ Decompress:
gunzip report1.txt.gz
๐ข Operation BZIP2: Smaller, Slower Compression
โ Compress:
bzip2 intel2.txt
๐ฏ Decompress:
bunzip2 intel2.txt.bz2
๐ง Operation XZ: Maximum Compression
โ Compress:
xz plan3.txt
๐ View Compression Stats:
xz -l plan3.txt.xz
๐ฏ Decompress:
unxz plan3.txt.xz
๐ฃ Combine tar
with Compression: One Command to Rule the Archive
So far, you've seen how tar
bundles files, and how gzip
, bzip2
, and xz
compress single files. But when you're dealing with folders or multiple files, it's time to bring the heavy artillery.
Imagine Tyler Rake extracting a whole ops base via a chopper, but it needs to be fast, efficient, and all at once. Thatโs why we need to combine
tar
+ compression.
๐ง General Syntax
tar -[c][compression flag][v][f] archive_name.tar.[ext] folder_or_files
Option | Meaning |
---|---|
-c |
Create a new archive |
-v |
Verbose (list files) |
-f |
File name of archive |
Compression Flags: | |
-z |
Use gzip |
-j |
Use bzip2 |
-J |
Use xz |
๐ฎ Examples (With Scenarios)
1. Compress Mission Folder with gzip
tar -czvf mission.tar.gz mission_folder/
Extract:
tar -xzvf mission.tar.gz
2. Compress with bzip2
tar -cjvf mission.tar.bz2 mission_folder/
Extract:
tar -xjvf mission.tar.bz2
3. Compress with xz
tar -cJvf mission.tar.xz mission_folder/
Extract:
tar -xJvf mission.tar.xz
4. Extract to Specific Location (-C
)
mkdir /tmp/extracted_mission
tar -xvzf mission.tar.gz -C /tmp/extracted_mission/
5. Combine and Compress Select Files
tar -czvf selected.tar.gz report1.txt intel2.txt
Or:
tar -czvf logs_archive.tar.gz /var/log/messages /var/log/secure
๐งช Compare Compression: Test It Yourself
cp largefile1.txt copy1.txt
gzip copy1.txt
cp largefile1.txt copy2.txt
bzip2 copy2.txt
cp largefile1.txt copy3.txt
xz copy3.txt
du -sh largefile1.txt copy1.txt.gz copy2.txt.bz2 copy3.txt.xz
๐ Compression Tool Comparison
Tool | Compression | Speed | Ratio | Extension | Multi-File | Best For |
---|---|---|---|---|---|---|
tar |
No | Fast | N/A | .tar |
โ Yes | Archiving folders |
gzip |
Yes | Fast | Moderate | .gz |
โ No | Fast transfers |
bzip2 |
Yes | Moderate | Better | .bz2 |
โ No | Balanced compression |
xz |
Yes | Slow | Best | .xz |
โ No | Long-term storage |
๐ง Wrap-Up Compression Tips
If speed matters โ use
gzip
If you want smaller files but donโt mind waiting โ go withxz
If you want the middle ground โbzip2
offers decent size and speed
For folders or multiple files โ wrap them in atar
archive first
๐ฎ Debrief
Youโve just completed your first compression mission:
- Set up a RHEL-based environment
- Created test data and learned file size inspection
- Used
tar
,gzip
,bzip2
, andxz
individually and in combination - Compared performance through practical examples
Ready for the next mission? Share this article or drop your compression tips in the comments. Until next time, compress confidently and extract cleanly. ๐๏ธ๐ต๏ธ
Top comments (0)