bzip2: Compress or decompress files
August 9th, 2024 1:35 PM Mr. Q Categories: Command
Command: bzip2
/ bunzip2
The bzip2
command is used to compress files using the bzip2 compression algorithm, which provides better compression ratios compared to gzip
. The bunzip2
command is used to decompress files compressed with bzip2
.
Sample Commands and Outputs:
bzip2 file.txt
: Compresses thefile.txt
file, creatingfile.txt.bz2
. Sample Command and Output:
$ bzip2 file.txt
Description:
file.txt
: The file to be compressed.- After execution,
file.txt
is replaced byfile.txt.bz2
, and the original file is deleted. bunzip2 file.txt.bz2
: Decompresses thefile.txt.bz2
file, restoring the originalfile.txt
. Sample Command and Output:
$ bunzip2 file.txt.bz2
Description:
file.txt.bz2
: The compressed file to be decompressed.- After execution,
file.txt.bz2
is replaced byfile.txt
, restoring the original file. bzip2 -c file.txt > file.txt.bz2
: Compressesfile.txt
and writes the output tofile.txt.bz2
, preserving the original file. Sample Command and Output:
$ bzip2 -c file.txt > file.txt.bz2
Description:
-c
: Write the compressed output to standard output (stdout), allowing redirection to a file.> file.txt.bz2
: Redirects the output tofile.txt.bz2
, leaving the originalfile.txt
intact.bunzip2 -c file.txt.bz2 > file.txt
: Decompressesfile.txt.bz2
and writes the output tofile.txt
, preserving the compressed file. Sample Command and Output:
$ bunzip2 -c file.txt.bz2 > file.txt
Description:
-c
: Write the decompressed output to standard output (stdout), allowing redirection to a file.> file.txt
: Redirects the output tofile.txt
, leaving the compressedfile.txt.bz2
intact.bzip2 -d file.txt.bz2
: Decompressesfile.txt.bz2
using an alternative tobunzip2
. Sample Command and Output:
$ bzip2 -d file.txt.bz2
Description:
-d
: Decompress the file. This is equivalent to usingbunzip2
.bzip2 -l file.txt.bz2
: Lists information about the compressed file. Sample Command and Output:
$ bzip2 -l file.txt.bz2
compressed uncompressed ratio uncompressed_name
1234 5678 78.0% file.txt
Description:
-l
: Lists details about the compressed file, including its original size and the compression ratio.
Note: bzip2
provides high compression ratios and is often used for compressing individual files. For compressing multiple files or directories, you may combine tar
with bzip2
(e.g., tar -cvjf archive.tar.bz2 /path/to/directory
).