12

I observe the following behavior with both tar 1.26 and 1.27.1:

$ mkdir a b a/diffowner
$ sudo mkdir b/diffowner
$ sudo chmod a+w b/diffowner
$ echo foo > a/diffowner/foo
$ tar -C a -cvf test.tar diffowner
diffowner/
diffowner/foo
$ tar -C b -xvf test.tar diffowner
diffowner/
diffowner/foo
tar: diffowner: Cannot utime: Operation not permitted
tar: diffowner: Cannot change mode to rwxr-xr-x: Operation not permitted
tar: Exiting with failure status due to previous errors

So what I'm trying to do here is extracting a tar file into an existing directory structure, where I'm not the owner of all the directories involved but I do have write permission on all of them. In fact they are shared among a group.

I don't care about timestamps, and I trust that permissions should be correct already. I'm running this as a normal user so it shouldn't be trying to --preserve-permissions unless told so, which I did not. What really does worry me is the exit status: I intend to use this in a script, and want to know whether the actual extraction worked fine.

Is there an option to tar which tells it not to set directory permissions, neither immediately nor delayed? Failing that, what other solutions would you suggest. Right now I'm thinking about extracting to a temporary directory and using rsync to move stuff into the existing tree. But perhaps you know a less hackish approach.

  • 1
    Wondering if there's a way to do this at tar creation time as opposed to extraction..? – Roy Truelove Mar 22 '16 at 18:06
22

This tar option might be what you're looking for:

 --no-overwrite-dir
       preserve metadata of existing directories

I tested as follows:

$ mkdir a b a/diffowner
$ sudo mkdir b/diffowner
$ sudo chmod a+w b/diffowner
$ echo foo > a/diffowner/foo
$ tar -C a -cvf test.tar diffowner
diffowner/
diffowner/foo
$ tar -C b --no-overwrite-dir -xvf test.tar diffowner
diffowner/
diffowner/foo
$ echo $?
0
  • Also worked for my case, in which I extracted a tar archive to a mounted Samba share. Note that unlike the command suggests, it also suppresses the error when the destination is blank (i.e. all directories that would give errors were created by tar itself). Thanks again! – user149408 Nov 26 '16 at 1:17

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.