Rsync (Remote Sync) – Rsync Command Examples and Usage

Rsync stands for remote synchronization.

Rsync is a fast and extraordinarily versatile file copying tool.

It is used for fast and incremental file transfer. Rsync is widely used for backups and mirroring purpose.

rsync is typically used to synchronize files and directories between two different systems or local directory.

It uses “delta-transfer algorithm”, which reduces the amount of data sent over the network by transferring only changed files to destination that has changed since the last backup.

It offers a wide range of options that control every aspect of its behavior and permit very flexible specification of the set of files to be copied.

Rsync finds files that need to be transferred using a “quick check” algorithm (by default) that looks for files that have changed in size or in last-modified time.

How to Install rsync on Linux

The rsync command isn’t installed by default on a Linux system, it’s available in the distribution repository, so use the following commands to install it.

For Fedora system, use DNF Command to install rsync.

$ sudo dnf install rsync

For Debian/Ubuntu systems, use APT-GET Command or APT Command to install rsync.

$ sudo apt install rsync

For Arch Linux based systems, use Pacman Command to install rsync.

$ sudo pacman -S rsync

For RHEL/CentOS systems, use YUM Command to install rsync.

$ sudo yum install rsync

For openSUSE Leap system, use Zypper Command to install rsync.

$ sudo zypper install rsync

The rsync command syntax is as follows:

rsync [options] [source] [destination]

There are many options available for rsync, but the options listed below are often used, so make a note of it.

-a : Archive mode
-v : Increase verbosity
-z : Compress files and folders data during the transfer
-e : Specify the remote shell to use
-r : Recurse into directories
--delete : Delete files that don't exist on sender system

1) How to Copy/Sync Files Locally

The example below will synchronize files locally from one location to another. The “cpmove-mageshco.tar.gz” file will be copied from the “/home/2daygeek” directory to the “/home/mageshm” directory.

# rsync -zvr /home/2daygeek/cpmove-mageshco.tar.gz /home/mageshm/
sending incremental file list
cpmove-mageshco.tar.gz

sent 39554341 bytes  received 31 bytes  26369581.33 bytes/sec
total size is 39540978  speedup is 1.00

2) How to Copy/Sync Directories Recursively Locally

The below example will synchronize directories locally from one location to another. The “/home/2daygeek/” entire directory will be copied to the /home/mageshm/ directory.

# rsync -zvr /home/2daygeek/ /home/mageshm/
or
# rsync -avz /home/2daygeek/ /home/mageshm/
sending incremental file list
2daygeek.txt
cpmove-mageshco.tar.gz
test/
test/2daygeek-insdie.txt

sent 37912510 bytes  received 73 bytes  10832166.57 bytes/sec
total size is 39541296  speedup is 1.04

3) How to Copy/Sync Files and Directories Recursively From Local System to Remote System

The example below will sync files and directories from a local system to a remote system.
Local folder: /home/mageshco/public_html/rsync-magesh
Remote System IP: 192.168.1.5
Remote folder: /home/mageshm/

# rsync -zvr /home/mageshco/public_html/rsync-magesh/ [email protected]:/home/mageshm/
or
# rsync -avz /home/mageshco/public_html/rsync-magesh/ [email protected]:/home/mageshm/
[email protected]'s password:
sending incremental file list
magesh.txt
test/magesh-inside.txt

sent 171 bytes  received 51 bytes  9.87 bytes/sec
total size is 0  speedup is 0.00

4) How to Copy/Sync Files and Directories From Remote System to Local System

It’s vice versa. The example below will sync files and directories from a remote system to a local system.
Local folder: /home/mageshco/public_html/rsync-magesh
Remote System IP: 192.168.1.5
Remote folder: /home/mageshm/

# rsync -zvr [email protected]:/home/mageshm/ /home/mageshco/public_html/rsync-magesh/
or
# rsync -avz [email protected]:/home/mageshm/ /home/mageshco/public_html/rsync-magesh/
[email protected]'s password:
receiving incremental file list
.bash_logout
.bash_profile
.bashrc
magesh.txt
test/
test/magesh-inside.txt

sent 110 bytes  received 610 bytes  53.33 bytes/sec
total size is 318  speedup is 0.44

5) How to Copy/Sync Files and Directories From Local System to Remote System Over SSH

The example below will synchronize files and directories from a local system to a remote system via SSH.

It uses a secure SSH protocol for file transfer, so I recommend users to go with this option when you want to perform file transfer on a remote machine.
Local folder: /home/mageshco/public_html/rsync-magesh
Remote System IP: 192.168.1.5
Remote folder: /home/mageshm/

# rsync -zvre ssh /home/mageshco/public_html/rsync-magesh/ [email protected]:/home/mageshm/
or
# rsync -avze ssh /home/mageshco/public_html/rsync-magesh/ [email protected]:/home/mageshm/
[email protected]'s password:
sending incremental file list
test.txt
test/
test/test-inside.txt

