I have a particular use case, where I want to extract a tar archive (as root) and intentionally overwrite some destination file(s) with the contents of the archive. This is all fine and easily achievable, but I also want to retain the original ownership and permissions of the original destination file(s). As an example:
$ touch file && tar cf test.tar.gz file &&
sudo chown www-data:www-data file &&
sudo tar xf test.tar.gz && ls -l file
-rw-r--r-- 1 tim tim 0 May 1 11:26 file
Here I create a file as my user (tim:tim
), archive it, change its ownership to www-data:www-data
, then (as root) extract the archive, overwriting the original file. As you can see, its ownership has been modified to that of the file in its pre-archived state, whereas post extraction, I want it to be owned by www-data:www-data
.
I've had a fairly close look at the tar
man page, but can't see an immediately obvious way to do what I want. Am I missing anything?