How to Extract Tar Files to Specific or Different Directory in Linux

The tar command is one of the utilities that you can use to create a backup on a Linux system. It includes many options that one can use to specify the task to achieve.

One thing to understand is that you can extract tar files to a different or specific directory, not necessarily the current working directory.

You can read more about tar backup utility with many different examples in the following articles, before proceeding further with this article.

In this guide, we shall take a look at how to extract tar files to a specific or different directory, where you want the files to reside.

The general syntax of tar utility for extracting files:

tar -xf file_name.tar -C /target/directory
tar -xf file_name.tar.gz --directory /target/directory

Note: In the above syntax, the -C option is used to specify a different directory other than the current working directory.

Let us now look at some examples below.

How to Extract Tar Files to a Specific Directory

In the first example, I will extract the files in articles.tar to a directory /tmp/my_article. Always make sure that the directory into which you want to extract the tar file exists.

Let me start by creating the /tmp/my_article directory using the mkdir command below:

mkdir /tmp/my_article

You can include the -p option to the above command so that the command does not complain.

To extract the files in articles.tar to /tmp/my_article, I will run the command below:

tar -xvf articles.tar -C /tmp/my_article/
Extract Tar Files to Different Directory
Img 01: Extract Tar Files to Different Directory

In the above example, I used the -v option to monitor the progress of the tar extraction.

Let me also use the --directory option instead of -c for the example above. It works just in the same way.

tar -xvf articles.tar --directory /tmp/my_articles/
Extract Tar Files to Specific Directory
Img 02: Extract Tar Files to Specific Directory

How to Extract .tar.gz or .tgz Files to Different Directory

First make sure that you create the specific directory that you want to extract into by using:

mkdir -p /tmp/tgz

Now we will extract the contents of documents.tgz file to separate /tmp/tgz/ directory.

tar -zvxf documents.tgz -C /tmp/tgz/ 
Extract tar.gz or .tgz Files to Different Directory
Img 03: Extract tar.gz or .tgz Files to Different Directory

How to Extract tar.bz2, .tar.bz, .tbz or .tbz2 Files to Different Directory

Again repeating that you must create a separate directory before unpacking files:

mkdir -p /tmp/tar.bz2

Now we will be unpacking the documents.tbz2 files to /tmp/tar.bz2/ directory.

tar -jvxf documents.tbz2 -C /tmp/tar.bz2/ 
Extract tar.bz2 Files to Different Directory
Img 04: Extract tar.bz2 Files to Different Directory

How to Extract Only Specific or Selected Files from Tar Archive

The tar utility also allows you to define the files that you want to only extract from a .tar file. In the next example, I will extract specific files out of a tar file to a specific directory as follows:

mkdir /backup/tar_extracts
tar -xvf etc.tar etc/issue etc/fuse.conf etc/mysql/ -C /backup/tar_extracts/
Extract Specific Files From Tar Archive
Img 05: Extract Specific Files From Tar Archive
Summary

That is it with extracting tar files to a specific directory and also extracting specific files from a tar file. If you find this guide helpful or have more information or additional ideas, you can give me feedback by posting a comment.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

12 Comments

Leave a Reply
  1. Great article.

    Just to note that “Example 4: Extract Only Specific or Selected Files from Tar Archive” is the wrong syntax.

    The order of the arguments has to be changed. The single files or directories to be extracted need to be last:

    It shall read

    # tar -xvf etc.tar  -C /backup/tar_extracts/    etc/issue etc/fuse.conf etc/mysql/
    

    reference: https://stackoverflow.com/a/9249779

    Reply
  2. Your blog is very good, your blog has great information, your content is also very good, your blog has got a lot of help.

    Reply
    • @SHIVANI

      I would like you to know that we are pleased to have read your beautiful and kind note of appreciation and encouragement.

      Reply
  3. Hi,
    tar command accepts options without minus[-] sign
    also ‘-j’ option is optional and can be ignore….
    example:
    tar xvf …..
    Thanks a lot

    Reply
    • @Ren,

      Have you created the separate directory before extracting the specific files to that directory? The -C option is used to specify a different directory and it must exists before extracting files.

      For example,

      # mkdir /tmp/dumpfiles
      # tar -xvf powertop-2.7.tar.gz powertop-2.7/m4/po.m4 -C /tmp/dumpfiles
      

      Else, you can also extract files withing the current directory without creating a new directory like:

      # tar -xvf powertop-2.7.tar.gz powertop-2.7/m4/lib-ld.m4
      

      powertop-2.7/m4/po.m4

      Reply
      • I have the same issue as @Ren, and in my case I have created the destination dir… no matter how I specify things the extracted file always ends up in the current directory.

        Reply
  4. In the last example, extracting several files from the etc directory out of the tar file, what is the cts/ in the first line of the extracted files?

    Reply
  5. Great post. It would be helpful for Linux beginners to explain the switches used. What does -xf do? You explain the addition of the -v switch which is good. What’s the difference between -C and –directory? In Example 2, you throw in the -z switch and in Example 3, the -j switch without any explanation. Then in example 4, you talk about extracting single files, yet the command also includes an option to extract all files in a specific directory – etc/mysql/. Again, a good example for using different options, but it could use a bit of explanation.

    Reply
    • @Konrad,

      Yes, we haven’t specified the meaning of each tar command option used in these examples, the reason because we’ve already requested users to read the article that says Mastering tar Command with this 18 Examples in Linux, before heading up further. No issue you can find all these options explanation below:

      c – create a archive file.
      x – extract a archive file.
      v – show the progress of archive file.
      f – filename of archive file.
      t – viewing content of archive file.
      j – filter archive through bzip2.
      z – filter archive through gzip.
      r – append or update files or directories to existing archive file.
      W – Verify a archive file.
      wildcards (*) – Specify patters in unix tar command.
      
      Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.