0
0
Linux CLIscripting~10 mins

bzip2 and xz compression in Linux CLI - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - bzip2 and xz compression
Start with file
Choose compression tool
bzip2
Compress file
Create .bz2
File size smaller
Decompress when needed
Original file restored
This flow shows how you start with a file, pick bzip2 or xz to compress it, get a smaller file, and later decompress it back.
Execution Sample
Linux CLI
bzip2 example.txt
ls example.txt.bz2
bzip2 -d example.txt.bz2
xz example.txt
ls example.txt.xz
Compress 'example.txt' with bzip2 and xz, then list the compressed files.
Execution Table
StepCommandActionOutputFile State
1bzip2 example.txtCompress example.txt using bzip2No output if successexample.txt.bz2 created, example.txt removed
2ls example.txt.bz2List compressed fileexample.txt.bz2example.txt.bz2 exists
3bzip2 -d example.txt.bz2Decompress bzip2 fileNo output if successexample.txt restored, example.txt.bz2 removed
4xz example.txtCompress example.txt using xzNo output if successexample.txt.xz created, example.txt removed
5ls example.txt.xzList compressed fileexample.txt.xzexample.txt.xz exists
6xz -d example.txt.xzDecompress xz fileNo output if successexample.txt restored, example.txt.xz removed
7ls example.txtList original fileexample.txtexample.txt exists, no compressed files
8ls example.txt.bz2 example.txt.xzCheck compressed filesls: cannot access 'example.txt.bz2': No such file or directory ls: cannot access 'example.txt.xz': No such file or directoryNo compressed files exist
💡 Compression commands remove original file and create compressed file; decompression restores original and removes compressed file.
Variable Tracker
FileStartAfter bzip2 compressAfter bzip2 decompressAfter xz compressAfter xz decompress
example.txtexistsremovedrestoredremovedrestored
example.txt.bz2not existcreatedremovednot existnot exist
example.txt.xznot existnot existnot existcreatedremoved
Key Moments - 3 Insights
Why does the original file disappear after compression?
Because both bzip2 and xz by default delete the original file after creating the compressed version, as shown in execution_table steps 1 and 4.
How do I get my original file back after compression?
Use the decompression commands 'bzip2 -d' or 'xz -d' which restore the original file and remove the compressed file, as seen in steps 3 and 6.
Can I keep the original file after compression?
Yes, by adding the '-k' option (e.g., 'bzip2 -k example.txt'), the original file stays, but this is not shown in the current execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the file state after step 1?
Aexample.txt and example.txt.bz2 both exist
Bexample.txt exists, example.txt.bz2 removed
Cexample.txt removed, example.txt.bz2 created
DNo files exist
💡 Hint
Check the 'File State' column in row for step 1.
At which step does the original file get restored after bzip2 compression?
AStep 3
BStep 5
CStep 6
DStep 2
💡 Hint
Look for decompression of bzip2 in the 'Command' column.
If you add '-k' option to bzip2, how would the file state after compression change?
AOriginal file removed, compressed file created
BOriginal file kept, compressed file created
COnly original file exists, no compressed file
DBoth files removed
💡 Hint
Recall the key moment about keeping original files with '-k' option.
Concept Snapshot
bzip2 and xz compress files to save space.
Use 'bzip2 filename' or 'xz filename' to compress.
Original file is removed by default.
Use '-d' option to decompress and restore original.
Add '-k' to keep original file after compression.
Full Transcript
This lesson shows how to compress files using bzip2 and xz commands in Linux. When you run 'bzip2 example.txt', it compresses the file and removes the original, creating 'example.txt.bz2'. Similarly, 'xz example.txt' creates 'example.txt.xz' and removes the original. To get the original file back, use 'bzip2 -d example.txt.bz2' or 'xz -d example.txt.xz'. By default, compression deletes the original file, but you can keep it by adding the '-k' option. The execution table traces these steps and file states clearly.