bzip2 - macOS
Overview
bzip2 is a command-line based file compression tool similar to gzip, but utilizing the Burrows-Wheeler block sorting text compression algorithm along with Huffman coding. It is designed to offer better compression ratios than other similar utilities, particularly for text files. bzip2 is useful for compressing large files to save disk space or to make transferring files over the network more efficient.
Syntax
The basic syntax of the bzip2 command is:
bzip2 [OPTIONS] [FILES]
- [FILES]: This represents one or more files that you wish to compress or decompress.
 
Compressing files:
Without any options, bzip2 will compress the specified file(s). The original file(s) will be replaced by the compressed version(s) with a .bz2 extension.
bzip2 file1.txt
Decompressing files:
To decompress, use the -d or --decompress option:
bzip2 -d file1.txt.bz2
Options/Flags
-d,--decompress: Decompress the specified file(s).-z,--compress: Force compression, even if it has ‘.bz2’ extension (this is the default action).-k,--keep: Keep (do not delete) input files during compression or decompression.-f,--force: Force overwrite of output files.-t,--test: Check integrity of the specified compressed file.-c,--stdout: Output to standard output; keep original files unchanged.-v,--verbose: Show verbose output; can be applied multiple times for more verbosity.-q,--quiet: Suppress noncritical error messages; opposite of--verbose.-s,--small: Reduce memory usage; compression or decompression will be slower.
Examples
- 
Compressing a file:
bzip2 sample.txtThis compresses
sample.txttosample.txt.bz2and deletes the original. - 
Decompressing a file:
bzip2 -d sample.txt.bz2This restores the original
sample.txtand deletes the compressed version. - 
Keeping the original files:
bzip2 -k sample.txt bzip2 -dk sample.txt.bz2This will retain
sample.txtafter compression and keepsample.txt.bz2after decompression. - 
Output to standard output:
bzip2 -c sample.txt > sample.txt.bz2 bzip2 -dc sample.txt.bz2 > sample.txt - 
Test integrity of a compressed file:
bzip2 -t sample.txt.bz2 
Common Issues
- File Permissions: Errors may occur if 
bzip2doesn’t have the necessary permissions to read or write files or directories. - Disk Space: Ensure adequate disk space is available before attempting to compress or decompress large files.
 - Corrupted Files: Corrupt compressed files might not decompress, yielding a file error. Recovery is usually not possible.
 
Integration
bzip2 can be effectively combined with other commands for advanced tasks. For instance:
- 
Archiving and compressing a directory:
tar -cvf - directory_name/ | bzip2 > directory_name.tar.bz2 - 
Searching within compressed files without decompression:
bzip2 -dc file.txt.bz2 | grep 'search-string' 
Related Commands
gzip: Another file compression tool, generally faster but with less compression ratio.zip: Archive and compression tool, handling multiple files by default.tar: Archive tool, often used with compression tools likebzip2.
For further reading, the official documentation here provides more detailed information.