Ubuntu – How to create a common base folder with tar and how to rename folders

archivecommand linetar

So I'm pretty new to Ubuntu and the whole Linux environment though I'm a computer scientist and I want to become familiar with the basic commands.

At the moment I am working with .tar and .tar.gz files and I have a problem creating one of them.

Let's assume I have the following file structure:

./
|-> a
    |-> Release
        |-> [some files]
|-> b
    |-> Release
        |-> [some files]

With the following commands I currently create my tar-file:

tar -cvf ../archive.tar a
tar -rvf ../archive.tar b

Now my archive.tar also has the format

archive.tar
|-> a
    |-> Release
        |-> [some files]
|-> b
    |-> Release
        |-> [some files]

Now let's come to my question:
How can I achieve the following three archive structures given the said file structure above:

  1. Add a common base folder

    archive.tar
    |-> baseFolder
        |-> a
            |-> Release
                |-> [some files]
        |-> b
            |-> Release
                |-> [some files]
    
  2. Rename the folders

    archive.tar
    |-> aChangedFolderName
        |-> Release
            |-> [some files]
    |-> bChangedFolderName
        |-> Release
            |-> [some files]
    
  3. Add a common base folder and rename the folders

    archive.tar
    |-> baseFolder
        |-> aChangedFolderName
            |-> Release
                |-> [some files]
        |-> bChangedFolderName
            |-> Release
                |-> [some files]
    

For me it doesn't matter if it just can be accomplished in multiple steps, at least I hope I can do it.
And: #1 would be the most important to me.

Best Answer

You can do 1. with the following command:

tar cvf archive.tar --transform 's,^,baseFolder/,'  a b

The archive will contain:

$ tar tvf archive.tar
drwxrwxr-x sylvain/sylvain   0 2015-03-18 12:47 baseFolder/a/
-rw-rw-r-- sylvain/sylvain   0 2015-03-18 12:47 baseFolder/a/foo
drwxrwxr-x sylvain/sylvain   0 2015-03-18 12:47 baseFolder/b/
-rw-rw-r-- sylvain/sylvain   0 2015-03-18 12:47 baseFolder/b/bar

For 2. you can run tar with multiple --transform options:

tar cvf archive.tar --transform 's,^a,changed_a,' --transform 's,^b,changed_b,'  a b

The archive will contain:

$ tar tvf archive.tar
drwxrwxr-x sylvain/sylvain   0 2015-03-18 12:47 changed_a/
-rw-rw-r-- sylvain/sylvain   0 2015-03-18 12:47 changed_a/foo
drwxrwxr-x sylvain/sylvain   0 2015-03-18 12:47 changed_b/
-rw-rw-r-- sylvain/sylvain   0 2015-03-18 12:47 changed_b/bar

For 3. you can combine 1. and 2. as follows:

tar cvf archive.tar --transform 's,^a,changed_a,' --transform 's,^b,changed_b,' --transform 's,^,baseFolder/,' a b

The archive will contain:

$ tar tvf archive.tar
drwxrwxr-x sylvain/sylvain   0 2015-03-18 12:47 baseFolder/changed_a/
-rw-rw-r-- sylvain/sylvain   0 2015-03-18 12:47 baseFolder/changed_a/foo
drwxrwxr-x sylvain/sylvain   0 2015-03-18 12:47 baseFolder/changed_b/
-rw-rw-r-- sylvain/sylvain   0 2015-03-18 12:47 baseFolder/changed_b/bar
Related Question