bzip2

2024-02-17

Compressing Files with bzip2

The core command for compressing files using bzip2 is straightforward:

bzip2 filename.txt

This command compresses filename.txt and creates a new file named filename.txt.bz2. The original file remains intact.

Let’s illustrate with a practical example. Suppose we have a text file named mydocument.txt. To compress it:

bzip2 mydocument.txt

After execution, you’ll find mydocument.txt.bz2 in the same directory.

Compressing Multiple Files

bzip2 can efficiently handle multiple files simultaneously. Use wildcards for convenience:

bzip2 *.txt

This compresses all files ending with .txt in the current directory. Each file will be compressed individually, resulting in files like file1.txt.bz2, file2.txt.bz2, and so on.

Specifying the Output Filename

For greater control, you can explicitly specify the output filename:

bzip2 -c mydocument.txt > compressed_document.bz2

The -c option sends the compressed output to standard output, which is then redirected using > to create a file named compressed_document.bz2. This allows you to choose a different name for the compressed archive.

Decompressing Files with bzip2

Decompressing files is equally simple using the bunzip2 command:

bunzip2 filename.txt.bz2

This decompresses filename.txt.bz2 and restores the original filename.txt.

For example, to decompress mydocument.txt.bz2:

bunzip2 mydocument.txt.bz2

Decompressing Multiple Files

Similar to compression, bunzip2 can handle multiple files using wildcards:

bunzip2 *.bz2

This decompresses all files ending in .bz2 in the current directory.

Level of Compression

bzip2 allows you to control the compression level using the -k option (keep original files) and -1 through -9 for compression levels. Level 9 provides the highest compression but takes the longest time.

bzip2 -k -9 mylargefile.txt

This compresses mylargefile.txt with the highest compression level (level 9) while keeping the original file.

Listing Compressed Files

While not a direct bzip2 function, you can use file command to identify the file type:

file mydocument.txt.bz2

This will output information about the file, including that it’s a bzip2 compressed file. This helps verify the compression process.

Verbosity

For more detailed output during compression or decompression, use the -v option:

bzip2 -v mydocument.txt

This will show the compression ratio and other statistics.

Error Handling

bzip2 returns error codes which can be checked using the $? variable after running the command. A return code of 0 indicates success. Script writers can use this for error handling.

These examples provide a solid foundation for using bzip2 effectively for file compression and decompression in your Linux workflows. Remember to consult the bzip2 man page (man bzip2) for a detailed list of options and further details.