Untar a specifc folder within tar.gz

8,765

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 ~/locationX/folder1, folder2 will go into ~/locationY/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.

Share:
8,765

Related videos on Youtube

frank
Author by

frank

Updated on September 18, 2022

Comments

  • frank
    frank over 1 year

    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...

  • frank
    frank over 9 years
    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!
  • BAR
    BAR over 9 years
    @frank There is the stackexchange community for you. Welcome!
  • iceman
    iceman over 9 years
    And I thought xkcd.com/1168