Lets say I have the following directory structure somewhere on my file system:
a/ dir; alice:work; default dir rights
b/ dir; alice:work; default dir rights
test.sh script; alice:work; executable
Next, I create an archive of this directory structure as bob and I only list test.sh as the file I want to add to the archive:
sudo tar cjf backup.tar.bz2 a/b/test.sh
Now I have an archive backup.tar.bz2 which should have retained the ownerships & rights of the file I specified in the command (and I also expect the same for the parent directories a & b!).
Now, here's my predicament. If I, still as bob, try to extract the contents of the archive in a different location on my file system as a test, I'd expect to get the same file structure with the same ownerships & rights as when I created the archive with. However, when using the following command
sudo tar xjf backup.tar.bz2
I get the following file structure instead:
a/ dir; root:root; default dir rights
b/ dir; root:root; default dir rights
test.sh script; alice:work; executable
Somehow the ownership of the parent directories is not kept when extracting from the archive. It seems it gives the ownership of those directories to whichever user extracted the archive (in my case root as I want to preserve ownership & rights).
Is there a way to tell tar to keep the ownership of the parent directories as well? I know it's possible by including them in the tar command that creates the archive:
sudo tar cjf backup.tar.bz2 a/ a/b/ a/b/test.sh
But I'd rather not do that in case there are other files or directories in either a/ or a/b/ as then those would also be part of the archive.