Change file ownership while copying

How to change the ownership of the file during the copy itself. I mean the cp command how to add ownership

I cannot rely on --preserve and copy the file, and then change the ownership of the file due to some restrictions. Thanks

+6
source share
4 answers

Not easy. Perhaps you could run the cp command as the user you want to own the new file, or copy it aside to a temporary location, chown , then copy it to where it wants to get it, but cp itself lacks chown .

+2
source

This is not a direct answer to your question, but if you have GNU cpio , you can use it (in copy mode) to copy files when changing ownership. eg.

 echo 'some_file_name' | cpio -p --owner someuser:somegroup destination_directory 
+2
source

As stated here: https://unix.stackexchange.com/questions/124855/move-files-and-change-ownership-at-the-sametime

Use rsync (1):

 rsync \ --remove-source-files \ --chown=wanteduser:wantedgroup \ /home/oldfolder /home/newfolder 
+1
source

I use a workaround with the scp command:

 scp -p file.txt <user>@localhost:<destination> 

Example:

 scp -p /home/reportuser/dailyReport.doc root@localhost :/root/dailyReports/20150105/ 

-p - save the timestamp of the source file

+1
source

Source: https://habr.com/ru/post/950817/


All Articles