how to use rsync in linux

How To Use rsync Command in Linux

rsync linux command allows you to synchronize files and directories across two location on same or different machines. It provides a very fast way to transfer files by only sending incremental data across two locations. In this article, we will look at how to use rsync command in linux. It is useful for incremental backups, data mirroring, file copying and transfers. it can also be used as an alternative to cp, scp and sftp commands.

How To Use rsync Command in Linux

Here is how to use rsync command in linux.

How to Install rsync

Although rsync is pre-installed in most linux systems, here are the commands to install rsync in linux.

Install rsync on Ubuntu/Debian

$ sudo apt install rsync

Install rsync on CentOS/Fedora

# sudo yum install rsync

Also read : How to Enable Password-based authentication in SSH

Rsync Command Syntax

Here is the command syntax for rsync across various location

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

In the above commands,

SRC - Source directory
DEST - Destination directory
USER - Remote username
HOST - Remote hostname or IP Address

rsync provides many options to perform various kinds of file sync. We will look at some basic example of how to use rsync.

Also read : How to Enable multi-factor authentication in SSH

How to Use rsync to Copy Files

The most common use of rsync is to sync files and directories. -a is most commonly used option for this purpose. It tells rsync to sync directories recursively, transfer block and special devices, preserve symbolic links, modification times, owner groups, file ownership, and user permissions.

Here is an example to copy a file to another location.

$ rsync -a /home/file.pdf /user/

In the above case, rsync will create a copy of file.pdf at destination /user/.

If you specify a new file name in destination, rsync will copy and rename the file

$ rsync -a /home/file.pdf /user/new_file.pdf

Here is an example to sync two directories.

$ rsync -a /home/test_folder/  /user/

If the destination folder does not exist, rsync will create it. Also, if you use a trailing slash (/) at the end of source folder, then rsync will copy all the contents of source folder to destination folder. If you don’t mention the trailing slash, then it will copy the entire source folder inside the destination folder.

Also read : How to Enable IPv6 in Linux

How to Use rsync Between Two Servers

If you want to sync files & directories with a remote machine, then rsync should be installed on both local and remote machine. Please note, rsync uses SSH as default shell so it communicates via port 22.

Here is the command to transfer files & directories from local to remote machine.

$ rsync -a /home/test/ remote_user@remote_host_or_ip:/user/

You need to specify source directory on local machine, remote user, remote host and destination directory on remote machine.

If you have not setup passwordless SSH login, then you will be prompted for a password. It is also important that you have write permission at destination folder for the above command to work.

Here is the command to transfer files & directories from remote server to local machine.

$ rsync -a remote_user@remote_host_or_ip:/user/ /home/test/

By default, SSH works on port 22. If you set up SSH on another port (e.g 23), then you can specify it in rsync using -e option as shown below.

$ rsync -a -e "ssh -p 2322" /home/test/ remote_user@remote_host_or_ip:/user/

Leave a Reply

Your email address will not be published.