0

I am trying to untar a file which has a *tar.gz extension to its own directory.

tar -zxf /tmp/ranger/ranger-2.1.0-solr_audit_conf.tar.gz 

If I do like above, it's untaring all files to its present working directory, but I want all those files go to its own DIR.

The following command extracts to a specific DIR:

tar -zxf /tmp/ranger/ranger-2.1.0-solr_audit_conf.tar.gz -C /tmp/ranger/ranger-2.1.0-solr_audit_conf 

NOTE: If I try without -C for any other files, it is able to extract to its own DIR, which is interesting.

1 Answer 1

1

tar -x will extract the contents of the *.tar.* file to the present working directory... Period. That being said, here's what that means, and how we can work around that to get the behaviour you want.

Usually, a directory is tarballed. Meaning someone wrote:

tar -czf tarball.tar.gz somedirectory 

In that case, if you tar -x it you'll get the contents extracted to somedirectory. However that's only by convention. It's very possible for someone to compress an archive with:

tar -czf tarball.tar.gz file1 file2 file3 

In that case, when you tar -x, you'll get file1, file2, and file3 in the current directory.


In this situation, if you want to keep the extracted files in an isolated directory, you should:

mkdir tarball && cd tarball && tar -xzf ../tarball.tar.gz 

To understand if you should tar -x or mkdir _ & cd _ & tar -x ../_, you can use tar --list -f tarball.tar.gz. This will list the contents of the tarball before you extract it so you can decide if you want to extract it to pwd as-is, or create a new directory and extract it from there.

1
  • Thank you for the explanation. I did what you mentioned. Commented Mar 21, 2021 at 5:14

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.