Linux Commands

How to Unpack Tar Files in Linux

When using Linux, you are guaranteed to see tar files, especially when downloading the open-source packages. Besides, creating tar compressed files is easy using the programs like gzip. Tar is meant for compressing magnetic tapes, and it stands for “tape archive”.

Tar files support various compression such as gzip, which have the .tar.gz. If you are looking on how to unpack the tar files, this guide covers how to create tar files and the two ways of unpacking tar files in Linux.

Working with Tar Command in Linux

For most Linux distributions, the tar command comes preinstalled. You can confirm by checking its version, as in the following image:

There are various options that you can use with tar. They include:

1. -x: Added to extract an archive.

2. -c: Creates an archive.

3. -v: Show details of the operation.

4. -f: Specifies an archive file name.

5. -t: Lists details of the archive.

Those are the main options. We will see how to use them using the following examples:

For this example, we will create an archive file using tar and then cover the two ways of unpacking it.

To create an archive file, the syntax would be:

$ tar -cf [archive-file] file1 file2 file_n

We created a “.tar” file. Next, let’s create a “.tar.gz” using the gzip compression. The syntax would be:

$ tar -czvf [archive-name] file1 file2 file_n

With our two archive files, we can unpack them using tar.

How to Unpack Tar Files

To extract the tar files, use the Command Line or the GUI.

1. Extract Tar Files Using GUI

The first thing is to use your file manager and navigate to where the tar archive is located. In our case, it is /Desktop.

Next, right-click the tar file that you wish to extract. You can either choose Extract Here to extract the contents of the tar file in the current directory or use the Extract To and specify where you want to extract the tar files.

Alternatively, you can double-click the tar archive which lists the contents inside it. From there, you can extract the tar by clicking the extract button at the top-left corner.

2. Extract Tar Files Using Command Line

Unlike GUI, the command line gives you more flexibility regarding what you can do when unpacking tar files.

Option 1. To extract the tar file in the same directory, use the -xf flags followed by the tar file.

The command would be:

$ tar -xf linuxhint.tar

Replace the tar file with the one you are using for your case.

Option 2. To display the extraction progress, add the -v flag. The new command would be:

$ tar -xvf linuxhint.tar

Note that the extract options are the same despite the extension of the tar archive file.

Option 3. To list the contents of a tar file before extracting it, use the -t flag. For instance, to view the contents of our tar.gz file, the command would be:

$ tar -tf linux.tar.gz

You must add the -f to specify which tar file to extract.

Option 4. You can extract the contents of a tar file to a different location instead of the default, which is the current directory. For this, use the -C flag followed by the path.

$ tar -xf tar-file -C /path/directory

Option 5. If you don’t need to extract all the contents of the tar file, you can specify which specific files to extract by specifying their names separated by spaces. For instance, to extract two files, file1 and file2, the command would be:

$ tar -xfv linux.tar.gz file1 file2

In case of many files with the same extension and you want to extract them all, you can use the —wildcards followed by the extension. For instance, to extract the .txt files, the syntax would be.

$ tar -xf archive-file --wildcards ‘*.txt’

Similarly, as in the following image, you can extract specific folders from the tar file.

We successfully unpacked the tar files using various options.

Conclusion

Working with tar files shouldn’t trouble you anymore. We covered the various options you have when you need to unpack the tar files. With this guide, you now understand how to handle the tar files easily.

Similar Posts