
Linux Command Line
The “bzip2” command is a powerful tool for compressing and decompressing files on a Linux system. It is particularly useful for compressing large files or groups of files, as it can significantly reduce their size.
One of the main advantages of bzip2 over other compression algorithms is its high compression ratio, which can result in smaller compressed files than other methods. However, this comes at the cost of longer compression and decompression times.
Basic Syntax
The basic syntax for using the bzip2 command is as follows:
bzip2 [options] [file(s)]
Here are some examples of how to use the bzip2 command:
- To compress a file named “largefile.txt”
bzip2 largefile.txt
This will create a new file named “largefile.txt.bz2” which will be the compressed version of the original file
- To decompress a file named “largefile.txt.bz2”
bzip2 -d largefile.txt.bz2
This will create a new file named “largefile.txt” which will be the decompressed version of the original file
- To compress multiple files with one command
bzip2 file1 file2 file3
This will create new files named “file1.bz2”, “file2.bz2” and “file3.bz2” which will be the compressed versions of the original files
- To decompress multiple files with one command
bzip2 -d file1.bz2 file2.bz2 file3.bz2
This will create new files named “file1”, “file2” and “file3” which will be the decompressed versions of the original files
It’s worth noting that bzip2 command also accepts multiple options such as -k, -f, -z, -t, -s, -1 to -9, and -c. You can check the manual or the help option for more information about the options and their usage.
Also the bunzip2 command and the bzip -d commands essentially do the same thing, they are equivalent commands.
Compressing Folders
You can compress a folder with the bzip2 command. However, you will need to use the command in combination with other Linux commands in order to compress the entire folder.
Here’s an example of how you can compress a folder named “myfolder” using the bzip2 command:
tar -cvjf myfolder.tar.bz2 myfolder/
This command will first create a tar archive of the folder using the “tar” command with the options “c” (create), “v” (verbose) and “j” (filter the archive through bzip2) and then it will compress the folder named “myfolder” into a file named “myfolder.tar.bz2”. The compressed file can be decompressed using the command
tar -xvjf myfolder.tar.bz2
Alternatively, you can use the -r option with bzip2 command to compress an entire directory and its subdirectories. But, note that it will compress the files one by one and it will not create a single archive file.
find /path/to/folder -type f -exec bzip2 {} \;
This command will find all files in the folder “/path/to/folder” and its subdirectories and then compress them one by one with bzip2.
It’s important to note that compressing a folder will not only compress the files inside the folder but also the metadata of the folder such as permissions and timestamps. This can be useful when you need to backup or transfer the entire folder, but it can also increase the size of the compressed file if the folder contains a lot of small files.
See our list of 75 Linux commands you should know about.