In this tutorial, we will be showing you how to use
tar command to extract tar.bz2 files.
Tar stands for tape archive, and it is one of the most used commands that deals with compressed archive files. Bz2 stands for
bzip2. It is a specific compression algorithm.
tar command comes pre-installed in most Linux distributions. The
tar utility is used to compress and extract files using different algorithms.
Tar supports a wide array of compression algorithms such as gzip, bzip2, xz, lzip, etc.
Table of Contents
Extract a tar.bz2 file quickly
In this section, we’ll show you a simple method to extract any tar.bz2 file. In the later sections, you will get to know the tar command in a little bit more detail.
To extract all the files inside of a tar.bz2 file, use the
-xf flag with the tar command:
tar -xf file.tar.bz2
x stands for extract and
f stands for the archive file. The
tar command detects the compression type automatically and extracts it. You don’t need to specify the file/compression type to extract. For example, you could extract a tar.gz file with the same command.
Alternatively, you can also use the graphical user interface (GUI) instead of the command line. Just right click on the tar bz2 archive file you want to extract and click on the Extract option.
Basic usage of tar command
The basic syntax of the tar command is as follows:
tar [options] [file]
The tar command has a plethora of options in the help menu. You can access it by typing in
tar --help. We’ll be using the main operation mode most of the time. This mode has some basic options for creating and extracting archives. Below are three of these options:
-c, – create create a new archive -t, – list list the contents of an archive -x, – extract, – get extract files from an archive
You’ve already seen the usage of the
-x flag for extracting an archive. Let’s take a look at some other options now.
Listing the contents of a tar bz2 archive
If you just wanted to take a look at the contents of an archive, you would use the
tar -tf compressed_file.tar.bz2
file file.log file.txt
We can get more details about the archive using the
--verbose flag with the command. The output will include file/folder details such as owner, permissions, etc. Let’s see it in action:
tar -tf compressed_file.tar.bz2
-rw-r--r – root/root 3153920 2021-10-15 21:55 file -rw-r--r – edxd/edxd 1048576 2021-10-15 21:54 file.txt -rw-r--r – root/root 2097152 2021-10-15 21:54 file.log
As you can see, the file permissions and owner along with the file size is shown in the output.
Extract specific files/folders from a tar bz2 archive
Imagine you need a specific file from a large archive. In this case, you might want to only extract that specific file from the archive. This can be done by simply specifying the filename (and file path) followed by the extract command. Let’s see how to extract only the required files:
Check what files are in the working directory with the
compressed_file.tar.bz2 file.log.save file.tar.bz2 tutorial.firstpage.php
I have already shown you the content of the compressed_file.tar.bz2 in the previous section (listing files). Now let’s extract file and file.log from this archive. We’ll be using the following command:
tar -xf compressed_file.tar.bz2 file file.log
Here, we used the
-xf flag with the command to extract the compressed_file.tar.bz2 archive. We also mentioned file and file.log indicating which files to extract. Now let’s check our working directory again with the
compressed_file.tar.bz2 file file.log file.log.save file.tar.bz2 tutorial.firstpage.php
As you can see, the file and file.log have been extracted from the compressed_file.tar.bz2 archive. You can also extract specific directories/folders by using this method. The command would look like the following:
tar -xf archive.tar.bz2 dir1 dir2 dir3
Here, dir1, dir2, and dir3 are the names of the directories/folders you want to extract from the archive.
Use wildcards for specific file extensions
Let’s say you want to extract all the files that have the same extension from inside of an archive. In this case, you could use a wildcard like
*.extension format. Here, you can use your specific extension. For example, if you wanted to extract all the text files, you would use
*.txt. Let’s see this in action:
First, let’s take a look at our current directory:
compressed_file.tar.bz2 file.tar.bz2 test.tar.bz2
Let’s take a look at the files inside the
tar -tf test.tar.bz2
file.log tutorial.firstpage.php num.txt pr_ex_creator.txt
Now, we have two text files that end in
.txt extension. Let’s extract these two files with the
*.txt wildcard. We also need to use the
--wildcards flag to enable the wildcard option:
tar -xf test.tar.bz2 – wildcards *.txt
Now let’s check our current directory to see if it worked –
compressed_file.tar.bz2 file.tar.bz2 num.txt pr_ex_creator.txt test.tar.bz2
From the output, we can see that those two text files were extracted. This is how you can use wildcards to extract all the files with a specific extension.
Change the location of extraction
If you want to change the location where the extracted files will occupy, you can use the
-C flag and specify the location of your choice. Here’s an example of how to do it:
tar -xf compressed_file.tar.bz2 -C /home/new/
Here, after the
-C flag, I’ve specified the path for saving the extracted files. The path /home/new/ is marked in color. Let’s take a look at the contents of the directory:
file file.log file.txt
As you can see, the compressed_file.tar.bz2 file extracted in the /home/new/ location.
In this tutorial, we showed you how to extract a
In brief, you can extract the archive using the
-xf flag with the
tar command. If you’re still unsure how to use the
tar command, you can use the graphical user interface when possible – you can also see check out some examples from the help menu.
Thank you for reading. If you have any questions, feel free to leave them in the comments section below.