Overview
bzip2 is a powerful compression utility used to reduce file sizes. Compressed files typically have the `.bz2` extension. Decompression can be performed using the `bzip2 -d` command or the `bunzip2` command.
Key Features
- Provides high compression ratios (generally better than gzip)
- Lossless compression method
- Optimized for single file compression
- Compressed files use the `.bz2` extension
Common Options
Here are the most commonly used options with the bzip2 command.
Basic Operations
Output and Performance
Generated command:
Try combining the commands.
Description:
`bzip2` Executes the command.
Combine the above options to virtually execute commands with AI.
Usage Examples
Various examples of compressing and decompressing files using the bzip2 command.
Compress a File
bzip2 myfile.txt
Compresses the specified file and deletes the original.
Decompress a File
bzip2 -d myfile.txt.bz2
Decompresses a file with the `.bz2` extension and deletes the original compressed file.
Compress While Keeping Original
bzip2 -k myfile.txt
Keeps the original file after compression.
Compress with Maximum Level
bzip2 -9 myfile.txt
Compresses a file using the highest compression ratio (slowest).
View Compressed File Content
bzcat myfile.txt.bz2
Displays the content of a compressed file to standard output without decompressing it.
Archive a Directory with tar
tar -cvjf archive.tar.bz2 mydirectory/
Uses the tar command to archive a directory and then compresses it with bzip2.
Tips & Notes
Useful tips and points to consider when using bzip2.
bzip2 vs gzip
- Compression Ratio: bzip2 generally provides a higher compression ratio than gzip.
- Speed: bzip2 is slower for both compression and decompression compared to gzip. Consider gzip if speed is critical.
File Archiving
bzip2 is used for compressing single files. To archive multiple files or directories, it's common practice to use it in conjunction with the `tar` command.
- Example: `tar -cvjf archive.tar.bz2 directory_name/`
Utilizing Standard Input/Output
bzip2 can accept standard input for compression or send decompressed data to standard output via pipes (`|`). This is useful for streaming data processing or when combining with other commands.