The rsync command uses SSH to synchronize files between a remote directory and a local directory. The advantage of synchronizing files is that only differences need to be considered. So, for example, if you synchronize a 100-MiB file in which only a few blocks have changed since the previous sync, only the changed blocks will be synchronized. This approach is also known as a delta sync.
The rsync command can copy files over SSH, or it can use the rsyncd daemon if you set it up on the remote system. The following is an example of synchronizing the files in a local directory to a remote directory over SSH:
# rsync -a /home/mydir/ user@host:/home/mydir/
Some of the additional features of rsync are
- support for copying links, devices, owners, groups, and permissions
- exclude and exclude-from options similar to GNU tar
- a CVS exclude mode for ignoring the same files that CVS would ignore
- can use any transparent remote shell, including ssh or rsh
- does not require super-user privileges
- pipelining of file transfers to minimize latency costs
- support for anonymous or authenticated rsync daemons (ideal for mirroring)
Commonly used rsync options
When you use the rsync command, multiple options are available. Table below provides an overview.
Option | Use |
---|---|
-r | Synchronizes the entire directory tree |
-l | Also synchronizes symbolic links |
-p | Preserves symbolic links |
-n | Performs only a dry run, not actually synchronizing anything |
-a | Uses archive mode, thus ensuring that entire subdirectory trees and all file properties will be synchronized |
-A | Uses archive mode, and in addition synchronizes ACLs |
-X | Synchronizes SELinux context as well |
If you encounter the below error while running the rsync command:
rsync: command not found
you may try installing the below package as per your choice of distribution:
OS Distribution | Command |
---|---|
OS X | brew install rsync |
Debian | apt-get install rsync |
Ubuntu | apt-get install rsync |
Alpine | apk add rsync |
Arch Linux | pacman -S rsync |
Kali Linux | apt-get install rsync |
CentOS | yum install rsync |
Fedora | dnf install rsync |
Raspbian | apt-get install rsync |
rsync Command Examples
1. Transfer file from local to remote host:
# rsync path/to/local_file remote_host:path/to/remote_directory
2. Transfer file from remote host to local:
# rsync remote_host:path/to/remote_file path/to/local_directory
3. Transfer file in [a]rchive (to preserve attributes) and compressed ([z]ipped) mode with [v]erbose and [h]uman-readable [P]rogress:
# rsync -azvhP path/to/local_file remote_host:path/to/remote_directory
4. Transfer a directory and all its children from a remote to local:
# rsync -r remote_host:path/to/remote_directory path/to/local_directory
5. Transfer directory contents (but not the directory itself) from a remote to local:
# rsync -r remote_host:path/to/remote_directory/ path/to/local_directory
6. Transfer a directory [r]ecursively, in [a]rchive to preserve attributes, resolving contained soft[l]inks , and ignoring already transferred files [u]nless newer:
# rsync -rauL remote_host:path/to/remote_directory path/to/local_directory
7. Transfer file over SSH and delete remote files that do not exist locally:
# rsync -e ssh --delete remote_host:path/to/remote_file path/to/local_file
8. Transfer file over SSH using a different port than the default and show global progress:
# rsync -e 'ssh -p port' --info=progress2 remote_host:path/to/remote_file path/to/local_file