sent 164 bytes  received 54 bytes  14.06 bytes/sec
total size is 0  speedup is 0.00

6) How to Copy/Sync Files and Directories From Remote System to Local System Over SSH

It’s like vice versa. The example below will synchronize files and directories from a remote system to a local system via SSH.

It uses a secure SSH protocol for file transfer, so I recommend users to go with this option when you want to perform file transfer on a remote machine.
Local folder: /home/mageshco/public_html/rsync-magesh
Remote System IP: 192.168.1.5
Remote folder: /home/mageshm/

# rsync -zvre ssh [email protected]:/home/mageshm/ /home/mageshco/public_html/rsync-magesh/
or
# rsync -avze ssh [email protected]:/home/mageshm/ /home/mageshco/public_html/rsync-magesh/
[email protected]'s password:
receiving incremental file list
./
.bash_logout
.bash_profile
.bashrc
test.txt
test/
test/test-inside.txt

sent 113 bytes  received 663 bytes  31.67 bytes/sec
total size is 318  speedup is 0.41

7) How to Copy/Sync Files and Directories From Local System to Remote System Over SSH With Non-Standard Port

The example below will synchronize files and directories from a local system to a remote system via SSH with Non-Standard port.

This example is useful if the default SSH port has been replaced by another port for security reasons.

It uses a secure SSH protocol for file transfer, so I recommend users to go with this option when you want to perform file transfer on a remote machine.
Local folder: /home/mageshco/public_html/rsync-magesh
Remote System IP: 192.168.1.5
Remote folder: /home/mageshm/

# rsync -zvre "ssh -p 2200" /home/mageshco/public_html/rsync-magesh/ [email protected]:/home/mageshm/
or
rsync -avze "ssh -p 2200" /home/mageshco/public_html/rsync-magesh/ [email protected]:/home/mageshm/
[email protected]'s password:
stdin: is not a tty
sending incremental file list
.bash_logout
.bash_profile
.bashrc
test.txt
test/
test/test-inside.txt

sent 613 bytes  received 111 bytes  96.53 bytes/sec
total size is 318  speedup is 0.44

8) How to Copy/Sync Files and Directories From Remote System to Local System Over SSH With Non-Standard Port

It’s like vice versa. The example below will synchronize files and directories from a remote system to a local system via SSH with Non-Standard port.

This example is useful if the default SSH port has been replaced by another port for security reasons.

It uses a secure SSH protocol for file transfer, so I recommend users to go with this option when you want to perform file transfer on a remote machine.
Local folder: /home/mageshco/public_html/rsync-magesh
Remote System IP: 192.168.1.5
Remote folder: /home/mageshm/

# rsync -zvre "ssh -p 2200" [email protected]:/home/mageshm/ /home/mageshco/public_html/rsync-magesh/
or
# rsync -avze "ssh -p 2200" [email protected]:/home/mageshm/ /home/mageshco/public_html/rsync-magesh/
[email protected]'s password:
stdin: is not a tty
receiving incremental file list
.bash_logout
.bash_profile
.bashrc
test.txt
test/
test/test-inside.txt

sent 128 bytes  received 309 bytes  17.84 bytes/sec
total size is 318  speedup is 0.73

9) How to Add a Progress Status When Executing the rsync Command

The below rsync command shows the progress of the file/folder copy. This may give you an estimate idea of how long this activity will last.
Local folder: /home/mageshco/public_html/rsync-magesh
Remote System IP: 192.168.1.5
Remote folder: /home/mageshm/

