Complete Guide to Install rsync Command in Ubuntu, Debian, CentOS, Fedora

Install rsync Command in Ubuntu, Debian, CentOS, Fedora

The rsync command, short for Remote Sync, is one of the most widely used Linux commands that is used to copy and synchronize files and directories remotely as well as locally in Linux systems. The versatile rsync command provides the possibility of transferring and synchronizing data between the local machine and the remote host or remote shell and saves the user’s time and energy for transferring files, synchronizing folders, incremental backups, and copying files between systems. The rsync command improves productivity with its valuable features in network synchronization and file transfer and copying.

Fortunately, the rsync synchronization tool is supported by default by most Linux distributions due to its widespread use and popularity as a system scripting tool. Therefore, to use the rsync command to synchronize files between the remote system and the local system, you need two systems, according to your needs, these two systems can be virtual servers, virtual servers and local systems, or other combinations.

Since the use of a virtual server has become very popular today, by purchasing a Linux VPS and installing the rsync utility, you can use the helpful features of this tool. In this article, we will teach you how to install the rsync command in Linux for file transfer and synchronization, and we will guide you in detail on how to use the rsync command and its options. Stay with us until the end of the article to learn how to use the rsync command.

Features of the rsync command

  • Flexible network and directory synchronization tool
  • Support links, devices, owners, groups, and permissions
  • The most efficient and fastest way to copy and synchronize files and directories from/to the remote system
  • Minimum bandwidth consumption due to the use of the algorithm to minimize the amount of copied data by moving the changed parts of the file and compressing the files at the source and destination while sending and receiving data.
  • It is faster than the SCP (Secure Copy) command, because by using the remote-update protocol, after transferring the complete content of the file from the source to the destination, the next time it transfers only the differences, blocks, and changed bytes to the destination.
  • optimally performs the transfer operation, and when the connection is interrupted, the transfer and copying of files continue automatically after reconnecting from where it was stopped.
  • Ideal replacement for scp, sftp, and cp commands

Installing the rsync command on Linux

As we mentioned, this tool is installed by default on most Linux distributions. But if the Linux distribution you are using does not support the Rsync tool by default, you can install it through the package manager of your distribution.

Install the Rsync tool on Debian and Ubuntu distributions and their derivatives by running the following command:

sudo apt install rsync

To install the Rsync tool on CentOS and Fedora distributions and their derivatives, run the following command:

sudo yum install rsync

Now you can use Rsync tool features to transfer, synchronize and copy files. In addition to using the Rsync tool, the Syncthing application can also be effective and useful in peer-to-peer file synchronization, we recommend that you do not neglect using the Syncthing program. We have previously taught how to install Syncthing on Ubuntu so that those interested in using Syncthing can achieve what they want by following the instructions provided in the guide.

Checking the version of the rsync command

If you don’t know if the Rsync tool is installed by default on the Linux you are using or not, running the following command to check the version of rsync will guide you in this regard:

rsync -version

Output:

rsync  version 3.1.3  protocol version 31

By checking the version of the Rsync tool, you will be sure that the Linux operating system you are using supports the Rsync command line tool by default. Also, the previous command assures users who have recently installed the Rsync command line tool that their installation process is successful.

rsync command Syntax

The main operation and syntax of the rsync command are the same as the ssh, scp, and cp commands. If you are familiar with the operation of these commands, it is not difficult to understand how to use the rsync command. The basic syntax of the rsync command is as follows:

Local to Local:  rsync [OPTION]... [SRC]... DEST
Local to Remote: rsync [OPTION]... [SRC]... [USER@]HOST:DEST
Remote to Local: rsync [OPTION]... [USER@]HOST:SRC... [DEST]

As you can see, running the rsync command is different when you use a remote shell such as SSH. For a better understanding of Rsync Syntax, pay attention to the following notes:

OPTION: rsync command options to control rsync command behavior

SRC: source directory

DEST: destination directory

USER: remote user name

HOST: Hostname or remote IP address

Since we talked about rsync command options, we will introduce you to the most used Rsync command options.

