Bird
Raised Fist0
Linux CLIscripting~20 mins

bzip2 and xz compression in Linux CLI - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Compression Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1:30remaining
Output of bzip2 compression command
What is the output of the following command sequence on a file named example.txt containing the text "hello world"?

cat example.txt | bzip2 > example.txt.bz2 && bzip2 -d -c example.txt.bz2
Linux CLI
echo 'hello world' > example.txt
cat example.txt | bzip2 > example.txt.bz2 && bzip2 -d -c example.txt.bz2
A
bzip2: (stdin) is not a valid bzip2 file
B
hello world.bz2
C
hello world
D
example.txt.bz2
Attempts:
2 left
💡 Hint
Think about what the bzip2 -d -c command does with the compressed file.
💻 Command Output
intermediate
1:30remaining
Comparing xz and bzip2 compression sizes
Given a file data.txt with repetitive content, which command will most likely produce the smallest compressed file size?
Axz -z data.txt
Bbzip2 -z data.txt
Ccat data.txt > data.txt.xz
Dgzip -z data.txt
Attempts:
2 left
💡 Hint
Consider which compression tool generally achieves better compression ratios on repetitive data.
🔧 Debug
advanced
2:00remaining
Why does this bzip2 command fail?
You run the command bzip2 -d example.txt.bz2 but get the error: bzip2: Can't decompress file: No such file or directory. The file example.txt.bz2 exists in your current directory. What is the most likely cause?
Linux CLI
bzip2 -d example.txt.bz2
AYou do not have read permission on the file
BThe file is actually compressed with xz, not bzip2
CThe file is corrupted and cannot be decompressed
DYou are running the command in a different directory than where the file is located
Attempts:
2 left
💡 Hint
Check your current directory and the file location carefully.
🧠 Conceptual
advanced
1:30remaining
Difference in compression options between bzip2 and xz
Which statement correctly describes a difference between bzip2 and xz compression tools?
A<code>xz</code> supports higher compression levels and multi-threading, <code>bzip2</code> does not
B<code>bzip2</code> compresses faster and produces smaller files than <code>xz</code>
C<code>bzip2</code> supports multi-threaded compression by default, <code>xz</code> does not
D<code>xz</code> cannot decompress files compressed by <code>bzip2</code>
Attempts:
2 left
💡 Hint
Think about modern compression features and performance.
🚀 Application
expert
2:30remaining
Automate compression and decompression with error handling
You want to write a shell script that compresses a file using xz and then decompresses it back, printing the decompressed content. The script should check if the compression succeeded before decompressing. Which script snippet correctly implements this?
Axz file.txt && xz -d -c file.txt.xz
Bxz file.txt; if [ $? -eq 0 ]; then xz -d -c file.txt.xz; fi
Cxz file.txt || echo 'Compression failed'; xz -d -c file.txt.xz
Dxz file.txt && echo 'Compression done'; xz -d -c file.txt.xz
Attempts:
2 left
💡 Hint
Use shell conditional statements to check command success.

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

  1. Step 1: Understand the purpose of bzip2

    The bzip2 command is used to compress files, reducing their size.
  2. Step 2: Compare with other options

    Options B, C, and D describe unrelated file operations, not compression.
  3. Final Answer:

    Compresses files to make them smaller -> Option B
  4. 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

  1. Step 1: Identify the option to keep original files

    The -k option tells xz to keep the original file after compression.
  2. Step 2: Check other options

    xz data.txt compresses but deletes original; B decompresses; D is invalid for keeping files.
  3. Final Answer:

    xz -k data.txt -> Option D
  4. 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

  1. Step 1: Understand bzip2 -k effect

    The -k option keeps the original file file.txt and creates compressed file.txt.bz2.
  2. Step 2: List files after compression

    Both file.txt and file.txt.bz2 exist, so ls shows both.
  3. Final Answer:

    file.txt file.txt.bz2 -> Option C
  4. 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

  1. Step 1: Understand the error message

    The error "No such file or directory" means the file file.xz is missing in the current folder.
  2. Step 2: Check command options

    -d is correct for decompression; -k is unrelated to decompression; file must exist to decompress.
  3. Final Answer:

    The file file.xz does not exist in the current directory -> Option A
  4. 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

  1. Step 1: Compress with original kept

    Use bzip2 -k server.log to compress and keep the original file.
  2. Step 2: Decompress the compressed file

    Use bzip2 -d server.log.bz2 to decompress back to original format.
  3. Final Answer:

    bzip2 -k server.log bzip2 -d server.log.bz2 -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard