How to use Rsync to synchronize local and remote directories?

Aggiornato il 2026-10-23 02:01:23 16 visite

Has it ever happened to you that you need to transfer files quickly and securely between different servers or you have lost an important file? If the answer is yes, Rsync is the perfect tool for you. Rsync is an open-source file and directory synchronization tool that allows you to transfer files quickly and securely between local and remote servers. It is a powerful and very versatile tool that can help you keep your files updated and secure.

In this tutorial, you will learn how to use Rsync to synchronize files and directories between different servers and how to schedule automatic synchronization tasks.

To start this tutorial, you will need to have access to two servers: one local and one remote. Additionally, you will need to have Rsync installed on both servers. If you do not have Rsync installed, do not worry, we will show you how to do it in the system preparation section.

Rsync basic concepts

Rsync is a file and directory synchronization tool for transferring files quickly and securely between local and remote servers. It is a very popular tool and widely used in the world of computing and programming.

Here we explain some basic concepts of Rsync that you should know before you start using it:

1. Synchronization: Rsync uses the delta file synchronization algorithm to transfer only the parts of the files that have been modified. This significantly reduces the time required for file transfer.

2. Local vs. remote copy: In local synchronization, Rsync is used to synchronize files between two directories on the same server. In remote synchronization, Rsync is used to synchronize files between two different servers.

3. Secure connection: Rsync uses SSH to establish a secure connection between local and remote servers. SSH is a secure network protocol used for communication between two devices.

Now that you know the basic concepts of Rsync, let's move on to the next section to prepare the system and be able to use this tool.

System preparation

Before you start using Rsync, it is necessary to ensure that it is installed on both servers and that all necessary requirements are met.

Step 1: Rsync Installation

If Rsync is not installed on your servers, you can install it using the package manager of your operating system.

On Debian or Ubuntu based systems, you can install Rsync with the following command:

sudo apt-get install rsync

On CentOS or Fedora based systems, you can install Rsync with the following command:

sudo yum install rsync

Step 2: SSH Access

Rsync uses SSH to establish a secure connection between servers. Therefore, it is necessary to ensure that you have SSH access on both.

Step 3: Version verification

It is important to verify that the version of Rsync installed on both servers is the same to avoid compatibility problems. You can verify the version of Rsync using the following command:

rsync --version

Now that we have prepared the system and installed Rsync, we can move to the next section to learn how to synchronize files and directories with Rsync.

Basic syntax

Basic syntax in local environment

The basic syntax for rsync in local environments is as follows:

rsync [optional modifiers] [source] [destination]

Basic syntax on remote server

The basic syntax for rsync between a local environment and a remote environment is as follows:

rsync [optional modifiers] [source] [user]@[host]:[destination]

And vice versa:

rsync [optional modifiers] [user]@[host]:[source] [destination]

Synchronization of files and directories with Rsync

In this first example, we create the origen and destino directories. Subsequently, we will create 5 files in the source directory and see the magic of rsync.

1. First we create the two directories:

cd ~
mkdir origen
mkdir destino

2. We create 5 example files in the source directory:

touch original/archivo{1..5}

3. Next, we will synchronize the content of the origen directory with destino using rsync:

rsync origen/* destino

The * indicates that all the content of the directory should be synchronized

In the event that files were added or modified in the origen directory and the rsync command was executed again, only the new and modified files would be synchronized, thus avoiding copying unnecessary files.

Most used optional modifiers

Below are some of the optional modifiers that can be added to the use of rsync:

  • -a or --archive this option activates: -rlptgoD, which means:
    • -r recursive
    • -l copy symbolic links as symbolic links.
    • -p preserve permissions.
    • -t preserve the modification / creation date of the file.
    • -g preserve the group of the files.
    • -o preserve the owner of the files.
    • -D preserve device files and preserve special files.
  • -v or --verbose to show process information.
  • -e or --rsh is used to specify a remote shell, for example, if you want to perform rsync between servers.
  • --delete deletes files in the destination directory if they do not exist in the source directory.
  • -z or --compress compresses data during transfer.

Usage examples with modifiers

In this first example, we will synchronize the content of the /var/log folder of server 10.0.0.11 to the /tmp folder of our server using the SSH shell.

rsync -avz -e 'ssh' [email protected]:/var/log /var/log /tmp/log

Next, we will carry out the same operation, but in this case with the --delete tag, so that files in the destination that do not exist in the source are deleted:

rsync -avz --delete -e 'ssh' [email protected]:/var/log /var/log /tmp/log

Local synchronization

To synchronize two directories on the same server, you can use the following command:

rsync -avzh /ruta/directorio-origen/ /ruta/directorio-destino/

This command synchronizes the content of the source-directory with the destination-directory. The -avzh option indicates that Rsync should synchronize files in archive mode, in verbose mode, in compression mode, and in recursive mode.

Remote synchronization

To synchronize two different servers, you can use the following command:

rsync -avzh -e ssh usuario@servidor-origen:/ruta/directorio-origen/ /ruta/directorio-destino/

This command synchronizes the content of the source-directory on the source-server with the destination-directory on the local server. The -e ssh option indicates that Rsync must use SSH to establish a secure connection between the servers.

Synchronization in test mode

If you want to verify which files will be synchronized before actually doing it, you can use dry run mode. Dry run mode performs a simulation of the synchronization without performing any real action. You can use the following command:

rsync -avzh --dry-run /ruta/directorio-origen/ /ruta/directorio-destino/

info This command shows a list of the files that will be synchronized if the real synchronization is executed.

Synchronization of files excluding some

If you wish to synchronize all files in a directory, except for some specific files, you can use the --exclude option. You can use the following command:

rsync -avzh --exclude 'archivo1.txt' --exclude 'archivo2.txt' /ruta/directorio-origen/ /ruta/directorio-destino/

This command synchronizes all files in the source-directory, except archivo1.txt and archivo2.txt.

Conclusions

Rsync is a very useful tool for synchronizing files and directories between local and remote servers. With the basic commands we have shown in this tutorial, you can start using Rsync effectively.

Additionally, it contains many very interesting advanced tools. Some of the advanced features include file compression, incremental synchronization, exclusion of specific files, and many more.

Finally, we hope that this tutorial has been useful to you and that you start using Rsync to synchronize your files and directories!