6

How do I untar/uncompress a specific folder within a tar.gz using the terminal/commandline (Ubuntu 14.04 (Trusty Tahr))?

Precise:

-file.tar.gz
 -- folder1
 -- folder2

How to extract...

... folder1 -->  ~/locationX
... folder2 --> ~/locationY

How can I do this?

Please notice: This is purely related to terminal/commandline and not Puppet. This is just one example - I would save roughly 50 additional steps in Puppet, if there is a command to uncompress a specific folder from within a tar.gz as I could avoid moving files around...

14

I think this should do it:

tar -xzf file.tar.gz -C ~/locationX folder1 -C ~/locationY folder2

The -C option means to change to the specified directory before doing the extraction. Specifying filename arguments after the tarfile name restricts the extraction to just those files or directories. And you can repeat this -Changing directories as you do.

Note that this will do the extractions in subdirectories: Folder1 will go into pathX/Folder1, Folder2 will go into folderY/Folder2. You can prevent this by using the --strip-components option:

tar -xzf file.tar.gz -C ~/locationX --strip-components=1 folder1 \
                     -C ~/locationY --strip-components=1 folder2

Also, if the actual prefixes in the tar file are ./folder1 and ./folder2, as is likely if you created the tar file with tar -czf file.tar.gz ., you'll need to include the ./ prefix in the filename arguments, and change to --strip-components=2.

  • awesome! Apart from the fact that you solved the question I had, I can´t remember when I received such a foreward thinking answer in any forum. Thank you very much! – frank Dec 19 '14 at 21:45
  • @frank There is the stackexchange community for you. Welcome! – BAR Dec 20 '14 at 0:40
  • And I thought xkcd.com/1168 – iceman Dec 28 '14 at 0:57

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.