Extract tar files to a specific directory on Linux

Utility tar is a utility that helps us to create backups on any Linux system, it includes many options which we must specify depending on what we want to do.

Something to know is that you can extract a file with the extension .tar to any directory, as long as we specify that directory, not necessarily in our current directory.

Here in the following example, we have the syntax to extract a file

# tar -xf filename.tar -C / file_path / folder
# tar -xf filename.tar.gz --directory / file_path / folder

NOTE: In the first syntax, the -C is to specify that you are working in a different directory than the current one, that is, when we are going to change the directory or folder.

Here are some examples where this can be better explained:

Example 1: Extract .tar files

We are going to extract the files from article.tar to a / tmp / my_article directory. Something that is very important is that you make sure that the destination directory or folder exists before extracting the .tar

We are going to create the destination folder with the following command:say

# mkdir / tmp / my_article

Now, to extract the files from article.tar to / tmp / my_article we execute the following:

# tar -xvf article.tar -C / tmp / my_article /

It can also be used –Directory instead of -C, they have exactly the same function

Example 2: Extract .tar.gz and .tgz files

As in the previous example, we must make sure that the destination folder exists using:

# mkdir / tmp /tgz

Now we are going to extract documents.tgz to the folder we created earlier

# tar -zvxf docs.tgz -C / tmp / tgz /

It can also be used this way (it does exactly the same thing)

# tar -zvxf docs.tgz --directory / tmp / tgz /

Example 3: Extract tar.bz2, tar.bz, .tbz or .tbz2 files to another directory

Once again, we make sure that the destination folder exists using:

# mkdir / tmp /tar-bz

And we unzip the file called documents.tbz2 in the folder created before

# tar -jvxf docs.tbz2 -C / tmp / tar-bz

Example 4: Extract one or more specific folders within the .tar file to a specific directory

Something that we can also do with tar, is to extract a specific part of the file that we are decompressing, without having to extract all the content.

In this case the file is called etc tar and the destination folder / tar-specific

Once again, we make sure that the destination folder exists using:

# mkdir / tmp /tar-specific
# tar -xvf etc.tar etc / issues / etc / content.odt etc / mysql / -C / tmp / tar-specific

Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Jose said

    Hi, does anyone know how I could move Google Chrome cache from hard drive to RAM in Ubuntu 14.04 LTS?

    1.    Leo said

      I do, it's easy. Just modify the fstab with the sig. values:
      tmpfs /home/Your_USER/.config/google-chrome/Default/Cache/ tmpfs defaults, exec, nosuid, nodev, mode = 0777 0 0

      I hope you serve.

  2.   Chaparral said

    Thank you, thank you very much for the explanation.

  3.   Alexander TorMar said

    Thanks for the explanation, this tutorial was needed (The other one out there as of 2012 never worked for me). You will have to read it several times and practice it ...

  4.   fedora_user said

    This is elementary I am surprised that you have to make a post explaining this.
    Nobody reads the manuals anymore?
    $ man tar !!!

  5.   Mauricio Lopez said

    Thanks for the clear explanation.