Permissions Rsync – Setting Ownership When Copying or Syncing Files

amazon ec2file-copypermissionsrsync

When moving or copying files as root I often want to set the ownership for those files based on the owner of the directory I am moving the files to.

Before I go off and write a script that parses the rsync output for all the files that were copied over and then goes through those setting chown on each file, is there a better/existing way to do this?

As an example, say I need to copy/move/sync the folder tmp/ftp/new-assests/ to ~user1/tmp/ and to ~user2/html-stuff/ the originals are owned by the user _www and I want the target files and the folder containing them and any other folders to be owned by user1 and user2, respectively and the target directories have existing files in them.

Yes, the users could copy the fils themselves if they had read access to that folder, but that is irrelevant in this case. Let’s assume these are all nologin users and they do not have access to the source file, if that helps.

Best Answer

Using rsync:

rsync -ai --chown=user1 tmp/ftp/new-assests/ ~user1/tmp/

This would copy the directory to the given location and at the same time change the ownership of the files to user1, if permitted.

The general form of the argument to --chown is USER:GROUP, but you may also use just USER to set a particular user as owner, as above, or just :GROUP to set a particular group (the : is not optional if you leave the user ID out).

Related Question