Site icon DesignLinux

How to Extract (Unzip) Tar Bz2 File

Extract tar.bz2 file

This tutorial explains how to extract (or unzip) tar.bz2 and tbz2 archives using the tar command. The tar command is used to make and extract tar archives. Compression programs such as gzip, bzip2, lzip, lzma, lzop, xz and compress are supporting by it.

Extract tar.bz2 File

By default the tar utility comes pre-installed in most Linux distro and macOS. To extract a tar.bz2 file, use the --extract (-x) option and specify the archive file name after the -f option:

tar -xf archive.tar.bz2

The tar command consider the compression type automatically and extracts the archive. The same command can be used to extract tar archives compressed with other algorithms such as .tar.gz or or .tar.xz .

To extract the file using File manager, simply right click on the file you want to extract and select “Extract”. If you are using Windows OS you will need a tool named 7zip to extract tar.bz2 files.

To show the verbose output use the -v option. This option tells tar to display the names of the files being extracted on the terminal.

tar -xvf archive.tar.bz2

The tar will extract the archive contents or files in the current working directory. If you would like to extract the archive files in a specific directory you should use --directory (-C):

For instance, to extract the archive contents to the /home/tecnstuff/files directory, you would type:

tar -xf test.tar.bz2 -C /home/tecnstuff/files

Extracting Specific Files from a tar.bz2 File

To extract a specific file from a tar.bz2 file, you should give the file name which you want to extract. You also can extract multiple files by space-separated list of file names:

tar -xf test.tar.bz2 filename1 filename2

Make sure you should give their exact names with the path, you can print using the --list (-t) option.

It’s a same procedure as files to extract one or more directories from an archive:

tar -xf test.tar.bz2 dir_name1 dir_name2

It will show following error message if the given file name is not exists in the archive:

tar -xf test.tar.bz2 tns.txt
tar: tns.txt: Not found in archive
tar: Exiting with failure status due to previous errors

You also can extract the files using --wildcards option based on a wildcard pattern. The pattern must be quoted to prevent the shell from interpreting it.

For example, to extract only .php files, you would use :

tar -xf test.tar.bz2 --wildcards '*.php'

Listing tar.bz2 File

To list the content of a tar.bz2 file, use the --list (-t) option:

tar -tf test.tar.bz2

The output will look something like this:

filename1
filename2
filename3

If you add the --verbose (-v) option, tar will print more information, such as owner, file size, timestamp, etc.:

tar -tvf test.tar.bz2
-rw-r--r-- tecnstuff/users       0 2020-02-02 01:59 filename1
-rw-r--r-- tecnstuff/users       0 2020-02-02 01:59 filename2
-rw-r--r-- tecnstuff/users       0 2020-02-02 01:59 filename3

Conclusion

tar.bz2 file is a Tar archive compressed with Bzip2. To extract a tar.bz2 file, use the tar -xf command followed by the archive name.

If you have any questions or feedback, feel free to leave a comment below.

Exit mobile version