How to Extract Bzip2 Files Using Terminal

Suraj Joshi Feb 02, 2024
  1. Extract a .tar.bz2 File
  2. List the Contents of a tar.bz2 File
  3. Extract Specific Files and Directories From the Compressed File
How to Extract Bzip2 Files Using Terminal

tar is a command-line utility that allows us to create and extract tar archives, and it supports the vast majority of compression programs like gzip, lzip, bzip2, lzma, lzop, xz and compress. The files compressed using bzip2 end with either .tar.bz2 or .tbz2. We can use the tar command to compress and extract files in .tar.bz2 and .tbz2 format.

Extract a .tar.bz2 File

We use the --extract or -x option followed by -f option and then specify the extracted file.

tar -xf compressed.tar.bz2

It extracts the compressed file compressed.tar.bz2 by detecting the type of compression used. We can use the same command to extract files compressed with other algorithms also.

For most of the Linux users, tar utility is installed by default at the installation time. For Windows users, we have a tool named 7-zip to extract bz2 files.

If we want some information about the extraction, we can use the -v option.

tar -xvf compressed.tar.bz2

It will print the names of all the extracted files in the terminal.

By default, the contents compressed file will be extracted in the current working directory. To extract the files in a particular directory, we can use the --directory or -C option and specify the path where the file needs to be extracted.

tar -xf compressed.tar.bz2 -C /home/Extracted_Files

It will extract the compressed file compressed.tar.bz2 in the current working directory to the Extracted_files directory inside home.

List the Contents of a tar.bz2 File

To list the contents of a tar.bz2 file, we use --list or -t option with the tar command.

tar -tf compressed.tar.bz2

Output:

Blues/hey.mp3
1.mp3
2.mp3

It shows all the contents of compressed.tar.bz2 file.

To get more details about the content like the owner, file size, time-stamp, we use the --verbose or -v option with the tar command.

tar -tvf compressed.tar.bz2

Extract Specific Files and Directories From the Compressed File

To only extract specific files and directories from the compressed file, we list the names of files and directories to be extracted after compressed files followed by a space.

tar -xf compressed.tar.bz2 1.mp3 2.mp3

It will only extract the files 1.mp3 and 2.mp3 from the compressed file.

While specifying the files, we must use the exact path for the file to be extracted, as shown by the tar command, with --list or -t option.

tar -xf compressed.tar.bz2 jazz Rock

It will only extract the directories jazz and Rock from the compressed file.

If we specify the file that doesn’t exist, we get an output saying the file is not found.

tar -xf compressed.tar.bz2 rolling.jpeg

Here if the file rolling.jpeg is not present, we get the following error:

tar: rolling.jpeg: Not found in archive
tar: Exiting with failure status due to previous errors
Author: Suraj Joshi
Suraj Joshi avatar Suraj Joshi avatar

Suraj Joshi is a backend software engineer at Matrice.ai.

LinkedIn

Related Article - Linux Files and Directories