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

10,413

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
Share:
10,413

Related videos on Youtube

Markus Weninger
Author by

Markus Weninger

Working as a Senior Lecturer in the Bachelor's and Master's program Computer Science at the Institute for System Software at the Johannes Kepler University Linz, Austria. Taught courses: Basics of Software Development, Software Development 1, Software Development 2, Compiler Construction, Algorithms and Datastructures 2, as well as Programming in Kotlin. PhD in Computer Science at the Johannes Kepler University Linz, Austria. Thesis: "Detection and Analysis of Memory Anomalies in Managed Languages Using Trace-Based Memory Monitoring" Master in Computer Science (focus on Software Engineering) at the Johannes Kepler University Linz, Austria. Bachelor in Informatik (Informatics) at the Johannes Kepler University Linz, Austria. Always eager to learn something new about ... everything interesting. Starting with the design and principles of programming languages, coding challenges and coding contests, game programming, micro controllers, code quality, software architecture, to how to improve my SE teaching skills, and much more. Hobbies include volunteer work as part of Rotaract, watching films, listening to music, going to concerts, geocaching, and a lot of board gaming. :)

Updated on September 18, 2022

Comments

  • Markus Weninger
    Markus Weninger over 1 year

    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.

  • Weekend
    Weekend over 5 years
    @MarkusWeninger your link breaks, it's 6.7 Modifying File and Member Names for now.