Rsync is a powerful Linux command that allows you to synchronize two directories. It is one of the cornerstones of many applications for backing up any Linux system. Today, with some examples, we will show you how this Linux command works.
rsync on Linux
Usually, you need to perform folder synchronizations on different computers. In Linux, this task is fully accomplished by the rsync command. Rsync is present in the official repositories of many Linux distributions like Debian, Ubuntu, CentOS, and others.
Also, the Linux rsync command has many different options that allow you to modify the way synchronizations are performed. These synchronizations are copies of directories or files, but they are done in an advanced way.
So, with some examples, we will explain how this command works.
Install rsync on Linux
rsync is usually installed on most popular Linux distributions. However, if for some reason it is not there you can install it very easily.
So, open a terminal and execute some of these commands according to the distribution you use.
For distributions like Debian, Ubuntu, and derivatives like Linux Mint, Pop!_OS or ElementaryOS:
:~$ sudo apt install rsync
In the case of CentOS or Fedora:
:~$ sudo dnf install rsync
So, for Arch Linux and derivatives:
:~$ sudo pacman -S rsync
And if you use OpenSUSE 15.1 or Tumbleweed, just run this command:
:~$ sudo zypper in rsync
In the end, you can check the installed version with the following command. I am using Debian 10 Buster and this is the screen output:
:~$ rsync – version rsync version 3.1.3 protocol version 31 Copyright (C) 1996-2018 by Andrew Tridgell, Wayne Davison, and others. Web site: http://rsync.samba.org/ Capabilities: 64-bit files, 64-bit inums, 64-bit timestamps, 64-bit long ints, socketpairs, hardlinks, symlinks, IPv6, batchfiles, inplace, append, ACLs, xattrs, iconv, symtimes, prealloc rsync comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions. See the GNU General Public Licence for details.
Using the Linux rsync command
Like almost every command in Linux, rsync has a basic syntax that we have to follow.
rsync [OPTION]... SRC [SRC]... DEST
In other words, command + options + source-folder + destination.
Next, with some examples, I will explain the use of this command.
Examples of how to use the rsync command
1.- Copy files or directories locally
To synchronize folders or files using rsync locally, simply run the following command:
:~$ rsync -avzh [file] [destination]
For example, if you wanted to back up a folder called
Files in another call
backups, it would be like this:
:~$ rsync -avzh /home/user/Files/ /home/user/backups/
In this case, the entire contents of the
Files folder will be copied to the
Backup folder. Even if the folder does not exist, rsync will create it.
Be careful with the / at the end of the source folder, if you execute this command:
:~$ rsync -avzh /home/user/Files /home/user/backups/
The result is that a folder called
Files will be created inside the
Backup folder and the contents will be there. Let us say that the directory structure at the destination will be
Now, the options I use for this command, help a lot to make the process optimal.
a: keeps the user, group, permissions, date, and time, as well as the symbolic links.
v: shows the result of the command execution.
z: This option, compresses the folder or file before making the synchronization or copy. Useful in very big files and folders.
h: Used to make the drives appear more readable.
This way it is quite simple, to make a backup of a folder locally.
2.- Remote synchronization using the Linux rsync command
In this section, we will synchronize with a remote computer. It is recommended to do this using SSH so that our files are transferred securely.
For this command, you have to add the option e which enables the use of an external shell which in this case is SSH.
:~$ rsync -avhze ssh [folder-or-file] [user]@[host]:[destination]
As we can see it is quite simple, in this case, I will synchronize the
Files folder to a host with IP address
192.168.1.23 in the
:~$ rsync -avhze ssh /home/user/Files [email protected]:/home/user1/Documents
And that is how simple it is to do. But what if SSH is not running on the port 22? Well, we can specify a specific port as follows:
:~$ rsync -avhze "ssh -p [port]" /home/user/Files [email protected]:/home/user1/Documents
3.- Delete the files in the source folder
In this case, the process is more like a move than a copy. However, it is also possible to do so. To do so, add the
:~$ rsync -avzh – remove-source-files /home/user/Files /home/user/backups/
It also works remotely:
:~$ rsync – remove-source-files -avhze ssh /home/user/Files [email protected]:/home/user1/Documents
Be careful with this option because you will delete whatever is in the source folder.
4.- Include or Exclude specific files
The Linux rsync command also allows you to exclude or include specific files during synchronization. This is done using the
A very useful example is to only synchronize files with an extension, for example, JPG. This can be done in the following way:
:~$ rsync -ahvz – include "*.html" – exclude "*" /home/user/Files /home/user/backups/
Also, these options can be used in a remote synchronization.
5.- Test the rsync command before synchronizing
With rsync, you can do quite delicate operations on servers. So it’s a good idea to test it before making any changes. Something like a preview.
For this, there is the
:~$ rsync -avzh – dry-run – remove-source-files /home/user/Files /home/user/backups/
This way you will know exactly what the command will do before making any changes, preventing errors.
Bonus: Some graphical applications for rsync
rsync is a command with a lot of options. And thanks to this, other developers have created programs that can handle it from a graphical interface.
These programs, help not only the novice but also the more experienced user that with a few clicks wants to use rsync.
The first one is LuckyBackup which is a wonderful application. It is quite complete and has a lot of options.
To download it, you can visit this link and choose the right package.
The second option is Grsync which provides from a GTK interface quite accomplished ease of using rsync. Maybe it is not as complete as Luckybackup but it is very complete and you will hardly miss any option.
To install it, just use the package manager of your distribution. For example, in Debian, Ubuntu, and derivatives:
:~$ sudo apt install grsync
With these two tools, you can take advantage of the rsync command and learn more about it.
The Linux rsync command is used to copy files in an advanced way. And even, this command is the basis for other graphic applications to facilitate the task of making backups.
This command is very powerful and fast and that is why it has become a tool that every sysadmin must know. In any case, in this post, these examples help to know, at least, in a basic way, how rsync works.