0

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.

0

Apparently I need to read help pages more often. If I add the --no-recursion parameter to my last command it does exactly what I want:

sudo tar --no-recursion -cjf backup.tar.bz2 a/ a/b/ a/b/test.sh

This will yield the following file structure after extracting with the command in the question:

a/             dir;    alice:work; default dir rights
  b/           dir;    alice:work; default dir rights
    test.sh    script; alice:work; executable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.