OptionsDescription
-a, --archiveThis option is used to activate archive mode, transfer specific devices, block devices, recursive synchronization of directories, and maintain groups, ownership, and symbolic links.
-P, --partial --progressThis option runs to view the progress bar while transferring files.
-z, --compressThis option is used to compress data when transferring to the destination device.
-q, --quietare executed to suppress non-error messages.
--deleteBy running this option, the rsync command will delete additional files from the destination.
-eto select a different remote shell from the default remote shell rsync(ssh) is executed.
-rused to copy data recursively.
-v, --verboseProvides visual output to view the progress of the process.
-h, --human-readable formatprovides human-readable format.

How to use the rsync command

First, we are going to familiarize you more with the rsync command by using examples so that you understand the role of the rsync command in improving performance.

Enter the following command to make changes to the home directory of your Linux system:

cd ~

And create two test directories by running the following commands:

mkdir test1
mkdir test2

Then add several files to the directories you created:

touch test1/file{1..50}

You have created the test1 directory and added 50 empty files to it.

To verify the changes you made, you can run thelscommand to get a list of the files in the directory:

ls test1

Synchronizing files in the local system Using rsync

In our example, in addition to test1, there is also a test2 directory on your system, which does not contain any files. The rsync command will help you to synchronize and copy files between the test1 and test2 directories, which is one of the basic uses of this command.

Of course, note that when using this command, you must have the privilege of reading at the source and writing at the destination. To synchronize the files of the directory test1 to test2, you can run the rsync command with the-rflag:

rsync -r test1/ test2

The-r(--recursive) option forces the rsync command to copy all existing files and subdirectories in the source directory to the destination directory on the local system.

If you want to synchronize the contents of directories recursively and preserve specific files, devices, and symbolic links, replace-awith the-rflag in the previous command:

rsync -a test1/ test2

Note: Don’t forget to use the trailing slash (/) because the trailing slash (/) indicates the contents of the test1 directory, which without (/) the test1 directory along with its contents will be moved to test2. That is, the trailing slash (/) at the end of the source directory causes the contents of the source directory to be copied to the destination directory, but executing the previous command without the trailing slash (/) copies the source directory to the destination directory.

To copy and synchronize all files except subdirectories between two directories, you can also use the following command:

rsync test1/* test2/

If you add new files to the test1 directory when synchronizing, by running the previous command, only the new files will be copied to the destination, and network bandwidth will be saved.

If you want to move files that are not in the source directory but are in the destination directory to the source directory, the following command is helpful:

rsync -r test1 test2/

By running this command, the contents of the source and destination directories will be the same.

Synchronizing via rsync command with remote system

To use the rsync command to synchronize and transfer data between the local system and the remote system, you must make sure that the Rsync tool is installed on both systems so that you can meet your needs by running rsync command.

It is worth mentioning that the default remote shell of the Rsync tool is SSH. So after confirming SSH access to the remote system, you can synchronize the directory from the local system, for example, the test1 directory that we created in this tutorial, with the remote system by the following command:

rsync -a ~/test1 username@remote_host:destination_directory

Since we intended to transfer the actual directory to the remote system, we did not use the trailing slash at the end of the directory.

The process of transferring data from the local system to the remote system is called pushes, and vice versa, if the data is transferred from the remote system to the local system, it is called pull.

If you want to move the directory from the remote system to the local system, you must enter the following command:

rsync -a username@remote_host:/home/username/test1 place_to_sync_on_local_machine

In rsync commands, we put the resource specification first and the local system specification is entered in the second argument.

If the SSH listening port is customized on the remote system and is not the default ssh port, specify the SSH listening port using the-ecommand:

rsync -a -e "ssh -p 2322" test1 remote_user@remote_host_or_ip: destination_directory

How to combine rsync command options

The-a (--archive) option is one of the most used options of the rsync command, which allows combining the rsync command with other options to change the default behavior of the rsync command. Therefore, with the help of the combination of rsync command options, you can copy permissions and modification times and other options in addition to transferring files.

-a option with -v is a common combination to view copied files that have not been modified:

rsync -av --dry-run  test1/ test2/

By running the previous command, you will access a list of copied files. Using the-dry-run(-n) option along with the rsync command causes the rsync command to run without making any changes.

Compressing files for transfer using the rsync command

If you are copying files and data between two systems in a network with limited bandwidth, to reduce bandwidth consumption and speed up the transfer process, you can compress files that have not been compressed before  for transfer using the-zoption:

rsync -az source destination

Adding Progress Bar when transferring data

One of the most used options is the-p, which by combining the--progressand--partialoptions, in addition to displaying the progress bar during data transfer, if the transfer is stopped for any reason, using the--partialflag will automatically resume the transfer process.  For this purpose, you can use the following syntax:

rsync -azP source destination

Output:

sending incremental file list
created directory destination
source/
source/file1
0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=99/101)
sourcefile10
0 100%    0.00kB/s    0:00:00 (xfr#2, to-chk=98/101)
source/file100
0 100%    0.00kB/s    0:00:00 (xfr#3, to-chk=97/101)
source/file11
0 100%    0.00kB/s    0:00:00 (xfr#4, to-chk=96/101)
source/file12
0 100%    0.00kB/s    0:00:00 (xfr#5, to-chk=95/101)
. . .

By running the previous command again, if no changes have been made to the files and directories, you will get a concise output. The output by running the rsync -azP source destination command will be as follows:

Output:

sending incremental file list
sent 818 bytes received 12 bytes 1660.00 bytes/sec
total size is 0 speedup is 0.00

As you can see, the output does not show any changes. The Rsync command recognizes the changes made by checking the modification times and helps you by providing the output to inform you of the changes in files and directories.

Transferring specific files using the rsync command

If you make changes in the files and you intend to transfer only changed files to the destination directory, the rsync command responds to your needs with its capabilities and features.

For example, update the modification time of some files, which is possible through the following command:

touch test1/file{1..10}

By running the following command, you will notice that the rsync command has transferred only the changed files to the destination:

rsync -azP source destination

Output:

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

Deleting files and directories

By default, the rsync command does not remove files from the destination directory. But to synchronize the source and destination directory, if a file is deleted from the source, it must be deleted from the destination, but how?

Combining the rsync command with the--deleteflag will get you to your goal:

rsync -an --delete source destination

Note: Before running the previous command, we recommend using the -n or -dry-run option along with the rsync command to run the rsync command as a test first and avoid losing your data.

In addition to the--deleteoption, the--excludeargument is also used to delete files and directories. By using the--excludeoption and specifying files and directories, you can exclude specific files and directories during synchronization in the terminal:

rsync -a --exclude=pattern_to_exclude source destination

If you specify a specific pattern to exclude files, you can exclude files that match another pattern using the--includeoption:

rsync -a --exclude=pattern_to_exclude --include=pattern_to_include source destination

Creating a rsync backup

By combining the--backupand--dir commands, you can make a backup copy of rsync and specify the storage location of the backup files:

rsync -a --delete --backup --backup-dir=/path/to/backups /path/to/source destination

FAQ

One of the main drawbacks of the rsync tool is the inability to manage and control the destination directory or the remote source. It must be available in the local system (local disk, nfs/cifs/other mountpoint)

Yes, the rsync command features support automatic synchronization of local files with remote server files.

Running rsync commands requires a Linux VPS and ssh client because these commands are executed through the shell.

Conclusion

The Linux Rsync tool is one of the most widely used and powerful Linux commands that simplifies and optimizes file transfer between systems in network connections. it is an ideal option to respond to the needs of users in synchronizing files and directories and other file-related operations.

Knowing how to use the Rsync tool is essential for server administrators and developers because by using the advanced features of the rsync command, you can effectively manage file and directory transfer operations and improve productivity. In this tutorial, we explained how to install and use Rsync and introduced you to everything helpful related to file transfer.

If you face any problem when using the rsync command for different purposes, ask us your question in the comments section so that we can guide you further.

Related Article : What is Debian OS

Leave a Reply

Your email address will not be published. Required fields are marked.