The bzip2 command in Unix and Linux is used for compressing and decompressing files using the Burrows-Wheeler block-sorting text compression algorithm and Huffman coding. It offers high compression ratios and is commonly used to compress large files for storage or transmission.
The basic syntax for the bzip2 command is:
bzip2 [options] [file(s)]options: Optional command-line options to control the compression level, verbosity, and other settings.file(s): The name(s) of the file(s) to compress or decompress.
To compress a file using bzip2:
bzip2 filename.txtThis command compresses filename.txt and creates a compressed file filename.txt.bz2.
To decompress a .bz2 file:
bzip2 -d filename.txt.bz2This command decompresses filename.txt.bz2 and restores it to filename.txt.
To achieve maximum compression (slower but smaller file size):
bzip2 -9 filename.txtThis command compresses filename.txt with the highest compression level (-9), resulting in the smallest possible file size.
-1to-9: Specify the compression level (1 for fastest compression, 9 for best compression ratio).
-d: Decompress the specified.bz2file.
-v: Verbose mode, display compression statistics.
-k: Keep the original file after compression or decompression.
-t: Test the integrity of compressed files.
To compress large data files for archiving or transmission:
bzip2 -9 largefile.datThis command compresses largefile.dat with maximum compression to save storage space.
To compress multiple files into separate .bz2 archives:
bzip2 -z file1.txt file2.txtThis command compresses file1.txt and file2.txt into file1.txt.bz2 and file2.txt.bz2, respectively.
To compress all .log files in a directory and its subdirectories:
find /path/to/logs -name "*.log" -exec bzip2 {} \;This command uses find to locate all .log files under /path/to/logs and compresses each one with bzip2.
The bzip2 command is a robust tool for compressing and decompressing files using the Burrows-Wheeler algorithm in Unix and Linux systems. It offers options for controlling compression levels, verbosity, and file integrity testing, making it versatile for various compression tasks. Understanding its usage and options can help you effectively manage file compression and storage on your system.
usage: bzip2 [flags and input files in any order]
-h --help print this message
-d --decompress force decompression
-z --compress force compression
-k --keep keep (don't delete) input files
-f --force overwrite existing output files
-t --test test compressed file integrity
-c --stdout output to standard out
-q --quiet suppress noncritical error messages
-v --verbose be verbose (a 2nd -v gives more)
-L --license display software version & license
-V --version display software version & license
-s --small use less memory (at most 2500k)
-1 .. -9 set block size to 100k .. 900k
--fast alias for -1
--best alias for -9
If invoked as `bzip2', default action is to compress.
as `bunzip2', default action is to decompress.
as `bzcat', default action is to decompress to stdout.