10

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.

1 Answer 1

16

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
3
  • 1
    Thank you for giving me the hint with --transform. Just looked it up (link) to get more information about it. Nice to able to work with regex here! Mar 18, 2015 at 12:58
  • 2
    @MarkusWeninger your link breaks, it's 6.7 Modifying File and Member Names for now.
    – Weekend
    Jan 26, 2019 at 5:48
  • The link have broken again. Try this link.
    – MrValdez
    Oct 3, 2022 at 4:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .