Enhancing Skills

bzip2: Compress or decompress files

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 the file.txt file, creating file.txt.bz2. Sample Command and Output:
  $ bzip2 file.txt

Description:

  • file.txt: The file to be compressed.
  • After execution, file.txt is replaced by file.txt.bz2, and the original file is deleted.
  • bunzip2 file.txt.bz2: Decompresses the file.txt.bz2 file, restoring the original file.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 by file.txt, restoring the original file.
  • bzip2 -c file.txt > file.txt.bz2: Compresses file.txt and writes the output to file.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 to file.txt.bz2, leaving the original file.txt intact.
  • bunzip2 -c file.txt.bz2 > file.txt: Decompresses file.txt.bz2 and writes the output to file.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 to file.txt, leaving the compressed file.txt.bz2 intact.
  • bzip2 -d file.txt.bz2: Decompresses file.txt.bz2 using an alternative to bunzip2. Sample Command and Output:
  $ bzip2 -d file.txt.bz2

Description:

  • -d: Decompress the file. This is equivalent to using bunzip2.
  • 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).


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.