Read in 5 minutes

last updated 

How to Use Rsync for Local and Remote Data Transfer and Synchronization

Rsync is a fast and versatile command line utility that synchronizes files and folders 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 locations.

Rsync can be used for mirroring data, incremental backups, copying files between systems and as a replacement for scp and cp commands for everyday use.

In this tutorial, we will show you how to use rsync through practical examples and detailed explanations of the most common rsync options.

Installing Rsync

The rsync utility is pre-installed on most Linux distributions and on Mac OSX. You can check if it is installed on your system by typing:

rsync --version
rsync  version 3.1.2  protocol version 31

If you don’t have rsync installed on your system, you can easily install it using your distribution’s package manager.

Install Rsync on Ubuntu and Debian

sudo apt install rsync

Install Rsync on CentOS and Fedora

sudo yum install rsync

Rsync Command Syntax

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

The rsync utility expressions take the following form:

Local to Local:  rsync [OPTION]... SRC [SRC]... DEST
Local to Remote: rsync [OPTION]... SRC [SRC]... [[email protected]]HOST:DEST
Remote to Local: rsync [OPTION]... [[email protected]]HOST:SRC [DEST]
  • OPTION - The rsync options
  • SRC - Source directory
  • DEST - Destination directory
  • USER - Remote username
  • HOST - Remote host

Rsync provides a number of options that control every aspect of its behavior. The most widely used options are:

  • -a, --archive archive mode, equivalent to -rlptgoD. The commonly used option which will syncs directories recursively, transfer special and block devices, preserve symbolic links, modification times, group, ownership, and permissions.
  • -z, --compress. This option will force 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.
  • -P, equivalent to --partial --progress. This option will tell rsync to show a progress bar during transfer and to keep the partially transferred files. It is useful when transferring large files over slow or unstable network connections.
  • --delete When using this option rsync will delete extraneous files from destination location. It is useful for mirroring.
  • -q, --quiet. Use this option if you want to suppress non-error messages.
  • -e. This option allows you to choose a different remote shell. By default Rsync is configured to use ssh.

Basic Rsync Usage

To copy a single file from one to another local location you should run the following command:

rsync -a /opt/ /tmp/

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 you need to specify the new name:

rsync -a /opt/ /tmp/

In the example bellow we are creating a local backup of our website files:

rsync -a /var/www/ /var/www/

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 you add a trailing slash on the source directory it will copy only the contents of the directory to the destination directory. When the trailing slash is omitted rsync will copy the source directory inside the destination directory.

How To Use Rsync to Sync Data from/to a remote Machine

When using rsync for remote transfer, 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/ [email protected]_host_or_ip:/opt/media/

If you haven’t set a passwordless SSH login to the remote machine, you will be asked to enter the user password.

If you want to transfer data from a remote to a local machine then you need to use the remote location as a source:

rsync -a [email protected]_host_or_ip:/opt/media/ /opt/media/

If SSH on the remote host is listening on a port other than the default 22 then you can specify the port using the -e argument:

rsync -a -e "ssh -p 2322" /opt/media/ [email protected]_host_or_ip:/opt/media/

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

Exclude Files and Directories

When excluding files or directories you need to use their relative paths. There are two options to exclude files and directories when using rsync.

The first option is to use the --exclude argument and specify the files and directories you want to exclude on the command line. In the following example we are excluding the node_modules and tmp directories which is located inside the src_directory

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

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

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


In this tutorial, you learned how to use Rsync to copy and synchronize files and directories. There’s lots more to learn about Rsync at Rsync User’s Manual page.