Compress with bzip2
Compress with bzip2
Gzip is not the only compression format you will meet. bzip2 uses a different encoding and commonly gives its output a .bz2 name. A bzip2 stream needs a tool that understands that format; changing its filename does not make it interchangeable with a gzip stream.
Let's repeat the report round trip with this tool. The useful skill is choosing the matching format and input/output behavior. Do not assume one compressor is always best: size, CPU time and compatibility depend on your data and the tools available to the receiver.
What you’ll learn
- Compress and decompress a report with bzip2.
- Select the matching format and preserve a compressed input when reading it.
The format determines how bytes are restored
Original
The report contains the mark line.
marks.txtCompressed
bzip2 -c produces a bzip2 stream while retaining the source.
marks.txt.bz2Restored output
bzip2 -dc reconstructs the text on standard output.
Diya: 72
Round-trip the mark report through bzip2
Prepare the example
Use a fresh directory with bzip2 installed. The first line writes the input report. We will preserve it while creating a compressed file, then read restored bytes through standard output so the compressed input remains available too.
Bash · Linux terminal
printf 'Diya: 72\n' > marks.txt
bzip2 -c marks.txt > marks.txt.bz2
bzip2 -dc marks.txt.bz2Expected result
Diya: 72bzip2 -c marks.txt encodes the report and writes the resulting stream to standard output. Redirection stores it in marks.txt.bz2. As in the gzip output-mode example, the source is not replaced by this operation.
bzip2 -dc marks.txt.bz2 uses the same format in the reverse direction: -d asks for decompression, while -c sends the restored content to output. The mark line should match the original. A gzip decompression command is not the right choice merely because this is also a compressed file; the format-specific tool must understand the bytes.
Prepare a report and compress its bytes
bzip2 is a file compressor that produces files ending in .bz2. Like gzip, it works on individual files and normally replaces the uncompressed source.
What is bzip2?
bzip2 is a command-line utility that compresses single files using the Burrows–Wheeler algorithm. It usually provides better compression ratios than gzip.
Step 1: Check if bzip2 is installed
On most modern Linux distros, bzip2 comes pre-installed. But you can verify it with this command:
bzip2 --version
If it's not installed, you can add it using your package manager:
sudo apt install bzip2 # Debian/Ubuntu
sudo yum install bzip2 # RHEL/CentOS
sudo dnf install bzip2 # Fedora
Step 2: Create a sample file
Let’s create a test file to practice with:
echo "This is a test file for compression." > testfile.txt
Step 3: Compress the file
Now let’s compress it using bzip2:
bzip2 testfile.txt
This command will replace the original file with a compressed version:
testfile.txt.bz2
Notice that the original testfile.txt is gone — it’s now inside the compressed file testfile.txt.bz2.
Preserving inputs and decompressing the result
Want to Keep the Original File?
Use the -k (keep) option:
bzip2 -k testfile.txt
testfile.txt
testfile.txt.bz2
Step 4: View the compressed file size
ls -lh testfile.txt.bz2
Step 5: Decompress the file
To unzip it back to the original form, use bunzip2:
bunzip2 testfile.txt.bz2
Or use the -d flag with bzip2:
bzip2 -d testfile.txt.bz2
Compress Multiple Files?
bzip2 only works on single files. If you want to compress a whole folder or multiple files, you should first archive them using tar:
tar -cvjf archive.tar.bz2 foldername/
This tar command uses these options:
-c: creates an archive-v: shows progress-j: compresses usingbzip2-f: specifies the filename
Compress and restore files with bzip2
bzip2 filename→ compresses the filebunzip2 filename.bz2→ decompresses it-koption → keeps original filetar -cvjf→ compresses directories
And that’s it! You now know how to compress and decompress files using bzip2. Keep practicing and try it on your own files.
Try it, then check the worked answer
Your task: Read a bzip2-compressed report without replacing it.
Use a fresh directory with bzip2 installed. The first line writes the input report. We will preserve it while creating a compressed file, then read restored bytes through standard output so the compressed input remains available too.
Bash · complete example for this question
printf 'Diya: 72\n' > marks.txt
bzip2 -c marks.txt > marks.txt.bz2
bzip2 -dc marks.txt.bz2Expected result
Diya: 72Worked answer
bzip2 -dc marks.txt.bz2
Use bzip2 -dc marks.txt.bz2 to read the restored report while keeping the compressed input. -c without decompression would encode the already-compressed stream again, which is not the restoration task. Choose the direction as well as the output destination.
Check your decision
Match the decompressor to the compressed format
Compression tools share some ideas, but their formats are not interchangeable. Match the decompressor to the data and make input preservation explicit. For a practical choice, compare compatibility and resource use on representative reports rather than relying on a universal claim about the best ratio.
Reaching the recap and completing the quizzes are separate milestones.