As its name suggests, rsync is a synchronization tool. The first time you use it, it simply copies files and directories from one location to another. Subsequent uses, however, synchronize only the differences between these locations. So, if you add one file to the source directory, rsync will copy only the new file to the destination. This saves a lot of time and bandwidth, especially when copying to a remote location.
Imagine you have 50GB of data you have to synchronize to a remote server. You can schedule rsync to run daily. Instead of having to copy the whole 50GB over and over again, the utility will only send the differences. This might mean it only has to upload a few megabytes of data every day, instead of gigabytes.
In the featured picture above, you can see how the first time 104MB were transferred, while during the second use only 31KB were required.
How to Install rsync on Various Linux Distributions
On Debian and Debian-based distributions, like Ubuntu, you can install rsync with:
On Fedora, install with:
And on distributions derived from RedHat (e.g., CentOS):
To install rsync on Arch Linux, use the command:
On openSUSE, use:
If it isn’t included in your default repositories, consult the openSUSE rsync installation page.
How to Use rsync to Synchronize Local Files or Directories
The general syntax of rsync is:
rsync [options] /path/to/source /path/to/destination.
[options] stands for command line options which you can find in the manual:
Press q to quit the manual.
To copy a file:
The command above is equivalent to
rsync /bin/ls /home/your_username. This copies the “ls” command to the directory you are currently in. By default, this should be
/home/your_username. The dot
. stands for current directory. Pathnames can be absolute (
/home/your_username/Downloads/chrome.zip) or relative to your current directory (
When you deal with very large files, you can use this command:
This will show progress and allow you to resume an interrupted transfer. In case you need to resume, use:
--append, the transfer would start from scratch.
To synchronize directories:
Effect of Trailing Slash in rsync (Important)
In the previous command note that the path to the source directory doesn’t contain a trailing slash
/bin/). Without a trailing slash, rsync copies the directory itself. So
rsync -av /bin . results in what is depicted in the following image.
When a trailing slash is used, rsync copies the contents in that directory. In this case every file in
/bin would be scattered around in the destination directory. So
rsync -av /bin/ . would result in what is depicted in the following image.
It’s very important to remember this difference, as using TAB to autocomplete directory names automatically adds a trailing slash at the end. So keep that in mind to avoid creating a mess in the destination directory.
How to Use rsync to Synchronize with Remote Locations (e.g. Servers)
What was described in the previous section holds true when working with remote locations. The only difference is that you have to prepend the login username and address of the remote location. To send your
/bin directory to a remote computer, use:
rsync connects through OpenSSH. This means you have to set up the OpenSSH server and login credentials on the remote destination beforehand. Note that
your_username refers to the username created on the server. This might be different from the username you have on your local machine.
If you own a domain name and point it to your server IP, you can use the domain in the command:
On large transfers, it’s useful to also compress data before sending it through the wire by adding the
You can omit the compression parameter when you send files that are already compressed (audio files like MP3, videos, JPEG images). They cannot be compressed further, so you would actually waste time by using
-z on these.
To copy files from the remote destination to your local computer, just reverse locations:
This covers the most important rsync commands you need to know. If you ever need more control over how to synchronize directories, consult the online rsync manual and scroll a few pages down until you find “Options Summary.”