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.
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
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
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
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
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
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
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.