How to Unzip tar.bz2 Files

July 13, 2023

Introduction

The tar command creates and extracts tar archive files. The file type has an extension .tar and supports various file compression algorithms to reduce file size. The bz2 command uses the BZ2 algorithm to compress tar files.

The combination of two commands creates tar.bz2 files. Combining the two allows grouping multiple files and directories into a single tar file and compressing them to reduce the file size.

This guide shows how to unzip (extract), view, and create tar.bz2 files.

How to Unzip tar.bz2 Files

Prerequisites

  • Access to the command line/terminal.
  • Read and write permissions to the destination.
  • Enough available disk space (see our guide to check the disk space in Linux).

List tar.bz2 Content Without Extracting

Before extracting the file, the tar command enables listing the archive's contents. To list the contents, use the following command:

tar -tf file.tar.bz2
List tar.bz2 contents terminal output

The -t option prints the list, while the -f flag allows specifying a file name. For verbose output, add the -v option:

tar -tvf file.tar.bz2
List tar.bz2 contents verbose terminal output

The file's permissions, ownership, size, and creation date print to the console.

Extract tar.bz2 Files

To extract a tar.bz2 file, use the tar -xf command and provide the file name. For example:

tar -xf file.tar.bz2
Extract tar.bz2 file terminal output

The tar command automatically detects the compression algorithm and decompresses the archive.

Extract Specific Files from tar.bz2

To extract specific files from a tar.bz2 archive, list the file or directory names and the path after the archive name. For example, the following command extracts one file and one directory:

tar -xf file.tar.bz2 dir1/file1 dir2
Extract tar.bz2 specific files and directories

Provide the exact file name and path when listing different elements. The paths and file names are visible when listing tar.bz2 contents without extraction.

To extract multiple files or directories with similar name formats, add the --wildcards option. For example, to extract all .txt files, use:

tar -xf file.tar.bz2 --wildcard "*.txt"

Quotes prevent the shell from interpreting the wildcard.

Note: For more information on using quotes, see the detailed explanation about the differences between Bash single vs. double quotes.

Extract tar.bz2 Files from stdin

To extract tar.bz2 files from stdin, such as through piping, add the -j option to the extraction command. For example, when downloading a tar.bz2 file from an online resource with the wget command, pipe the tar command to extract the downloaded file:

wget [some link] | tar -xj
wget pipe tar stdout terminal output

The option starts decompression for the .bz2 format immediately after download.

How to Create tar.bz2 File

Use the tar command with the -c and -j options to create a tar.bz2 file. Provide the tar.bz2 file name first, followed by the files or directories to compress:

tar cjf files.tar.bz2 test
tar cjf terminal output tar.bz2 file

The tar command creates the tar.bz2 file in the same directory.

Conclusion

After reading this guide, you know how to view, extract, and create tar.bz2 files. The combined formats allow compressing multiple files and directories into a single file. The tar command can also unzip tar.gz and other compression formats.

When working only with .bz2 files, there are specialized commands to decompress BZ2 files. Follow our guide to learn how to unzip bz2 files.

Was this article helpful?
YesNo
Milica Dancuk
Milica Dancuk is a technical writer at phoenixNAP with a passion for programming. With a background in Electrical Engineering and Computing, coupled with her teaching experience, she excels at simplifying complex technical concepts in her writing.
Next you should read
How to Unzip BZ2 File
July 11, 2023

BZ2 files have higher compression rates and relatively low decompression speeds. This simple step-by-step guide will show you how to unzip BZ2 files on Linux and Windows.
Read more
How to Unzip a ZIP File in Ubuntu / Linux
March 25, 2024

Zip files are ubiquitously used on all operating systems, including Linux. This simple guide explains how to unzip files from the command line, as well as how to unzip multiple files.
Read more
How to Extract or Unzip tar.gz Files from Linux Command Line
February 12, 2024

This article shows which commands to use when compressing and decompressing files from the command line.
Read more
How to Zip a File in Linux
October 31, 2022

Did you know you can work with ZIP files on Linux? The handy zip command lets you create ZIP archive files quickly. Learn how to use the command in this guide.
Read more