0

I've already checked many threads regarding the topic and tried different flags combinations, but couldn't solve the problem.

Scenario: I'm working under user A and have source files owned by A, target files are owned by user B, I copy source to destination using rsync under sudo and want target files to stay owned by B after copying.

sudo rsync -ar src/ dst/

result: A:A

sudo rsync -ar --no-o src/ dst/

result: B:A (if files contents is same) or root:A (if files contents is different)

How can I keep B:B on target files?

For example, simple sudo cp -r src/ dst/ can do this.

1

Use --chown=B:B

From the rsync manual:

--chown=USER:GROUP

This option forces all files to be owned by USER with group GROUP. [...] If either the USER or GROUP is empty, no mapping for the omitted user/group will occur. If GROUP is empty, the trailing colon may be omitted, but if USER is empty, a leading colon must be supplied. [...]

If you want to preserve the ownerships of the existing files on the destination side, then the easiest way to do that is to use both --no-group and --no-user and then post-process the destination files and directories in such a way that anything that is owned by root gets owned by the other user.

This could be done through

rsync -a --no-group --no-user src/ dst

(note that -r is implied by -a/--archive), and then

find dst -user  root -exec chown B {} +
find dst -group root -exec chgrp B {} +

This would find anything in or below dst that is owned by the root user or group (these would be new files added by rsync at the destination), and changes the owner and group of these to B.

  • But what if I don't know target owner or target files have mixed owners? I don't want to assign target owner, I want to keep it unchanged. – o.v Jan 11 at 12:33
  • @o.v The easiest way to do that is to use both --no-group and --no-user and then post-process the destination files and directories in such a way that anything that is owned by root gets owned by the other user. I will add this to the answer. – Kusalananda Jan 11 at 12:38

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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