1

I have a tar archive and I need to extract the contents of a directory into another directory.

So for example the content may be located in /home/me/stuff/ and everything in the stuff folder should be extracted to /extract. So after the extract is complete everything that was in the archived /home/me/stuff/ folder is now in the /extract folder.

Thoughts?

  • You are not telling us how and in which directory you generated that tar. – Rui F Ribeiro May 31 '18 at 2:25
3

If I understand what you are asking, it looks like, with GNU tar, the following will do it:

tar --extract --file in.tar --directory /extract /home/me/stuff --strip 3

Simulating with an archive that has the following contents:

$ tar tf in.tar
home/
home/me/
home/me/stuff/
home/me/stuff/b
home/me/stuff/a
home/foo

And an output directory /tmp/extract (which must be created beforehand), we get:

$ find /tmp/extract
/tmp/extract
/tmp/extract/b
/tmp/extract/a

Note how the file foo was not extracted at all.

  • Ah, and now I see that you probably meant for the leading part home/me/stuff to be stripped. Oops. – dhag May 31 '18 at 2:34
  • With GNU tar, there's --strip-components – steeldriver May 31 '18 at 2:36
  • Thanks, I ended up noticing that right after posting my answer. – dhag May 31 '18 at 2:44
  • OK - So IIUC the --strip 3 means skip everything starting from root (/) down three directories? – Ole May 31 '18 at 2:55
  • 1
    Yes, that's the idea. Perhaps it's not very convenient to have to compute the value 3, but then again it's possible to write a script around this if this is a recurring need. – dhag May 31 '18 at 3:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.