Rsync is a great tool to copy and sync directories and files across computers. It can also be used to manage source code and data on production machines or take backup of files and directories. This article will use Ubuntu Linux but rsync can run on other operating systems also.
Here are some common use cases to use rsync:
Basic archive rsync
This is most widely used option. It is quick way to specify that you want recursion and want to preserver almost everything.
$ rsync -azv ./src ./dest/
Note that ./dest/ is a directory and this will create src folder inside it, if it does not exist. Option -v is to add verbosity and -z is to enable compression.
The files are transferred in “archive” mode, which ensures that symbolic links, devices, attributes, permissions, ownerships, etc. are preserved in the transfer.
skip files that are newer on the receiver
This is very useful option in case you want to rsync only if files are newer on local system.
$ rsync --update -azv ./src ./dest/
delete extraneous files from dest dirs
This option must be used with caution. Avoid if possible.
$ rsync --delete --update -azv ./src ./dest/
rsync with dryrun
To run rsycn with dryrun:
$ rsync --dry-run -azv ./src ./dest/
Note that rsync by default check file size and timestamp on source an destination to decide if it should transfer the file. This can be changed with –checksum option which will cause it to check for checksum for all files.