Rsync ( Remote Sync) is an open source command utility that provides fast incremental file transfer from one host to another. In this article, we will explore some Rsync command examples for various use cases.

It is one of the lightweight application, because the file transfers are incremental. Rsync copies only the differences of files that have actually changed, compressed through ssh.

It's very popular and widely used for moving files or directories on Linux/Unix.

Features of rsync command

  • Speed : First time, rsync replicates the whole content between the source and destination directories. Next time, rsync transfers only the changed blocks or bytes to the destination location, which makes the transfer really fast.
  • Security : rsync allows encryption of data using ssh protocol during transfer.
  • Low Bandwidth: rsync uses compression and decompression of data block by block at the sending and receiving end respectively. So the bandwidth used by rsync will be always less compared to other file transfer protocols like FTP.
  • Privileges : No special privileges are required to install and execute rsync.

Install rsync on Linux

Before you install Rsync on your server, make sure to run below commands as root user.

yum install rsync (On Red Hat based systems)
apt-get install rsync (On Debian based systems)

The basic syntax of rsync is very straight forward, and operates in a way that is similar to ssh, scp, and cp.

rsync options source destination
  • -v : verbose
  • -r : copies data recursively (but don’t preserve timestamps and permission while transferring data
  • -a : archive mode, archive mode allows copying files recursively and it also preserves symbolic links, file permissions, user & group ownerships and timestamps.
  • -z : compress file data
  • -h : human-readable, output numbers in a human-readable format
  • -d : transfer directories without recursing -e : specify the ssh as remote shell

Rsync Command Examples

1. Sync a File on a Local Computer

The following command will sync a single file on a local machine from one location to another location.

[root@localhost]# rsync -zvh test.txt /home/test1/
test.txt
sent 87.76M bytes received 31 bytes 35.11M bytes/sec
total size is 87.74M speedup is 1.00

Here in this example, a file name test.txt needs to be copied or synced to /home/test1/ folder.

If the destination directory not exists then rsync will create a directory automatically for the destination.

2. Sync a Directory from Local Server to a Remote Server

This command will sync a directory from a local machine to a remote server.

For example: There is a folder in your local computer “public_html” which contains some files and you want that local directory’s content send to a remote server, you can use following command.

[root@localhost]$ rsync -avzh public_html/ root@10.11.12.13:/home/
root@10.11.12.13's password:
sending incremental file list
public_html/
public_html/cgi-bin
public_html/index.php
sent 72 bytes  received 30 bytes  140.80 bytes/sec
total size is 1M  speedup is 1.00

3. Sync a Remote Directory to a Local server

This command will help you sync a remote directory to a local directory. Here in this example, a directory /home/public_html which is on a remote server is being copied in your local computer in /home/test.

[root@localhost]# rsync -avzh root@10.11.12.13:/home/public_html /home/test/
root@10.11.12.13's password:
receiving incremental file list
created directory /home/test
public_html/
public_html/cgi_bin
public_html/cgi_bin/index.php
sent 91 bytes  received 4.99M bytes  322.16K bytes/sec
total size is 4.99M  speedup is 1.00

4. rsync through SSH

By using SSH protocol you can transferred the data in a secured connection with encryption. You need to provide the root password to connect this, using SSH option will send your logins in an encrypted manner so that your password will be safe.

rsync -avzh startup.php -e 'ssh -p 73' root@0:0:0:0/home/

5. How to sync a File from a Local server to a Remote server with SSH

[root@localhost]# rsync -avzhe ssh public_html/ root@10.11.12.13:/home
root@10.11.12.13's password:
sending incremental file list
public_html/
sent 16.56M bytes  received 45 bytes  1.28M bytes/sec
total size is 2M  speedup is 1.10

6. Sync a file & directory from remote to local with –include option

In this below example, rsync command will include only .php files and exclude everything in that directory.

[root@localhost]# rsync -avzeh ssh --include '*php' --exclude '*' root@10.11.12.13:/home/public_html /root/public_html
receiving incremental files list
test.php
test1.php

7. Syncing file & directory from remote to local with –exclude option

In this below example, rsync command will exclude only .txt files and include everything in that directory.

[root@localhost]# rsync -avze ssh --exclude '*txt' root@10.11.12.13:/home/public_html /root/public_html
receiving incremental files list
test.html
test1.php

8. How to Sync a file & directory from local to remote with –delete option

--delete option used when you want to sync data from the local machine and some of the files which are not available on source but present at the destination. This time --delete option will help you to remove unused files from the destination.

[root@localhost]# rsync -avzP --delete root@10.11.12.13:/home/test/public_html root/public_html
root@10.11.12.13's password:
receiving incremental file list
deleting public_html/test/file1
deleting public_html/test/file2
deleting public_html/test/
public_html/
sent 28 bytes received 137 bytes 36.67 bytes/sec
total size is 2M speedup is 2.00

9. Sync a file & directory from local to remote with progress

Below example is sync a file & directory from local server to remote server with file transfer speed and times.

[root@localhost]# rsync -avzeh ssh --progress /home/pkgs root@10.11.12.13:/root/pkgs
root@10.11.12.13's password: sending incremental file list pkgs/ pkgs/httpd-2.2.3-82.el5.centos.i386.rpm 1.02M 100% 2.72MB/s 0:00:00 (xfer#1, to-check=3/5) pkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm 99.04K 100% 241.19kB/s 0:00:00 (xfer#2, to-check=2/5) sent 4.99M bytes received 92 bytes 475.56K bytes/sec total size is 4.99M speedup is 1.00

10. Set file size limit

You can specify the MAX file size limit while sync. So that, it will leave big like zip and tar files. It can be done by adding –max-size= into rsync command. In this example it will sync 100k files also less then 100k files.

[root@localhost]# rsync -avze ssh --max-size = '100k' root@10.11.12.13:/home/public_html /root/public_html

11. Delete source Files automatically after successful Transfer

This automatic deletion can be done using ‘–remove-source-files‘ option.

[root@localhost]# rsync --remove-source-files -zvh backup.tar /home/backups/
backup.tar
sent 14.71M bytes  received 31 bytes  4.20M bytes/sec
total size is 16.18M  speedup is 1.10
[root@localhost]# ll backup.tar
ls: backup.tar: No such file or directory

12. To set Bandwidth Limit and Transfer File

You can set the bandwidth limit while transferring data from one server to another server with the the help of ‘bwlimit‘ option. This options helps us to limit I/O bandwidth.

[root@localhost]# rsync --bwlimit=100 -avzhe ssh  /home/public_html root@10.11.12.13:/root/public_html
root@10.11.12.13's password:
sending incremental file list
sent 508 bytes  received 18 bytes  81.09 bytes/sec
total size is 43M  speedup is 145347.05

13. rsync with Do a Dry Run

If you are new to rsync and don’t know what exactly the output if you run any rsync commands, than this commands will surely help you check the output first. rsync commands could really mess up the things in your destination folder and then doing an undo can be a tedious job.

This option will not make any changes only do a dry run of the command and shows the output of the command. This rsync commands help you check the output before you execute in real environment.

root@localhost]# rsync --dry-run --remove-source-files -zvh backup.tar /home/pu
backup.tar
sent 35 bytes  received 15 bytes  100.00 bytes/sec
total size is 16.18M  speedup is 323584.00 (DRY RUN)

When you run rsync commands, it may ask for password while doing rsync from local to remote server. If you want to run rsync automatically without password prompt, you must generate and use SSH key based authentication.

That's all!

Was this answer helpful? 0 Users Found This Useful (0 Votes)