简体   繁体   English

Tarring文件排除了我想要tar的文件夹之前的路径

[英]Tarring files exclude the path before the folder i want to tar

Im trying to tar a set of subfolders and then tar its parent folder afterwards via a ruby script. 我试图tar一组子文件夹,然后通过ruby脚本tar其父文件夹。

The structure is as follows: 结构如下:

/x/y/z/ParentFolder/Subfolder1
/x/y/z/ParentFolder/Subfolder2
/x/y/z/ParentFolder/Subfolder3
/x/y/z/ParentFolder/Subfolder4

So what i want to end up with is Subfolder1.tar.gz,Subfolder2.tar.gz,Subfolder3.tar.gz,Subfolder4.tar.gz all contained in ParentFolder.tar.gz. 所以我最终想要的是Subfolder1.tar.gz,Subfolder2.tar.gz,Subfolder3.tar.gz,Subfolder4.tar.gz都包含在ParentFolder.tar.gz中。

My problem at the moment is that im able to tar the parent folder with its subfolders but it structure remains as /x/y/z/ParentFolder/SubFolder1----4 我的问题是,我能够使用其子文件夹来tar父文件夹,但它的结构仍为/ x / y / z / ParentFolder / SubFolder1 ---- 4

tarParentFolder = "tar -zcvf /x/y/z/ParentFolder.tar.gz /x/y/z/ParentFolder 2>/dev/null"

`#{tarParentFolder}`

I have searched around but cannot seem to find a solution to this, 我已经四处寻找,但似乎无法找到解决方案,

Anybody got any ideas? 有人有任何想法吗?

Thanks 谢谢

The answer to how to get the path to be relative to the right path is to use the -C tar option. 如何使路径相对于正确的路径的答案是使用-C tar选项。 That's a capital C. The parameter you pass is the directory from which you want the tar to start relative. 这是一个大写字母C.您传递的参数是您希望tar从中开始相对的目录。

so you would do: 所以你会这样做:

tar -zcvf /x/y/z/ParentFolder.tar.gz -C /x/y/z ParentFolder

But ... you should also probably think twice about putting tars in tars. 但是......你也应该三思而后行。 You should be fine just tarring up the containing dir. 你应该没问题,只需要包含dir。

For creating tar archives containing multiple files/folders use this: 要创建包含多个文件/文件夹的tar存档,请使用以下命令:

$ mkdir f1 f2
$ tar -czf tar.tgz f1 f2 # creates the tar
$ tar -tzf tar.tgz       # lists tar contents
f1/
f2/
f3/
$

So you should write something like: 所以你应该这样写:

tar -zcvf /x/y/z/ParentFolder.tar.gz /x/y/z/Subfolder{1,2,3,4}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM