bunzip2 Command in Linux



The bunzip2 command is a robust tool used to decompress files that were compressed using the bzip2 compression algorithm. This command essentially reverses the compression process, restoring the original file.

bunzip2 expects a list of compressed file names as input. These files should have been compressed using bzip2. Each compressed file is replaced by its decompressed version.

The decompressed file retains the same modification date, permissions, and ownership as the original compressed file, ensuring that these properties are preserved.

bunzip2 is designed to decompress files that were specifically compressed using the bzip2 algorithm. If you try to decompress a file that wasn’t created by bzip2, bunzip2 will detect this (using hex values) and ignore the file. It will also issue a warning to inform you that the file wasn’t processed.

When decompressing a file, bunzip2 tries to guess the original filename based on the extension of the compressed file. Here are the rules it follows −

  • filename.bz2 -> filename
  • filename.bz -> filename

If the compressed file doesn’t have one of the recognized extensions (i.e., .bz2, .bz, .tbz2, .tbz), bunzip2 will append “.out” to the original filename.

For instance, if you have a file named datafile (without any of the recognized extensions), after decompression, it will be named datafile.out.

This behavior ensures that the decompressed files have meaningful and recognizable names, even if the original compressed file names don’t follow the standard conventions.

Table of Contents

Here is a comprehensive guide to the options available with the bunzip2 command −

Syntax of bunzip2 Command

The syntax for the bunzip2 command is quite straightforward. Here is how it looks like −

bunzip2 [options] [file ...]

bunzip2 Command Options

The following are different options you can use with the bunzip2 command −

Options Description
-h, --help Prints the help message, showing all available options.
-d, --decompress Forces decompression of the input files.
-z, --compress Forces compression of the input files.
-k, --keep keep (don't delete) input files
-f, --force Overwrites existing output files without prompting.
-t, --test Tests the integrity of compressed files without decompressing them.
-c, --stdout Outputs the result to standard output (stdout) instead of a file.
-q, --quiet Suppresses non-critical error messages.
-v, --verbose Provides verbose output. Using -v twice (-vv) gives even more detailed output.
-L, --license Displays the software version and license information.
-V, --version Displays the software version and license information (same as -L).
-s, --small Uses less memory, limiting it to at most 2500k.
-1 to -9 Sets the block size for compression, ranging from 100k (-1) to 900k (-9).
--fast Alias for -1, which sets the block size to 100k for faster compression.
--best Alias for -9, which sets the block size to 900k for the best compression.

Examples of bunzip2 Command in Linux

The following are examples that demonstrate how you can use the bunzip2 command to decompress files. Ensure you have already compressed files.

  • Basic Decompression
  • Decompress to Standard Output
  • Keep Original File
  • Force Overwrite
  • Test the Integrity of a Compressed File without Decompressing
  • Decompress a File and Output the Result to Standard Output
  • Decompress a File Quietly
  • Decompress a File with verbose Output
  • Decompress Multiple Files at Once

Basic Decompression

To decompress a file that has been compressed using the bzip2 compression algorithm, you can use the following command −

bunzip2 file1.txt.bz2

This command creates the original file (e.g., file) and removes the compressed file (file.bz2).

Basic Decompression bunzip2 Command

Decompress to Standard Output

To decompress a file and send the output to standard output (usually the terminal), instead of creating a decompressed file on disk, you can use the bunzip2 command with the “-c” option −

bunzip2 -c file2.txt.bz2
Decompress to Standard Output

Keep Original File

To keep the original compressed file after decompression, you can use the bunzip2 command with “-k” flag.

bunzip2 -k file1.txt.bz2

This command decompresses the file and does not delete the original file.bz2.

Keep Original File bunzip2 Command

Force Overwrite

To force the decompression even if the target file already exists, you can simply use the “-f” option with bunzip2 command −

bunzip2 -f file3.txt.bz2
Force Overwrite bunzip2 Command

Test the Integrity of a Compressed File without Decompressing

To test the integrity of a compressed file without actually decompressing it, you can use the bunzip2 command with the “-t” option −

bunzip2 -t file4.txt.bz2
Compressed File without Decompressing

Decompress a File and Output the Result to Standard Output

To decompress a file and output the result to standard output, you can use the following command −

bunzip2 -c file2.txt.bz2
Decompress File and Output Result

Decompress a File Quietly

To decompress a file quietly, suppressing non-critical error messages, you can simply use the following syntax −

bunzip2 -q file4.txt.bz2
Decompress File Quietly bunzip2 Command

Decompress a File with verbose Output

To decompress a file with verbose output, you can use the “-v” option with bunzip2, which will provide verbose output, giving you detailed information about the decompression process.

bunzip2 -v file6.txt.bz2
Decompress Multiple Files at Once

Decompress Multiple Files at Once

To decompress multiple files at once using bunzip2, you can simply list all the files you want to decompress. For instance −

bunzip2 file7.txt.bz2 file8.txt.bz2 file9.txt.bz2

This command will decompress file1.bz2, file2.bz2, and file3.bz2 in one go.

Decompress Multiple Files at Once

Conclusion

The bunzip2 command aids in combining the files into one, using less storage space than the original file did. It uses more memory and has a slower decompression time.

The examples above should help you get started with using bunzip2 for various tasks.

Advertisements