How to Untar Files in Linux?

All the Linux distributions offer the “tar (tape archive)” command line tool to create archive files. The “archive” file comprises one or more files and metadata. They are beneficial as they are easily portable and take less storage. Once the archive file is created, it can be untared or extracted using the “tar” command.

This guide provides the possible ways to untar files in Linux using the “tar” command.

The outcomes of this guide are listed below:

Let’s start with the process to untar files in Linux.

How to Untar the “tar” Format Files in Linux?

The basic syntax to untar the tar archive in Linux is written below:

Syntax:

$ tar -xf [file_name.tar]

The above syntax holds the following components:

  • tar: Represents the “tar” utility.
  • x: Helps to extract the archive.
  • f: Tells the archive with the given filename
  • file_name.tar: Denotes the tar archive.

Example:

Let’s use this syntax to untar the “Sample.tar” tar archive file. This file is available in the “notes” directory as shown in the screenshot:

$ ls -l

Use the below “tar” command to list down the content of the “Sample.tar” archive file:

$ tar -tf Sample.tar

To untar the “Sample.tar” archive into its PWD “notes” execute the “tar” command in the following way:

$ tar -xvf Sample.tar

Run the “ls -l” command for the verification of extracted “Sample.tar” archive file:

$ ls -l

The content of the “Sample.tar” archive file is extracted or untar into the “notes” directory. The archive file also remains to save in the “notes” directory.

How to Untar the “tar.gz” Format files in Linux?

A tar.gz archive is typically smaller than a regular tar archive because the gzip compression algorithm reduces the size of the file. The generalized syntax to untar the compressed archive is typed below:

$ tar -zxvf [file_name.tar.gz]

The only difference between the syntax is the “z” flag to untar/extract the compressed archive file.

Example

The compressed tar archive has a “tar.gz” extension. In this situation, the “Test.tar.gz” compressed located in the “Extra” directory has the following contents:

$ tar -tf Test.tar.gz

Now, type the tar command in the terminal to untar the “Test.tar.gz” archive file:

$ tar -zxvf Test.tar.gz

For the verification execute the “ls-l” command as shown below:

$ ls -l

It is verified that the “Test.tar.gz” is extracted or displaying the “Extra” directory that is compressed in it.

How to Untar a “tar.bz2/.tar.zx/.tar.bz/.tbz/ or .tbz2” File?

The tar archive compressed with “bzip2” follows the “tar.bz2”, “tar.bz”, “tbz2”, and “tbz” extensions at their ends. The generalized syntax to untar the “tar.bz2”, “tar.zx”, “tbz” compressed archives are written below:

$ tar -jvxf [file_name.tar.gz]

The “-j” flag is used to extract the “bzip2” compressed tar archive files.

As the “Extra.tar.tbz” already created compressed tar archive in the “home” directory is displayed in the terminal:

$ tar -jcvf Extra.tar.tbz Directory1

Extract it into the “pwd” directory using the “tar” command:

$ tar -jvxf Extra.tar.tbz

The above command is successfully executed without any error.

Run the “ls -l” command for the verification:

$ ls -l

The output verifies that “Extra.tar.tbz” content is extracted into the “home” directory.

How to Untar the Specific File/Directory in Linux?

The “tar” file also facilitates the Linux user to untar or extract the specific file/directory from the archive files. As the “test.tar” archive file contains the following content:

$ tar -tf test.tar

Use the “tar” command to extract the “FF.txt” directory from the above-shown list:

$ tar -xvf test.tar Dir1/D21/FF.txt -C

It is confirmed that the “FF.txt” text file is extracted into the present working directory. For more verification, use the “ls -l” command:

$ tar -l Dir1/D21/FF.txt

How to Untar Files Into Particular Directory?

It is not mandatory to untar the files in the same directory in which the archive file is located. However, the user can extract/untar the archive file into a directory. For this purpose, use the “-C” flag with the absolute path of the directory:

For “tar” Format Archive Files:

$ tar -xf file_name.tar -C /path/to/directory/

For “tar.gz” Format Archive Files:

$ tar -zxf file_name.tar -C /path/to/directory/

That’s how you can untar files in Linux.

Conclusion

Linux offers the “tar” command to untar archive files easily. The option “-x” of the “tar” utility is used to perform this task. Moreover, the “tar” command is also useful to untar the compressed archive files with the extension “tar.gz” and other compression formats. This post has described all the possible ways to untar both Files in Linux.