How to extract Tar files to a specific directory in Linux?

The well-reputed Linux distributions support the “tar (tape archive)” command line utility by default. It is used to create and display the tar archive files. Creating a tar file compresses the large files into one place as it takes less storage. Once the tar archive file is created, it can be extracted in its own and another specific directory by utilizing the “tar” command.

This guide describes various examples of extracting the tar files to a specific directory using the “tar” command.

The outline of this guide is typed below:

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

How to Extract Tar Files to a Specific Directory in Linux?

This section contains practical examples to extract the tar files into the specific directory.

The generalized syntax to extract the “tar” files into a specific directory is written below:

Syntax:

$ tar [Options] file.name.tar -C /path/to/directory

OR

tar [Options] file.tar --directory /path/to/directory

The above both syntax contains the following components:

  • tar: Represents the “tar” utility.
  • Options: Supported options of “tar” command
  • C: Shows the other particular mentioned directory.
  • –directory: Perform the same task as the “C” flag.
  • file_name.tar: Denotes the tar archive file.
  • path/to/directory: Absolute path of the target directory.

Let’s start with the first example.

Example 1: Extract a “tar” File

As the “New1” tar archive is created in the “home” directory having the following content:

$ tar -cvf New1.tar Dir1

Extract the “New1” tar archive into the “first” directory by using the “tar” command in the following way:

$ tar -xvf New1.tar -C /home/itslinuxfoss/Videos/my_aticles/

All the files and directories of the “New1.tar” is extracted successfully into “/Videos/my_articles/”. The content shown in the terminal is displayed with the help of the “-v” verbose option

Example 2: Extract a “tar.gz” File

In this example, the “-z” flag is used to extract the compressed tar archives.

Suppose the “sample.tar.gz” compressed tar archive is placed in the present working directory “Desktop” containing the following files and directories:

$ tar -tf sample.tar.gz

Use the above defined “tar” syntax to extract “sample.tar.gz” into the “/home/itslinuxfoss/Documents/” directory:

$ tar -xzvf sample.tar.gz -C /home/itslinuxfoss/Documents/

 The compressed tar achieve “sample.tar.gz” is extracted into the “Documents” directory.

To verify it change the directory via the “cd” command and use the “ls -l” command with the “Documents” directory:

$ ls -l Documents

Example 3: Extract 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. To extract the “bzip2” compressed tar archive files, the “-j” flag is used in this example.

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 “Templates” directory by using the “tar” command and its absolute path, such as:

$ tar -jvxf Extra.tar.tbz -C /home/itslinuxfoss/Templates/

The above command is executed without any error.

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

$ ls -l Templates

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

Example 4: Extract a Specific File from the “tar” File

It is not compulsory to extract the entire tar archive into a particular directory. The user can also extract a specific file or directory from the tar archive.

For this purpose, type the particular desired “file_names” that will extract from the tar archive.

As the “test.tar” archive contains the “FF.txt” files in the “/Dir1/D22” directory as shown below:

$ tar -tf test.tar

Type the “Dir1/D21/FF.txt” with the tar command to extract it into the “Downloads” directory:

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

The output confirms that the “FF.txt” file is extracted into the “Downloads” directory.

That’s all about extracting the tar files to a specific directory.

Conclusion

Linux provides the “tar” command line tool for extracting the tar files into a specific directory. This command is also beneficial for extracting the compressed “tar.gz”, “tbz”, “tbz2”, “tar.xz” and many other files. Apart from that, the user can easily extract the “particular file” into another specific directory. This guide has illustrated various examples of extracting tar files into a specific directory.