The Linux rsync command transfers and synchronizes files or directories efficiently between a local machine, remote server, or any match of these. Anyone working with Linux based systems should use this powerful utility to improve their productivity. With the help of this article, you’ll learn everything you need to start using it.
Syncing folders or copying files manually can be incredibly time-consuming. The rsync utility can do most of the work for you while adding great features to save time. Even if you lose connection during transfer, this tool will start exactly where it left off, once the connection is reestablished.
The basic syntax for rsync works as follow:
rsync [optional modifiers] [SRC] [DEST]
There are a couple of different ways you can use Linux rsync. In this example, [optional modifiers] indicate the actions to be taken, [SRC] is the source directory, and [DEST] is the destination directory or machine.
How to Check the Rsync Version
Before we check for Rsync, we need to log into the VPS server we will use. This helpful tutorial will show you how to do that on a Windows machine using Putty SSH. If you’re using a MacOS or Linux computer, just use the terminal instead.
Rsync comes pre-installed with many Linux distributions. To check whether rsync is installed on your machine, execute the following command:
On our Ubuntu distribution the command produced the following output:
rsync version 3.1.2 protocol version 31
That means rsync version 3.1.2 is already on our machine. Simple, right?
If your machine doesn’t have rsync pre-installed, you can do it manually in just a minute! On Debian based distributions like Ubuntu you can do it using the following commands:
apt-get install rsync
On rpm-based distributions like Fedora and CentOS use following command:
yum install rsync
That’s it! Linux Rsync is ready to sync data, perform file transfers and delete files! You can check if the installation was successful using the previously mentioned command:
Working with Rsync
For this tutorial, we created two directories on our Linux desktop with the names Original and Duplicate. The original directory has 3 images in it while the duplicate is empty. Now let’s see how Rsync will create new ways to increase productivity.
The following command, when typed into the command line, will copy or sync all files placed in the original directory to the duplicate directory.
rsync original/* duplicate/
* instructs the rsync command to synchronize everything in the original directory to the duplicate.
If we place a new image or file in the original folder and rerun the same command, only the new image will be copied to the destination.
This feature is handy when you are copying files over a network with limited bandwidth.
Keep in mind that this command will only copy files from the Original folder’s main directory, but not any of the subdirectories.
If you want to copy the subdirectories as well, use this command:
rsync -r original/ duplicate/
The -r option tells rsync to copy everything including subdirectories and the files from our original folder.
The / modifier used after original instructs rsync to copy the contents of the original directory to the duplicate folder.
If you want to synchronize files instead – copy files that might be in the destination folder, but aren’t in original to the original folder, use the following command:
rsync -r original duplicate/
With this command, we can be sure that both original and duplicate folders will contain the same files.
Another useful option is -a. It stands for archive and can be joined with many other commands. That means it doesn’t just copy the files, it also copies the permissions, modification times, and any other date.
To use -a the command would look similar to this:
rsync -av --dry-run Original/ Duplicate/
Don’t worry, it only looks complicated! Let’s break it down.
This command will only display the files that will be copied without doing any actual changes. With this command, you can get a list of the files that will be copied.
If all the files are the ones you want to be copied, rerun the command without —dry-run.
—dry-run makes rsync perform a trial run that doesn’t make any changes.
-a enables archive mode
-v which is added to -a to making -av in our command, increases verbosity. That’s just a fancy way of saying, that using -v you’ll get a visual output that shows the progress of the process.
If you want to sync the two folders, but delete the items in the duplicate, that are not present in the original folder, add –delete, like this:
rsync -av --delete original/ duplicate/
The last command variable we’ll cover is -z. This command will compress the files that are being transferred over the network. As a bonus, let’s see how to make a transfer from the source server to another one. The command would look like this:
rsync -za ~/Desktop/Original email@example.com:~/tmp/
As mentioned before -z compresses the files, -a, or just adding a to -z, will ensure all permissions are copied as well.
~/Desktop/Original is the source. It’s a local directory – the one that is on the machine you’re logged into, and lastly firstname.lastname@example.org:~/tmp/ indicates the destination. email@example.com is the destination remote server address, while :~/tmp/ points to a specific folder on that machine.
We only covered the tip of the iceberg! Linux Rsync is an incredibly powerful utility that every server admin or Linux developer should know. We covered everything to get you started, from installation to the basic commands. If you want to get to know the more advanced features – check the official documentation!
Get ready to improve your productivity! Good luck with your project.