user1694873
user1694873

Reputation: 473

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.

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.

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

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

Upvotes: 1

Views: 614

Answers (2)

underrun
underrun

Reputation: 6841

The answer to how to get the path to be relative to the right path is to use the -C tar option. That's a capital C. The parameter you pass is the directory from which you want the tar to start relative.

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.

Upvotes: 2

mdesantis
mdesantis

Reputation: 8517

For creating tar archives containing multiple files/folders use this:

$ 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}

Upvotes: 1

Related Questions