Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract a specific folder to specific directory from a tar.gz

I have searched and found how to the two portions of what I want but nothing that would allow you to do it in whole....

What I would like to do is extract a specific folder from a tar.gz to another folder in a different path that how it is in the tar.gz.

For example:

  • Directory path in my backup.tar.gz file is: a/b/c/d (d is my main with many others inside)

  • I want to unpack directory 'd' into 'm' inside this different server path: a/b/m

  • If I attempt this code: tar -xzf backup.tar.gz -C a/b/m

** The folder structure looks like: a/b/m/a/b/c/d but I would like it to look like a/b/m+d so that all my main files/folders in the archived 'd' path ends up in the 'm' path

Any help is greatly appreciated.

Thanks!

like image 759
Yevgen Avatar asked Jun 15 '14 21:06

Yevgen


People also ask

How do I extract a directory from a tar GZ file?

To extract (unzip) a tar. gz file simply right-click on the file you want to extract and select “Extract”. Windows users will need a tool named 7zip to extract tar. gz files.

How do I extract a single file from a tar file in Linux?

For that, you need to make use of the “tar” command with the “-xvf” option and the name of a “tar” file while we are currently located at the “Downloads” folder. The option “x” is used for extraction, “-v” is used to display in ascending order, and “-f” is used to perform the extraction forcefully.


1 Answers

Ok I figured it out!

Basically I can just use the strip command to remove the x number of leading directories. In this case, my command would look like this:

tar -xzf backup.tar.gz --strip-components=3 -C a/b/m

That removed the first three path directories from my archive (backup.tar.gz : a/b/c/d) before extracting it to the desctination directory.

Now it looks like this: a/b/m+d

like image 160
Yevgen Avatar answered Oct 01 '22 04:10

Yevgen