The Linux rsync command transfers and synchronizes files or directories efficiently between a local machine, another host, remote shell, or any combination of these. Anyone working with Linux based systems should use this powerful command 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 re-established.

Basic Syntax

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.

Basic Syntax for Remote Shell

When using a remote shell, such as SSH or RSH, the rsync syntax will be slightly different.

To access the remote shell (PULL) use the rsync command:

rsync [optional modifiers] [USER@]HOST:SRC [DEST]

To access the remote shell (PUSH) use the rsync command:

rsync [optional modifiers] SRC [USER@]HOST:[DEST]

How to Check the Rsync Version

Before we check for rsync, we need to log into the VPS 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,  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:

rsync -version

On our Ubuntu distribution the command produced the following output:

rsync  version 3.1.3  protocol version 31

That means rsync version 3.1.3 is already on our machine. Simple, right?

How to Install Rsync

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 command:

apt-get install rsync

On rpm-based distributions like Fedora and CentOS use following command:

yum install rsync

On MacOS use following command:

brew 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:

rsync -version

How to Use Rsync Commands

For this tutorial, we will create two directories on our Linux desktop with the names original and duplicate. The original directory has three files in it while the duplicate is empty. Now let’s see how rsync will create new ways to increase productivity.

To create two test directories use the following commands:

cd ~
mkdir original
mkdir duplicate
touch original/file{1..3}

If you want to double-check, use the ls command to list all the files inside the directory:

ls original

The output will look similar to this:

file1
file2
file3

If you use the ls command with the directory duplicate, the outlook should be empty.

Now that the directories are set up, let’s try out a few more commands.

The following command will copy or sync all files in the original directory into 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.

Rsync Most Common Commands

Here is a list of the most common commands used with rsync:

This enables archive mode.

-a, --archive

This gives you a visual output that shows the progress of the process.

-v, --verbose

This outputs in a human-readable format.

-h, --human-readable format

This compresses the file data during the transfer.

-z, --compress

This is to copy data recursively

-r

How to Use Rsync Commands with Subdirectories

Keep in mind that the above command will only copy files from the original folder’s main directory, and not any of the subdirectories.

If you want to copy the subdirectories as well, you will need to use this command:

rsync -r original/ duplicate/

The -r (–recursive) 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.

How to Synchronize Files

If you want to synchronize files, meaning copying files that are 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 all the same files.

How to Combine Rsync Commands

Another useful option is -a (–archive) since it can be joined with many other commands. That means it won’t just copy the files, it will also copy the permissions, modification times, and any other date.

Adding the -a option with -v 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 making 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 (or -n) makes rsync perform a trial run that doesn’t make any changes.

Other Options For Rysnc Commands

When adding the  -a option to the -v option, making -av in our command,  increases verbosity. Here is what it should look like:

rsync -av original/ duplicate/

If you want to sync two folders and delete the items in the duplicate folder, that don’t exist in the original folder, add the  –delete option, like this:

rsync -av --delete  original/ duplicate/

You can also exclude specific files or sub-directories when you are syncing two folders. Do this by adding -exclude=. If you need to specify more than one file, separate them by a comma.

rsync -av --exclude=file1,file2  original/ duplicate/

You can also include specific files or sub-directories when you are syncing. Do this by adding -include=. You can also use this with the -exclude= option. The example below will include files beginning with the letter L and exclude all the other files:

rsync -av --include=L* --exclude=*  original/ duplicate/

Using Rsync, you can also specify the file size that can be synced. To do this, use the –max-size option:

rsync -av --max-size=10k original/ duplicate/

Using the option -z (–compress) will consolidate 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 -az ~/Desktop/Original edward@192.168.22.90:~/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, edward@192.168.22.90:~/tmp/ indicates the destination. edward@192.168.22.90 is the destination remote server address, while :~/tmp/ points to a specific folder on that machine.

How to Add a Progress Bar

In addition to the above commands, you can add -P which will combine the options –progress and –partial. This will give you a progress bar for the files being transferred and also allows you to resume any interrupted file transfers.

rsync -azP [SRC] [DEST]

The output will look similar to this:

sending incremental file list
./
file1
         0 100%    0.00kB/s   0:00:00   (xfer#1, to-check=1/3)
file2
         0 100%    0.00kB/s   0:00:00   (xfer#2, to-check=2/3)
file3
         0 100%    0.00kB/s   0:00:00   (xfer#3, to-check=3/3)

Then, if we run the command again, we will see a shorter output. This is because no new changes have been made. The output will look similar to this:

sending incremental file list
send 26 bytes received 5 bytes 1200.00 bytes/sec
total size is 0 speedup is 0.00

If you only want to transfer certain files, you can specify with the following command:

touch original/file{1..5}
rsync -azP [SRC] [DEST]

This will give a similar output to the previous command, but only with the files specified between the curly brackets.

How to create a Rsync Backup

Another important command is to create a Rsync backup. Do this by combining –backup with the –dir command so you can specify where the backup files will be stored.

rsync -a --delete --backup --backup-dir=/path/to/backup /path/to/SRC [DEST]

Conclusion

We only covered the tip of the iceberg when it comes to Rsync commands. 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.