📖 Scenario: You have a folder with text files that you want to compress to save space. You will learn how to use bzip2 and xz commands to compress and decompress files in Linux.
🎯 Goal: Learn to compress a file using bzip2 and xz, then decompress it back to the original file.
📋 What You'll Learn
Use the bzip2 command to compress a file
Use the xz command to compress a file
Use the bunzip2 command to decompress a bzip2 file
Use the unxz command to decompress an xz file
Display the contents of the decompressed files
💡 Why This Matters
🌍 Real World
Compressing files saves disk space and makes it easier to transfer files over the internet or between systems.
💼 Career
System administrators and developers often compress logs, backups, and data files to optimize storage and network usage.
Progress0 / 4 steps
1
Create a sample text file
Create a file called example.txt with the exact content: Hello, this is a test file for compression.
Linux CLI
Hint
Use the echo command with redirection > to create the file.
2
Compress the file using bzip2
Use the bzip2 command to compress the file example.txt. This will create example.txt.bz2 and remove the original file.
Linux CLI
Hint
Simply run bzip2 example.txt to compress the file.
3
Decompress the bzip2 file and display content
Use the bunzip2 command to decompress example.txt.bz2 back to example.txt. Then use cat to display the content of example.txt.
Linux CLI
Hint
Use bunzip2 example.txt.bz2 to decompress and cat example.txt to show the file content.
4
Compress and decompress using xz
First, recreate example.txt with the same content. Then compress it using xz to create example.txt.xz. Next, decompress it using unxz back to example.txt. Finally, display the content of example.txt using cat.
Linux CLI
Hint
Use echo to recreate the file, xz to compress, unxz to decompress, and cat to display.
Practice
(1/5)
1. What does the bzip2 command do in Linux?
easy
A. Deletes files permanently
B. Compresses files to make them smaller
C. Changes file permissions
D. Lists files in a directory
Solution
Step 1: Understand the purpose of bzip2
The bzip2 command is used to compress files, reducing their size.
Step 2: Compare with other options
Options B, C, and D describe unrelated file operations, not compression.
Final Answer:
Compresses files to make them smaller -> Option B
Quick Check:
bzip2 compresses files = A [OK]
Hint: bzip2 always compresses files to save space [OK]
Common Mistakes:
Confusing compression with deletion
Thinking bzip2 changes file permissions
Assuming bzip2 lists files
2. Which command correctly compresses a file named data.txt using xz and keeps the original file?
easy
A. xz data.txt
B. xz -d data.txt
C. xz --delete data.txt
D. xz -k data.txt
Solution
Step 1: Identify the option to keep original files
The -k option tells xz to keep the original file after compression.
Step 2: Check other options
xz data.txt compresses but deletes original; B decompresses; D is invalid for keeping files.
Final Answer:
xz -k data.txt -> Option D
Quick Check:
Use -k to keep original = C [OK]
Hint: Use -k with xz to keep original file after compression [OK]
Common Mistakes:
Forgetting -k deletes original file
Using -d which decompresses instead
Assuming --delete keeps original
3. What is the output of the command sequence?
echo "Hello" > file.txt
bzip2 -k file.txt
ls file.txt file.txt.bz2
medium
A. file.txt.bz2
B. file.txt
C. file.txt file.txt.bz2
D. No files listed
Solution
Step 1: Understand bzip2 -k effect
The -k option keeps the original file file.txt and creates compressed file.txt.bz2.
Step 2: List files after compression
Both file.txt and file.txt.bz2 exist, so ls shows both.
Final Answer:
file.txt file.txt.bz2 -> Option C
Quick Check:
-k keeps original and compressed files = B [OK]
Hint: bzip2 -k keeps original and compressed files both [OK]
Common Mistakes:
Assuming original file is deleted
Expecting only compressed file
Thinking no files remain
4. You run xz -d file.xz but get an error saying "file.xz: No such file or directory". What is the likely problem?
medium
A. The file file.xz does not exist in the current directory
B. The -d option is invalid for decompression
C. You need to use -k to decompress
D. The file is already decompressed
Solution
Step 1: Understand the error message
The error "No such file or directory" means the file file.xz is missing in the current folder.
Step 2: Check command options
-d is correct for decompression; -k is unrelated to decompression; file must exist to decompress.
Final Answer:
The file file.xz does not exist in the current directory -> Option A
Quick Check:
File missing causes No such file error = A [OK]
Hint: Check file exists before decompressing with -d [OK]
Common Mistakes:
Thinking -d is wrong option
Using -k to decompress
Ignoring file location
5. You have a large log file server.log. You want to compress it using bzip2 but keep the original file and later decompress it. Which sequence of commands is correct?
hard
A. bzip2 -k server.log
bzip2 -d server.log.bz2
B. bzip2 server.log
bzip2 -k -d server.log.bz2
C. bzip2 -d server.log
bzip2 -k server.log.bz2
D. bzip2 -k -d server.log
bzip2 server.log.bz2
Solution
Step 1: Compress with original kept
Use bzip2 -k server.log to compress and keep the original file.
Step 2: Decompress the compressed file
Use bzip2 -d server.log.bz2 to decompress back to original format.
Final Answer:
bzip2 -k server.log
bzip2 -d server.log.bz2 -> Option A
Quick Check:
Use -k to keep original, -d to decompress = D [OK]
Hint: Compress with -k, decompress with -d for safe file handling [OK]
Common Mistakes:
Using -d to compress
Not keeping original file
Mixing options order incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.