Specify root dir inside tarball

Solution 1:

Make a symlink first, and have tar follow it.

ln -s project-root something-else
tar cjf foo.tar.bz2 something-else/*
rm something-else

The /* is necessary to follow the link and select all the contents rather than just tarring the symlink itself. You could alternatively use the -h option on GNU tar (but this will also follow links inside, which you may not want to do) or the H option on BSD tar (like on a Mac).

You could even do this from inside project-root:

cd project-root
ln -s . something-else
tar cjf foo.tar.bz2 --exclude=something-else/something-else something-else/*
rm something-else

Unless you explicitly exclude it (as above), the symlink will exist in the tarball too.

Obviously if you are doing this lots of times and don't mind the symlink hanging around it doesn't have to be deleted and recreated each time.

Solution 2:

You can rename project-root inside the tarball with the 7-zip GUI (and probably other archiver programs as well), but this may do a full decompression-recompression cycle on compressed tarballs. (Read: this may take a while on large, heavily-compressed tarballs.)

The best way to do what you want is to change the name before creating the tarball. A simple way would be to temporarily rename project-root to something-else:

# temporary rename; tar; restore original name
$ mv project-root something-else ; tar cjf foo.tar.bz2 something-else ; mv something-else project-root

I'd probably go a little more complicated: make a copy with the name I want in the tarball. This avoids potential bugs or missed steps in whatever fix-it-up-later process might be employed, and I might want to keep both copies around for other purposes.

# make me a copy
$ ( cd project-root ; tar cf - . ) | ( mkdir something-else; cd something-else; tar xf - )

# test the copy here if needed
$ diff -r --brief project-root/ something-else/

# create my tarball
$ tar cjf foo.tar.bz2 something-else