A “tar.bz2” file is a type of compressed file created using the Unix/Linux tar command coupled with the bzip2 compression method. This format is especially useful for combining and shrinking large files or file collections into a more manageable and transferable single file.

Advertisement

To make such an archive, one employs the tar command to gather multiple files or directories into one archive, and then applies the bzip2 algorithm to reduce the archive’s size. This produces a file with the extension .tar.bz2, which can be decompressed using the tar command and the -j option.

This tutorial offers guidance on:

  1. Decompressing a tar.bz2 file.
  2. Creating a tar.bz2 compressed file.
  3. Viewing the contents of a tar.bz2 file without extracting them.

Decompressing a tar.bz2 file

To extract a bz2 file, use the Linux tar command with the -j option. Since it’s also a tar compressed file, the -x option is necessary.

To extract a .tar.bz2 file, use the command:

tar -xjf filename.tar.bz2

This command will decompress the contents into the current directory.

For extraction to a specific location, employ the -C option along with the desired destination directory.

tar -xvjf filename.tar.bz2 -C /opt 

This will extract all files into the /opt directory.

Creating a tar.bz2 File

Use the tar command with -c for creating, and -j for compressing into a tar.bz2 archive.

For instance, to back up a web server directory:

tar -cjf backup.tar.bz2 /var/www/html 

This compresses all files in /var/www/html into a backup.tar.bz2 file in the current directory.

Listing Contents of a tar.bz2 File Without Extracting

You never need to extract a archive file to just view the file content. Use -t with -v (verbose) command line option to list archive file content only.

tar -tvjf backup.tar.bz2
Output
drwxr-xr-x root/root 0 2023-02-01 11:54 var/www/html/ drwxr-xr-x root/root 0 2023-02-01 11:54 var/www/html/static/ drwxr-xr-x root/root 0 2023-02-01 11:55 var/www/html/static/js/ -rw-r--r-- root/root 14150 2023-02-01 11:55 var/www/html/static/js/main.js drwxr-xr-x root/root 0 2023-02-01 11:55 var/www/html/static/css/ -rw-r--r-- root/root 14149 2023-02-01 11:55 var/www/html/static/css/main.css -rw-r--r-- root/root 10701 2023-01-31 11:01 var/www/html/index.html

Conclusion

An archive file are useful for creating backups, making bundle of files and save disk space. In this tutorial, you have learned how to extract a .tar.bz2 file using command line. Additionally, you learned to create .tar.bz2 file or list files without extracting it.

Share.

1 Comment

  1. Thiyagu Ganesan on

    Thanks for sharing the good details. If you still get an error, even after trying out the above steps – please do the below.

    #yum install bzip2 -y

Leave A Reply

Exit mobile version