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

半腔热情 提交于 2021-01-20 19:37:28

问题


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!


回答1:


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




回答2:


This worked for me on OS X (pulling specific files from a GitHub repo):

# Move to target directory and run:
curl -#L https://github.com/USER/REPO/tarball/BRANCH | tar -xzv --strip-components 3 --include=*./some/folder


来源:https://stackoverflow.com/questions/24234184/extract-a-specific-folder-to-specific-directory-from-a-tar-gz

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!