Score:7

scp: How to change ownership of folder and files while transferring files

mn flag

When I transfer folder with files from srv1 with the username srv1 to srv2 with the username srv2 then the transferred folder and its containing files on srv2 are keeping the ownership srv1

scp -r /var/www/vhosts/srv1.com/httpdocs/profile_image root@777.777.777.77:/var/www/vhosts/srv2.com/httpdocs

Is there a way to change the owner of the transferred folder/files at the destination instantly to srv2 while transferring, instead of doing it afterward?

Raffa avatar
jp flag
Maybe with `rsync` but not `scp` AFAIK.
cn flag
is there a reason for this? I would have gone for a "group": add both to the same group(id) on both systems. Permissions would then work too if you need to use the files on srv2.
user19559647 avatar
mn flag
I need the users to be able to edit the new files they are uploading to all servers at the same time, but when they are overwritten by transferring everything they wouldn't be able to do that because of wrong ownership
Score:8
jp flag

scp doesn't have an option for changing ownership on-the-fly but, rsync does ... it has -og --chown=user:group ... So, you can do something like:

rsync -og --chown=srv2 /var/www/vhosts/srv1.com/httpdocs/profile_image root@777.777.777.77:/var/www/vhosts/srv2.com/httpdocs

To change the user only and keep the source group ... You can also change the group only with -og --chown=:group(Notice the colon :) keeping the source user intact or obviously change both with -og --chown=user:group … These will not affect the original source files/directories but will rather be sent to the destination connecting shell as if they were actually originally applied likewise to the source files/directories replacing the actual ownership attributes of the original source files/directories.

Notices:

  • The user(on the source machine) must have permissions to change the original files' ownership.

  • If the option --archive, -a is used, then the -og options might not be needed.

  • If you don't specify any of the above mentioned or other user and group preserving options, the files/directories ownership will be set to the remote user(root in your case) by default.

  • When dealing with remote copying/syncing tools like rsync, it might be worth reading: Using scp to copy a remote file containing spaces requires double space escaping. Why?

  • You might want to read man rsync and get familiar with its other copying/syncing options before you issue your first command.

in flag
Absolutely spot-on answer. If you need to do any type of remote file copy other than copying the contents of a couple of small files, `rsync` is usually the correct answer unless there is some domain-specific tool for what you’re doing (and even then may still be the answer). I seriously wish it got covered more frequently in Linux command-line tutorials, because so many newer Linux users never learn about it.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.