Bird
Raised Fist0
Linux CLIscripting~5 mins

bzip2 and xz compression in Linux CLI - Commands & Configuration

Choose your learning style10 modes available
Introduction
Sometimes files are too large to store or send easily. bzip2 and xz help by making files smaller using compression. This saves space and speeds up transfers.
When you want to reduce the size of a log file before archiving it.
When you need to send a large text file over email and want it smaller.
When you want to save disk space by compressing old documents.
When you want to compress a backup folder into a single smaller file.
When you want to decompress a downloaded file compressed with bzip2 or xz.
Commands
This command compresses the file example.txt using bzip2. It replaces the original file with a compressed file named example.txt.bz2.
Terminal
bzip2 example.txt
Expected OutputExpected
No output (command runs silently)
This command lists the compressed file to confirm it was created and to see its size.
Terminal
ls -l example.txt.bz2
Expected OutputExpected
-rw-r--r-- 1 user user 1234 Apr 27 12:00 example.txt.bz2
→
-l - Shows detailed file information including size
This command decompresses the bzip2 file back to the original example.txt file.
Terminal
bunzip2 example.txt.bz2
Expected OutputExpected
No output (command runs silently)
This command compresses example.txt using xz compression. It replaces the original file with example.txt.xz.
Terminal
xz example.txt
Expected OutputExpected
No output (command runs silently)
This command decompresses the xz compressed file back to example.txt.
Terminal
unxz example.txt.xz
Expected OutputExpected
No output (command runs silently)
Key Concept

If you remember nothing else from this pattern, remember: bzip2 and xz compress files by replacing the original with a smaller compressed file, and bunzip2 or unxz restore them.

Common Mistakes
Trying to compress a file without write permission.
The command fails because it cannot replace the original file with the compressed version.
Make sure you have write permission on the file or compress a copy instead.
Using bunzip2 or unxz on a file that is not compressed with the matching tool.
The decompression fails because the file format does not match the tool.
Use bunzip2 only on .bz2 files and unxz only on .xz files.
Expecting the original file to remain after compression.
bzip2 and xz delete the original file by default after compression.
Use the -k flag to keep the original file if needed.
Summary
Use bzip2 or xz to compress files and reduce their size.
bunzip2 and unxz decompress files compressed by bzip2 and xz respectively.
By default, compression replaces the original file unless you use the -k flag.

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