Oct 21, 2023 4 min read

How to Extract (Unzip) Tar Bz2 File

Extract (Unzip) Tar Bz2 File with our step-by-step tutorial. One of the most often used techniques for compressing tar files is Bzip2.

Extract (Unzip) Tar Bz2 File
Table of Contents

Introduction

You can use the tar command to generate and extract tar archives. Gzip, bzip2, lzip, lzma, lzop, xz, and compress are just a few of the compression applications it supports.

One of the most often used techniques for compressing tar files is Bzip2. The name of a tar archive compressed with bzip2 should end in either .tar.bz2 or .tbz2.

In this tutorial, you will use the tar command to extract (or unzip) tar.bz2 and tbz2 archives. We will also address a few FAQs on how to extract (Unzip) Tar Bz2 File.

Extract tar.bz2 File

The tar program comes pre-installed on most Linux variants and macOS.

Use the --extract (-x) option and supply the archive file name after the -f option to extract a tar.bz2 file:

tar -xf archive.tar.bz2

The tar program automatically determines the type of compression and extracts the archive. The same command may be used to extract tar archives compressed using different techniques, such as .tar.gz or .tar.xz.

If you're a desktop user who prefers not to utilize the command line, you can use your file manager instead. To extract (unzip) a tar.bz2 file, simply right-click it and choose "Extract." To extract tar.bz2 files, Windows users will need to use the 7zip program.

Use the -v option to get more detailed output. This option instructs tar to print the names of the files being extracted to the terminal.

tar -xvf archive.tar.bz2

Tar extracts the contents of an archive in the current working directory by default. To extract archives in a specific directory, use the --directory (-C) option:

To extract the contents of an archive to the /home/vega/files directory, for instance, type:

tar -xf archive.tar.bz2 -C /home/vega/files

Extract Specific Files from tar.bz2 File

Append a space-separated list of file names to be extracted after the archive name to extract a specific file(s) from a tar.bz2 file:

tar -xf archive.tar.bz2 file1 file2

When extracting files, you must use their exact names, including the path, as displayed by the --list (-t) option.

Extracting numerous files from an archive is the same as extracting one or more directories:

tar -xf archive.tar.bz2 dir1 dir2

If you try to extract a file that doesn't exist in the archive, you'll get an error notice that looks like this:

tar -xf archive.tar.bz2 README
Output

tar: README: Not found in archive
tar: Exiting with failure status due to previous errors

You can use the --wildcards option to extract files from a tar.bz2 file using a wildcard pattern. To prevent the shell from reading the pattern, it must be quoted.

To extract only the files with names ending in .md (Markdown files), for example, use:

tar -xf archive.tar.bz2 --wildcards '*.md'

Extract tar.bz2 File from stdin

You must specify the decompression option when extracting a compressed tar.bz2 file from standard input (often via piping). The -j option instructs tar to compress the file using the bzip2 algorithm.

We'll use the wget command to get the Vim sources and pipe the output to the tar command in the example below:

wget -c ftp://ftp.vim.org/pub/vim/unix/vim-8.1.tar.bz2 -O - | sudo tar -xj

If you don't specify a decompression option, tar will recommend the options to you:

Output

tar: Archive is compressed. Use -j option
tar: Error is not recoverable: exiting now

List tar.bz2 File

Use the --list (-t) option to list the contents of a tar.bz2 file:

tar -tf archive.tar.bz2

You will get an output like below:

Output

file1
file2
file3

Tar will print more information if you use the --verbose (-v) option, such as the owner, file size, timestamp, and so on:

tar -tvf archive.tar.bz2
Output

-rw-r--r-- linuxize/users       0 2019-02-15 01:19 file1
-rw-r--r-- linuxize/users       0 2019-02-15 01:19 file2
-rw-r--r-- linuxize/users       0 2019-02-15 01:19 file3

FAQs to Extract (Unzip) Tar Bz2 File

What is a tar.bz2 file?

A tar.bz2 file is a compressed archive file that combines multiple files into a single compressed file using the tar and bzip2 compression algorithms.

Can I extract a tar.bz2 file without installing additional software?

No, you need to have a utility like tar installed on your system because it is required to extract the contents of a tar.bz2 file.

What is the command for extracting a tar.bz2 file?

The command for extracting a tar.bz2 file is tar -xjf filename.tar.bz2.

Can I specify a destination directory while extracting the tar.bz2 file?

Yes, you can specify a destination directory by using the -C flag followed by the desired directory path.

Can I extract only specific files from a tar.bz2 file?

Yes, you can extract specific files by providing their names or patterns as arguments to the tar command.

How do I view the contents of a tar.bz2 file without extracting it?

To view the contents of a tar.bz2 file without extracting, you can use the tar -tf filename.tar.bz2 command.

Will extracting a tar.bz2 file overwrite existing files?

Yes, if a file with the same name already exists in the extraction directory, it will be overwritten during the extraction process.

Conclusion

The tar.bz2 file is a Bzip2 compressed Tar archive. Use the tar -xf command followed by the archive name to extract a tar.bz2 file.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.