The “bz2” File is the compressed form of the specific file. It reduces the file size to take up less space in the directory. The “bzip2” is the most popular compressed algorithm supported by the “tar” command. The “bzip2” compressed file follows the “tar.bz2” or the “tbz2” extension.
This guide will illustrate the possible ways to open a bz2 file using the command line interface. The outcomes of this guide are as follows:
Let’s start with the first method.
Method 1: Using the “bzip2” Command
The “bzip2” command line tool in Linux is utilized for compressing and decompressing a file. A file decompression is helpful because it takes less storage in the system than the actual file. It combines a large no of files and directories into a single compressed file having the extension “bz2”:
The generalized syntax to extract or open the compressed “bz2” file is written below:
$ bzip2 -d [file.txt.bz2]
The syntax is divided into three essential components such as:
- bzip2: Represents the “bzip2” command utility
- -d: Helps to decompress the “bz2” text file
- File.txt.bz2: Denotes the targeted “bz2” file.
Suppose the “bz2” compressed text file is available in the “New” directory:
$ ls -l New
Execute the “bzip2” command with the “-d” option to open the “Test.txt.bz2” file into the “New” directory:
$ bzip2 -d Test.txt.bz2
The output of “ls -lh” verifies that the “Test.txt.bz2” has been extracted into the “New” directory.
Method 2: Using the “tar” Command
The “tar (tape archive)” command line tool helps create or extract archive files. The simple tar archive files follow the “.tar” extension. However, the tar archive compressed with “bzip2” follows the “tar.bz2”, “tar.bz”, “tbz2”, and “tbz” extensions.
The generalized syntax to open the compressed “bz2” archive is typed here:
$ tar -xf [file_name.tar.bz2]
The syntax contains the following components:
- tar: Identifies the “tar” utility.
- x: Extracts the tar archive.
- f: Tells the archive with the given filename
- file_name.tar.bz2: Denotes the compressed tar.bz2 archive.
Example 1: Open a bz2 File in its Default directory
The “home” directory shows the “New.tar.bz2” compressed tar archive containing the following contents:
$ tar -tf New.tar.bz2
Run the tar command in the following way to open the “New.tar.bz2” compressed archive file into the “home” directory:
$ tar -xf New.tar.bz2
The above command has been executed successfully.
Now for the verification, execute the “ls-l” command as shown below:
$ ls -l
It is verified that the “New.tar.bz2” content has been extracted into the “home” directory.
Example 2: Open a bz2 File in the Specific Directory
The user can also extract the “bz2” compressed tar archive into another specific directory using the “tar” command.
Suppose to extract the “New.tar.bz2” compressed archive file into the “Templates” directory by using its absolute path with the “tar” command:
$ tar -xf New.tar.bz2 -C /home/itslinuxfoss/Templates/
In the above command, the “-C” flag denotes the absolute path of the target directory.
The “ls -lh” command verifies that the “New.tar.bz2” compressed archive file has been extracted into the “Templates” directory.
Example 3: Open a Particular File from the “tar.bz2” File
The “tar” command allows the users to open or extract the particular file from the compressed “tar.bz2” file.
As the “New.tar.bz2” archive contains the “File2.txt” file in the “/Directory1/Folder2” directory as shown below:
$ tar -tf New.tar.bz2
Type the “Directory1/Folder2/File2.txt” with the tar command to extract it into the “Downloads” directory:
$ tar -xvf New.tar.bz2 Directory1/Folder2/File2.txt -C /home/itslinuxfoss/Downloads/
The “v” is the “verbose” option used to display the result in the terminal.
The output displays that the “File2.txt” file has been extracted into the “Downloads” directory.
That’s all about the possible aspects of opening a “bz2” file.
Linux offers the “bzip2” command to open the “bz2” compressed file. This task can also be performed using the “tar” command line utility. These command line tools are available in the most commonly used Linux distributions. The guide has illustrated all the possible aspects of opening the “bz2” file in Linux.