# rsync -zvre ssh --progress [email protected]:/home/mageshm/ /home/mageshco/public_html/rsync-magesh/
or
# rsync -avze ssh --progress [email protected]:/home/mageshm/ /home/mageshco/public_html/rsync-magesh/
[email protected]'s password:
receiving incremental file list
.bash_logout
          18 100%   17.58kB/s    0:00:00 (xfer#1, to-check=5/7)
.bash_profile
         176 100%  171.88kB/s    0:00:00 (xfer#2, to-check=4/7)
.bashrc
         124 100%  121.09kB/s    0:00:00 (xfer#3, to-check=3/7)
test.txt
           0 100%    0.00kB/s    0:00:00 (xfer#4, to-check=2/7)
test/
test/test-inside.txt
           0 100%    0.00kB/s    0:00:00 (xfer#5, to-check=0/7)

sent 110 bytes  received 594 bytes  56.32 bytes/sec
total size is 318  speedup is 0.45

10) How to Delete Files/Folders That Don’t Exist in the Sender System When Using the rsync Command

The example below will sync files and directories from the local system to the remote system and remove files and folders that aren’t in the sender system.

This can be achieved by adding the --delete option with the rsync command.
Local folder: /home/mageshco/public_html/rsync-magesh
Remote System IP: 192.168.1.5
Remote folder: /home/mageshm/

# rsync -zvre ssh --delete /home/mageshco/public_html/rsync-magesh/ [email protected]:/home/mageshm/
or
# rsync -avze ssh --delete /home/mageshco/public_html/rsync-magesh/ [email protected]:/home/mageshm/
[email protected]'s password:
sending incremental file list
deleting test.txt

sent 141 bytes  received 13 bytes  20.53 bytes/sec
total size is 318  speedup is 2.06

11) How to Remove Source System Files After Synchronization

The example below will synchronize files and directories from the local system to the remote system and remove all files and folders from the source system once synced.

This can be achieved by adding the --remove-source-files option with the rsync command.
Local folder: /home/mageshco/public_html/rsync-magesh
Remote System IP: 192.168.1.5
Remote folder: /home/mageshm/

# rsync -zvre ssh --remove-source-files /home/mageshco/public_html/rsync-magesh/ [email protected]:/home/mageshm/
or
# rsync -avze ssh --remove-source-files /home/mageshco/public_html/rsync-magesh/ [email protected]:/home/mageshm/
[email protected]'s password:
sending incremental file list
test.html
test.php
test1.php

sent 255 bytes  received 70 bytes  34.21 bytes/sec
total size is 318  speedup is 0.98

# ls -lh /home/mageshco/public_html/rsync-magesh/
ls: cannot access /home/mageshco/public_html/rsync-magesh/: No such file or directory

12) How to Synchronize Only the Specific Type of Files From Local System to a Remote System

The example below will synchronize only the “.php” extension files from the local system to the remote system.

This can be achieved by adding the --include option with the rsync command.
Local folder: /home/mageshco/public_html/rsync-magesh
Remote System IP: 192.168.1.5
Remote folder: /home/mageshm/

# rsync -zvre ssh --include '*.php' --exclude '*' /home/mageshco/public_html/rsync-magesh/ [email protected]:/home/mageshm/
or
# rsync -avze ssh --include '*.php' --exclude '*' /home/mageshco/public_html/rsync-magesh/ [email protected]:/home/mageshm/
[email protected]'s password:
sending incremental file list
test.php
test1.php

sent 132 bytes  received 50 bytes  17.33 bytes/sec
total size is 0  speedup is 0.00

13) How to Synchronize All Files From a Local System to a Remote System Except for a Specific Type

The example below will synchronize all files except the “.txt” extension files from the local system to the remote system.

This can be achieved by adding the --exclude option with the rsync command.
Local folder: /home/mageshco/public_html/rsync-magesh
Remote System IP: 192.168.1.5
Remote folder: /home/mageshm/

# rsync -zvre ssh --exclude '*.txt' /home/mageshco/public_html/rsync-magesh/ [email protected]:/home/mageshm/
or
# rsync -avze ssh --exclude '*.txt' /home/mageshco/public_html/rsync-magesh/ [email protected]:/home/mageshm/
[email protected]'s password:
sending incremental file list
test.html
test.php
test1.php

sent 255 bytes  received 70 bytes  34.21 bytes/sec
total size is 318  speedup is 0.98

14) How to Exclude Files That Have the Latest Date in the Target System

The following example will skip any files that have been newly modified on the target system. This option helps you when you synchronize files from production servers to development servers.

This can be achieved by adding the --update option with the rsync command.
Local folder: /home/mageshco/public_html/rsync-magesh
Remote System IP: 192.168.1.5
Remote folder: /home/mageshm/

# rsync -zvre ssh --update /home/mageshco/public_html/rsync-magesh/ [email protected]:/home/mageshm/
or
# rsync -avze ssh --update /home/mageshco/public_html/rsync-magesh/ [email protected]:/home/mageshm/
[email protected]'s password:
sending incremental file list

sent 190 bytes  received 13 bytes  23.88 bytes/sec

15) How To Set the File Size Limit in the rsync Command

You can specify the MAX file size limit in the sync command. This will leave large (Like, zip, tar and video files) size files. This can be achieved by adding the --max-size= option with the rsync command.

The below example will synchronize only files that has a size ≤100KB.
Local folder: /home/mageshco/public_html/rsync-magesh
Remote System IP: 192.168.1.5
Remote folder: /home/mageshm/

# rsync -zvre ssh --max-size='100k' /home/mageshco/public_html/rsync-magesh/ [email protected]:/home/mageshm/
or
# rsync -avze ssh --max-size='100k' /home/mageshco/public_html/rsync-magesh/ [email protected]:/home/mageshm/
[email protected]'s password:
sending incremental file list
test.html
test.php
test.txt
test1.php

sent 334 bytes  received 95 bytes  24.51 bytes/sec
total size is 318  speedup is 0.74

16) How to Automate Your Backup to Remote Server Using the rsync Command

Automating remote backups is one of the mandatory procedures in any environment. If you want to do that, follow the four steps described in the article below.

I hope you found this article helpful. Please provide your valuable feedback / comments in the comment section.

Stay tuned with us !!

Magesh Maruthamuthu

Love to play with all Linux distribution

You may also like...