I have been working on creating a bunch of debootstrap images lately, and I ended up doing a whole bunch of work creating tarballs and did it wrong.  So I took the time to sort out an easy way to fix them all with two commands.

The Core of My Problem

So what started this is I forgot to “stand” in the directory I was archiving, as such all of my images ended up having a top level directory named lenny32 (insert appropriate distribution and architecture here) which isn’t very helpful when you are extracting it to the root of a file system.

Here is the command I used to originally create my archive.

bash
tar -czf lenny32.tgz lenny32/

Here is the command I should have used.

bash
tar -czf lenny32.tgz -C lenny32/ .

The -C switch tells tar to cd into the directory before performing the archive.

Now before we get started lets make sure that you are have 2 separate working directories, one for .tgz files and one for the source files.

Extracting All of the Archives

So the first part of the process is re-extracting all of the archives, you can optionally just re-archive your original source files provided that they are still accessible.  This assumes that you have created an unfixed directory, in which we will be locating our source files.

bash
for a in `ls -l *.tgz`; do echo -n $a; tar -xzf $a -C unfixed; echo "     done."; done

Recreating All of the Archives

Now since we have extracted everything we end up with a directory which holds a bunch of subdirectories for each of our archives.  Now we will turn each directory into its own archive.  Make sure there are no extra directories in here.

bash
for a in `ls`; do echo -n $a; tar -czf $a.tgz -C $a .; echo "      done."; done

Now once you have done this you will have your source files in the same directory as your fixed tarballs.  This can be resolved by doing a move.

bash
mv *.tgz ../fixed

There you go.  This is really rather simple, but it isn’t something I use frequently so I wanted to write it down.  Now when you extract one of these archives make sure you are “standing” in an empty directory OR use the -C option to have tar stand in that empty directory for you, otherwise you will dirty up that directory.

bash
tar -xzf lenny32.tgz -C lenny32/

Enjoy!