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.