Guides and tutorials

Hundreds of tutorials and step by step guides carefully written by our support team.

Synchronizing local and remote directories with rsync

In this guide we will explain how to use rsync to improve productivity on Linux based systems.

What is rsync?

Rsync is a utility that allows you to efficiently transfer and synchronize files or directories between a local machine or remote server.

Basic syntax

Basic syntax in local environment

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

rsync [optional modifiers] [source] [destination] [destination] [source] [destination] [source] [destination

Basic syntax on remote server

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

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

And vice versa:

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

Install rsync

Many Linux distributions include the rsync package preinstalled. In case it is not preinstalled you can do it manually with the following command:

  • Debian-based distributions (Ubuntu, etc):
apt-get install rsync
  • RPM based distributions (Fedora, CentOS, etc):
yum install rsync
  • MacOS:
bew install rsync

It's that easy! To verify that it has been installed you can get the version with the following command:

rsync -version

Examples of use

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

  1. First we create the two directories:
cd ~
mkdir source
mkdir destination
  1. We create 5 example files in the source directory:
touch original/file{1..5}
  1. Next we will synchronize the contents of the source directory with destination by using rsync:
rsync source/*destination

*The * indicates to synchronize the entire contents of the directory.

If files are added or modified in the source directory and the rsync command is run again, only the new and modified files will be synchronized, thus avoiding copying unnecessary files.

Most commonly used optional modifiers

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

  • -a or --archive this option enables us: -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 file group.
    • o` preserve the owner of the files.
    • D` preserve device files and preserve special files.
  • -v or --verbose to display process information.
  • -e or --rsh is used to specify a remote shell, for example, if you want to rsync between servers.
  • --delete deletes files in the destination directory if they do not exist in the source directory.
  • -z or --compress compresses the data during the transfer.

Examples of use with modifiers

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

rsync -avz -e 'ssh' test@10.0.0.11:/var/log /var/log /tmp/log

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

rsync -avz --delete -e 'ssh' test@10.0.0.11:/var/log /var/log /tmp/log

Conclusions

In conclusion with rsync you can transmit file transfers over network connections or local directories. Its flexibility makes it a very good choice for many file-level operations.

A good command of rsync allows you to design complex backup operations and gain control over how and what is transferred.

More than 2000 m² of own facilities and Data Centers in Spain
Your privacy is important for us
We use our own cookies for the proper functioning of the site. In addition, third-party cookies are used for analytical purposes only. This information is not associated with any person so that personal identifying data is not stored, but is only information that is collected to identify the session, with the aim of facilitating the analysis of the website. You can change your preferences at any time by entering this website again. For more information about our cookie policy you can visit our Cookies. You can press the "Accept and close" button to give us your consent or you can access more detailed information and manage cookies.
More than 2000 m² of own facilities and Data Centers in Spain
Your privacy is important for us
We use our own cookies for the proper functioning of the site. In addition, third-party cookies are used for analytical purposes only. This information is not associated with any person so that personal identifying data is not stored, but is only information that is collected to identify the session, with the aim of facilitating the analysis of the website. You can change your preferences at any time by entering this website again. For more information about our cookie policy you can visit our Cookies. You can press the "Accept and close" button to give us your consent or you can access more detailed information and manage cookies.