Rsync Command in Linux with Examples easy to learn – Basic guide

'rsync' is a command-line utility used for data synchronization and file transfer between two systems. It is commonly found in Unix and Unix-like operating systems but can also be used on Windows / Linux through various implementations.

The primary purpose of ‘rsync‘ is to efficiently copy and synchronize files and directories from one location to another while minimizing data transfer.

rsync‘ is a fast and versatile command-line utility for synchronizing files and directories between two locations over a remote shell, or from/to a remote Rsync daemon. It provides fast incremental file transfer by transferring only the differences between the source and the destination.

There are basically two ways in which rsync can copy/sync data:

  • Copying/syncing to/from another host over any remote shell like sshrsh.
  • Copying/Syncing through rsync daemon using TCP.

rsync‘ is a versatile tool used for backups, mirroring, and efficiently updating files across different systems. Rsync can be used for mirroring data, incremental backups, copying files between systems, and as a replacement for scp , sftp, and cp commands.

Installing Rsync

The rsync utility is pre-installed on most Linux distributions. If you don’t have rsync installed on your system, you can easily install it using your distribution’s package manager.

To install rsync, you need to use the package manager specific to your operating system. Here are instructions for some common operating systems:

Linux (Debian/Ubuntu):

sudo apt-get update

sudo apt-get install rsync

Linux (CentOS and Fedora):

sudo yum install rsync

Make sure to adapt these commands based on the specific package manager used by your operating system. Once installed, you can use rsync from the command line to synchronize files and directories between different locations.

Rsync Command Syntax

Before going into how to use the rsync command, let’s start by reviewing the basic syntax.

rsync

The rsync utility expressions take the following form:

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]
  • OPTION – The rsync options .
  • SRC – Source directory.
  • DEST – Destination directory.
  • USER – Remote username.
  • HOST – Remote hostname or IP Address.

rsync provides a number of options that control how the command behaves. The most widely used options are:

  1. -a--archive, archive mode, equivalent to -rlptgoD. This option tells rsync to syncs directories recursively, transfer special and block devices, preserve symbolic links, modification times, groups, ownership, and permissions.
  2. -z--compress. This option forces rsync to compresses the data as it is sent to the destination machine. Use this option only if the connection to the remote machine is slow.
  3. -P, equivalent to --partial --progress. When this option is used, rsync shows a progress bar during the transfer and keeps the partially transferred files. It is useful when transferring large files over slow or unstable network connections.
  4. --delete. When this option is used, rsync deletes extraneous files from the destination location. It is useful for mirroring.
  5. -q--quiet. Use this option if you want to suppress non-error messages.
  6. -e. This option allows you to choose a different remote shell. By default, rsync is configured to use ssh.

Basic Rsync Usage

The most basic use case of rsync is to copy a single file from one to another local location. Here is an example:

rsync -a /opt/filename.zip /tmp/

The user running the command must have read permissions on the source location and write permissions on the destination.

Omitting the filename from the destination location copies the file with the current name. If you want to save the file under a different name, specify the new name on the destination part:

rsync -a /opt/filename.zip /tmp/newfilename.zip

The real power of rsync comes when synchronizing directories. The example below shows how to create a local backup of website files:

rsync -a /var/www/domain.com/public_html/ /var/www/domain.com/public_html_backup/

If the destination directory doesn’t exist, rsync will create it.

It is worth mentioning that rsync gives different treatment to the source directories with a trailing slash (/). If the source directory has a trailing slash, the command will copy only the directory contents to the destination directory. When the trailing slash is omitted, rsync copies the source directory inside the destination directory.

Using rsync to Sync Data from/to a remote Machine

When using rsync to transfer data remotely, it must be installed on both the source and the destination machine. The new versions of rsync are configured to use SSH as default remote shell.

In the following example, we are transferring a directory from a local to a remote machine:

rsync -a /opt/media/ remote_user@remote_host_or_ip:/opt/media/

To transfer data from a remote to a local machine, use the remote location as a source:

rsync -a remote_user@remote_host_or_ip:/opt/media/ /opt/media/

If SSH on the remote host is listening on a port other than the default 22 , specify the port using the -e option:

rsync -a -e "ssh -p 2322" /opt/media/ remote_user@remote_host_or_ip:/opt/media/

When transferring large amounts of data it is recommended to run the rsync command inside a screen session or to use the -P option:

rsync -a -P remote_user@remote_host_or_ip:/opt/media/ /opt/media/

Exclude Files and Directories

There are two options to exclude files and directories. The first option is to use the --exclude argument and specify the files and directories you want to exclude on the command line.

When excluding files or directories, you need to use their relative paths to the source location.

In the following example shows how exclude the node_modules and tmp directories:

rsync -a --exclude=node_modules --exclude=tmp /src_directory/ /dst_directory/

The second option is to use the --exclude-from option and specify the files and directories you want to exclude in a file.

rsync -a --exclude-from='/exclude-file.txt' /src_directory/ /dst_directory/

/exclude-file.txt

node_modules
tmp

Conclusion

We have shown you how to use Rsync to copy and synchronize files and directories. There’s lots more to learn about Rsync. Also check out on our other blog on How To Reset Root Password On CentOS 7 to reset password on Cent OS using commands.