Linux Commands

How to extract Tar files to a specific directory in Linux

While using Linux, you might have noticed that many packages come with a “.tar” file extension. So, what is a tar file? A tar file is a collection of many files into one file. Tar (Tape Archive) is a program that collects the files and grabs some helpful information about files it archives, such as permissions, dates, etc. It is noteworthy that the Tar utility does not compress the files; for compression, you need “gzip” or “bzip” utilities.
As a Linux administrator, you often have to deal with tar files. In many situations, you have to extract the data of a tar file to some specific directory. So, how to extract the tar file to a particular path? One way is to extract the tar file in the current directory and then copy it to the desired directory. It will do the job, but the procedure is time-consuming. This guide is focusing on finding solutions to extract various types of tar files to a specific directory.

How to extract Tar file to a specific directory in Linux:

To extract the Tar file to another directory, follow the below-mentioned syntax:

$tar -xf [file_name].tar -C [/path_of/directory]

Alternatively:

$tar --extract --file=[file_name].tar --directory [/path_of/directory]

The “-x” flag tells the Tar utility to extract the file mentioned in the argument after “-f.” Whereas, “-C” flag is used to set a specific directory to extract the file. Alternatively, you can explicitly mention the total words to extract files using tar. In my opinion, using flags is a much quicker way to extract files than typing the entire word. Let’s do an example; I have a file by the name of “my_documents.tar,” which I want to extract to a directory “files/tar_files,” and to do that command would be:

$tar -xf my_documents.tar -C files/tar_files

Or:

$tar --extract --file=my_documents.tar --directory files/tar_files

If you want to monitor the progress of extraction of the file in the terminal, then use the “-v” (verbose) flag:

$tar -xvf my_documents.tar -C files/tar_files

How to extract “tar.gz/tgz” files to a specific directory in Linux:

As discussed above that tar files can be compressed by using the “gzip” utility. To extract such files to a specific directory, the procedure is pretty much similar; an additional flag “-z” will be added in the command to deal with “tar.gz” or “tgz” files:

$tar -zxf my_documents.tar.gz -C files/tar_gz_files

Or:

$tar -zvxf my_documents.tar.gz -C files/tar_gz_files

How to extract “tar.bz2/tar.bz/tbz/tbz2” files to a specific directory in Linux:

Before we learn the extracting method, lets understand what “tar.bz2,tar.bz,tbz,tbz2” files are. These are the file extensions of the tar files compressed by either the “bzip” or “bzip2” utility in Linux. To extract files with any of these extensions, we will add the “-j” flag:

$tar -jxf my_documents.tar.bz2 -C files/bzip_files

For verbose output use:

$tar -jvxf my_documents.tar.bz2 -C files/bzip_files

Conclusion:

Tar is a widely used utility in Linux and UNIX-based operating systems to make backup archives. Tar utility also comes with a feature to extract tar files to a specific directory. Files can be extracted using the “-C” flag with the specified folder path. Moreover, using the Tar utility, you can also extract the specific files from the archived files. This all-in-one utility has a lot to explore and to learn more about Tar utility execute “man tar” in the terminal.

About the author

Sam U

I am a professional graphics designer with over 6 years of experience. Currently doing research in virtual reality, augmented reality and mixed reality.
I hardly watch movies but love to read tech related books and articles.