2024-02-17
The core command for compressing files using bzip2 is straightforward:
bzip2 filename.txtThis 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.txtAfter execution, you’ll find mydocument.txt.bz2 in the same directory.
bzip2 can efficiently handle multiple files simultaneously. Use wildcards for convenience:
bzip2 *.txtThis 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.
For greater control, you can explicitly specify the output filename:
bzip2 -c mydocument.txt > compressed_document.bz2The -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 is equally simple using the bunzip2 command:
bunzip2 filename.txt.bz2This decompresses filename.txt.bz2 and restores the original filename.txt.
For example, to decompress mydocument.txt.bz2:
bunzip2 mydocument.txt.bz2Similar to compression, bunzip2 can handle multiple files using wildcards:
bunzip2 *.bz2This decompresses all files ending in .bz2 in the current directory.
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.txtThis compresses mylargefile.txt with the highest compression level (level 9) while keeping the original file.
While not a direct bzip2 function, you can use file command to identify the file type:
file mydocument.txt.bz2This will output information about the file, including that it’s a bzip2 compressed file. This helps verify the compression process.
For more detailed output during compression or decompression, use the -v option:
bzip2 -v mydocument.txtThis will show the compression ratio and other statistics.
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.