The rsync
command is a powerful tool for synchronizing files and directories between two locations. It is commonly used in backup and mirroring operations.
When using rsync
, the -a
or --archive
option is often used to preserve the permissions, ownership, and timestamps of the files being transferred(https://www.zyxware.com/articles/3660/how-to-preserve-permissions-ownership-timestamp-archive-mode-in-rsync-using-rsync-a)[[4]]. This means that rsync
will attempt to maintain the same permissions and ownership in the destination as in the source.
However, it's important to note that the user running the rsync
command needs to have the necessary permissions to read the source files and write to the destination directory. If the user doesn't have these permissions, the rsync
command may fail.
The rsync
command can be used to move files from one location to another by using the --remove-source-files
option. This option tells rsync
to remove the source files after they have been successfully transferred to the destination.
Here's an example of how to use this option:
rsync -av --remove-source-files source_directory/ destination_directory/
In this command, -a
stands for "archive", which preserves permissions, ownership, and timestamps, and -v
stands for "verbose", which provides detailed output of the operation. The --remove-source-files
option tells rsync
to remove the source files after they have been successfully transferred.
While the -a
option preserves the permissions and ownership of the files, the ownership of the transferred files may change to the invoking user on the receiving end if you don't use certain options. If you want to specify some other user, you will need to add a chown
command to your script.
Moreover, if the source has nonsensical permissions, rsync
may fail because it creates the destination directories with the same permissions, then can't create any files in them. In such cases, you can use the --chmod=ugo=rwX
option to ensure that all non-masked bits get enabled, giving new files the destination-default permissions while leaving existing files unchanged.