tar command allows you to create and extract tar archives. It supports a vast range of compression programs such as gzip, bzip2, lzip, lzma, lzop, xz and compress.
Bzip2 is one of the most popular algorithms for compressing tar files. By convention, the name of a tar archive compressed with bzip2 ends with either .tar.bz2 or .tbz2.
In this tutorial, we will explain how to extract (or unzip) tar.bz2 and tbz2 archives using the
Extracting tar.bz2 File
Most Linux distributions and macOS comes with the tar utility pre-installed by default.
To extract a tar.bz2 file, use the
-x) option and specify the archive file name after the
tar -xf archive.tar.bz2
If you are a Desktop user and the command-line is not your thing you can use your File manager. To extract (unzip) a tar.bz2 file simply right-click the file you want to extract and select “Extract”. Windows users will need a tool named 7zip to extract tar.bz2 files.
For more 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
tar will extract the archive contents in the current working directory. Use the
-C) to extract archive files in a specific directory:
For example, to extract the archive contents to the
/home/black/files directory, you would type:
tar -xf archive.tar.bz2 -C /home/black/files
Extracting Specific Files from a tar.bz2 File
To extract a specific file(s) from a tar.bz2 file, append a space-separated list of file names to be extracted after the archive name:
tar -xf archive.tar.bz2 file1 file2
When extracting files, you must provide their exact names including the path, as printed when the
-t) option is used.
Extracting one or more directories from an archive is the same as extracting multiple files:
tar -xf archive.tar.bz2 dir1 dir2
If you try to extract a file that doesn’t exist in the archive, an error message similar to the following will be shown:
tar -xf archive.tar.bz2 README
tar: README: Not found in archive tar: Exiting with failure status due to previous errors
--wildcards option allows you to extract files from a tar.bz2 file based on a wildcard pattern. The pattern must be quoted to prevent the shell from interpreting it.
For example, to extract only the files whose names end in
.md (Markdown files), you would use:
tar -xf archive.tar.bz2 --wildcards '*.md'
Extracting tar.bz2 File from stdin
When extracting a compressed tar.bz2 file by reading the archive from standard input (usually through piping), you must specify the decompression option. The
-j option tells
tar that the file is compressed with bzip2.
In the example below we are downloading the Vim sources using the
wget command and pipe its output to the
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 show you which option you should use:
tar: Archive is compressed. Use -j option tar: Error is not recoverable: exiting now
Listing tar.bz2 File
To list the content of a tar.bz2 file, use the
tar -tf archive.tar.bz2
The output will look something like this:
file1 file2 file3
If you add the
tar will print more information, such as owner, file size, timestamp ..etc:
tar -tvf archive.tar.bz2
-rw-r--r-- black/users 0 2019-02-15 01:19 file1 -rw-r--r-- black/users 0 2019-02-15 01:19 file2 -rw-r--r-- black/users 0 2019-02-15 01:19 file3
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, please leave a comment below.