DEV Community

LaTerral Williams
LaTerral Williams

Posted on

๐Ÿ“ƒ Mission: Compression in Linux - Inspired by Extraction

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


Image description

โœจ Table of Contents


๐Ÿ› ๏ธ 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
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“Ž 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

๐Ÿ” Check File Size with du -sh

du -sh largefile1.txt
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”ฅ 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/
Enter fullscreen mode Exit fullscreen mode

๐Ÿ” View Contents:

tar -tvf mission.tar
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Extract Archive:

tar -xvf mission.tar
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Extract to Specific Location:

mkdir extracted_files
tar -xvf mission.tar -C extracted_files/
Enter fullscreen mode Exit fullscreen mode

๐Ÿ’จ Operation GZIP: Quick Compression

โœ… Compress:

gzip report1.txt
Enter fullscreen mode Exit fullscreen mode

๐Ÿ” View Without Decompressing:

zcat report1.txt.gz
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Decompress:

gunzip report1.txt.gz
Enter fullscreen mode Exit fullscreen mode

๐Ÿข Operation BZIP2: Smaller, Slower Compression

โœ… Compress:

bzip2 intel2.txt
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Decompress:

bunzip2 intel2.txt.bz2
Enter fullscreen mode Exit fullscreen mode

๐Ÿง˜ Operation XZ: Maximum Compression

โœ… Compress:

xz plan3.txt
Enter fullscreen mode Exit fullscreen mode

๐Ÿ” View Compression Stats:

xz -l plan3.txt.xz
Enter fullscreen mode Exit fullscreen mode

๐ŸŽฏ Decompress:

unxz plan3.txt.xz
Enter fullscreen mode Exit fullscreen mode

Image description


๐Ÿ’ฃ 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
Enter fullscreen mode Exit fullscreen mode
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/
Enter fullscreen mode Exit fullscreen mode

Extract:

tar -xzvf mission.tar.gz
Enter fullscreen mode Exit fullscreen mode

2. Compress with bzip2

tar -cjvf mission.tar.bz2 mission_folder/
Enter fullscreen mode Exit fullscreen mode

Extract:

tar -xjvf mission.tar.bz2
Enter fullscreen mode Exit fullscreen mode

3. Compress with xz

tar -cJvf mission.tar.xz mission_folder/
Enter fullscreen mode Exit fullscreen mode

Extract:

tar -xJvf mission.tar.xz
Enter fullscreen mode Exit fullscreen mode

Image description

4. Extract to Specific Location (-C)

mkdir /tmp/extracted_mission
tar -xvzf mission.tar.gz -C /tmp/extracted_mission/
Enter fullscreen mode Exit fullscreen mode

Image description

5. Combine and Compress Select Files

tar -czvf selected.tar.gz report1.txt intel2.txt
Enter fullscreen mode Exit fullscreen mode

Or:

tar -czvf logs_archive.tar.gz /var/log/messages /var/log/secure
Enter fullscreen mode Exit fullscreen mode

๐Ÿงช 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
Enter fullscreen mode Exit fullscreen mode

Image description


๐Ÿ“Š 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 with xz

If you want the middle ground โ†’ bzip2 offers decent size and speed

For folders or multiple files โ†’ wrap them in a tar 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, and xz 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)