Did You Know?
Donate to TecMint

We are pleased to announce our new TecMint Q/A section to submit your Linux questions

Rsync (Remote Sync): 10 Practical Examples of Rsync Command in Linux

Download Your Free eBooks NOW - 10 Free Linux eBooks for Administrators

Rsync (Remote Sync) is a most commonly used command for copying and synchronizing files and directories remotely as well as locally in Linux/Unix systems. With the help of rsync command you can copy and synchronize your data remotely and locally across directories, across disks and networks, perform data backups and mirroring between two Linux machines.

Rsync Commands

Rsync Local and Remote File Synchronization

This article explains 10 basic and advanced usage of the rsync command to transfer your files remotely and locally in Linux based machines. You don’t need to be root user to run rsync command.

Some advantages and features of Rsync command
  1. It efficiently copies and sync files to or from a remote system.
  2. Supports copying links, devices, owners, groups and permissions.
  3. It’s faster than scp (Secure Copy) because rsync uses remote-update protocol which allows to transfer just the differences between two sets of files. First time, it copies the whole content of a file or a directory from source to destination but from next time, it copies only the changed blocks and bytes to the destination.
  4. Rsync consumes less bandwidth as it uses compression and decompression method while sending and receiving data both ends.
Basic syntax of rsync command
# rsync options source destination
Some common options used with rsync commands
  1. -v : verbose
  2. -r : copies data recursively (but don’t preserve timestamps and permission while transferring data
  3. -a : archive mode, archive mode allows copying files recursively and it also preserves symbolic links, file permissions, user & group ownerships and timestamps
  4. -z : compress file data
  5. -h : human-readable, output numbers in a human-readable format
Install rsync in your Linux machine

We can install rsync package with the help of following command.

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

1. Copy/Sync Files and Directory Locally

Copy/Sync a File on a Local Computer

This following command will sync a single file on a local machine from one location to another location. Here in this example, a file name backup.tar needs to be copied or synced to /tmp/backups/ folder.

[root@tecmint]# rsync -zvh backup.tar /tmp/backups/

created directory /tmp/backups

backup.tar

sent 14.71M bytes  received 31 bytes  3.27M bytes/sec

total size is 16.18M  speedup is 1.10

In above example, you can see that if the destination is not already exists rsync will create a directory automatically for destination.

Copy/Sync a Directory on Local Computer

The following command will transfer or sync all the files of from one directory to a different directory in the same machine. Here in this example, /root/rpmpkgs contains some rpm package files and you want that directory to be copied inside /tmp/backups/ folder.

[root@tecmint]# rsync -avzh /root/rpmpkgs /tmp/backups/

sending incremental file list

rpmpkgs/

rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm

rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm

rpmpkgs/nagios-3.5.0.tar.gz

rpmpkgs/nagios-plugins-1.4.16.tar.gz

sent 4.99M bytes  received 92 bytes  3.33M bytes/sec

total size is 4.99M  speedup is 1.00

2. Copy/Sync Files and Directory to or From a Server

Copy a Directory from Local Server to a Remote Server

This command will sync a directory from a local machine to a remote machine. For example: There is a folder in your local computer “rpmpkgs” which contains some RPM packages and you want that local directory’s content send to a remote server, you can use following command.

[root@tecmint]$ rsync -avz rpmpkgs/ root@192.168.0.101:/home/

root@192.168.0.101's password:

sending incremental file list

./

httpd-2.2.3-82.el5.centos.i386.rpm

mod_ssl-2.2.3-82.el5.centos.i386.rpm

nagios-3.5.0.tar.gz

nagios-plugins-1.4.16.tar.gz

sent 4993369 bytes  received 91 bytes  399476.80 bytes/sec

total size is 4991313  speedup is 1.00
Copy/Sync a Remote Directory to a Local Machine

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

[root@tecmint]# rsync -avzh root@192.168.0.100:/home/tarunika/rpmpkgs /tmp/myrpms

root@192.168.0.100's password:

receiving incremental file list

created directory /tmp/myrpms

rpmpkgs/

rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm

rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm

rpmpkgs/nagios-3.5.0.tar.gz

rpmpkgs/nagios-plugins-1.4.16.tar.gz

sent 91 bytes  received 4.99M bytes  322.16K bytes/sec

total size is 4.99M  speedup is 1.00

3. Rsync Over SSH

With rsync, we can use SSH (Secure Shell) for data transfer, using SSH protocol while transferring our data you can be ensured that your data is being transferred in a secured connection with encryption so that nobody can read your data while it is being transferred over the wire on the internet.

Also when we use rsync we need to provide the user/root password to accomplish that particular task, so using SSH option will send your logins in an encrypted manner so that your password will be safe.

Copy a File from a Remote Server to a Local Server with SSH

To specify a protocol with rsync you need to give “-e” option with protocol name you want to use. Here in this example, We will be using “ssh” with “-e” option and perform data transfer.

[root@tecmint]# rsync -avzhe ssh root@192.168.0.100:/root/install.log /tmp/

root@192.168.0.100's password:

receiving incremental file list

install.log

sent 30 bytes  received 8.12K bytes  1.48K bytes/sec

total size is 30.74K  speedup is 3.77
Copy a File from a Local Server to a Remote Server with SSH
[root@tecmint]# rsync -avzhe ssh backup.tar root@192.168.0.100:/backups/

root@192.168.0.100's password:

sending incremental file list

backup.tar

sent 14.71M bytes  received 31 bytes  1.28M bytes/sec

total size is 16.18M  speedup is 1.10

4. Show Progress While Transferring Data with rsync

To show the progress while transferring the data from one machine to a different machine, we can use ‘–progress’ option for it. It displays the files and the time remaining to complete the transfer.

[root@tecmint]# rsync -avzhe ssh --progress /home/rpmpkgs root@192.168.0.100:/root/rpmpkgs

root@192.168.0.100's password:

sending incremental file list

created directory /root/rpmpkgs

rpmpkgs/

rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm

           1.02M 100%        2.72MB/s        0:00:00 (xfer#1, to-check=3/5)

rpmpkgs/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)

rpmpkgs/nagios-3.5.0.tar.gz

           1.79M 100%        1.56MB/s        0:00:01 (xfer#3, to-check=1/5)

rpmpkgs/nagios-plugins-1.4.16.tar.gz

           2.09M 100%        1.47MB/s        0:00:01 (xfer#4, to-check=0/5)

sent 4.99M bytes  received 92 bytes  475.56K bytes/sec

total size is 4.99M  speedup is 1.00

5. Use of –include and –exclude Options

These two options allows us to include and exclude files by specifying parameters with these option helps us to specify those files or directories which you want to include in your sync and exclude files and folders with you don’t want to be transferred.

Here in this example, rsync command will include those files and directory only which starts with ‘R’ and exclude all other files and directory.

[root@tecmint]# rsync -avze ssh --include 'R*' --exclude '*' root@192.168.0.101:/var/lib/rpm/ /root/rpm

root@192.168.0.101's password:

receiving incremental file list

created directory /root/rpm

./

Requirename

Requireversion

sent 67 bytes  received 167289 bytes  7438.04 bytes/sec

total size is 434176  speedup is 2.59

6. Use of –delete Option

If a file or directory not exist at the source, but already exists at the destination, you might want to delete that existing file/directory at the target while syncing .

We can use ‘–delete‘ option to delete files that are not there in source directory.

Source and target are in sync. Now creating new file test.txt at the target.

[root@tecmint]# touch test.txt
[root@tecmint]# rsync -avz --delete root@192.168.0.100:/var/lib/rpm/ .
Password:
receiving file list ... done
deleting test.txt
./
sent 26 bytes  received 390 bytes  48.94 bytes/sec
total size is 45305958  speedup is 108908.55

Target has the new file called test.txt, when synchronize with the source with ‘–delete‘ option, it removed the file test.txt.

7. Set the Max Size of Files to be Transferred

You can specify the Max file size to be transferred or sync. You can do it with “–max-size” option. Here in this example, Max file size is 200k, so this command will transfer only those files which are equal or smaller than 200k.

[root@tecmint]# rsync -avzhe ssh --max-size='200k' /var/lib/rpm/ root@192.168.0.100:/root/tmprpm

root@192.168.0.100's password:

sending incremental file list

created directory /root/tmprpm

./

Conflictname

Group

Installtid

Name

Provideversion

Pubkeys

Requireversion

Sha1header

Sigmd5

Triggername

__db.001

sent 189.79K bytes  received 224 bytes  13.10K bytes/sec

total size is 38.08M  speedup is 200.43

8. Automatically Delete source Files after successful Transfer

Now, suppose you have a main web server and a data backup server, you created a daily backup and synced it with your backup server, now you don’t want to keep that local copy of backup in your web server.

So, will you wait for transfer to complete and then delete those local backup file manually? Of Course NO. This automatic deletion can be done using ‘–remove-source-files‘ option.

[root@tecmint]# rsync --remove-source-files -zvh backup.tar /tmp/backups/

backup.tar

sent 14.71M bytes  received 31 bytes  4.20M bytes/sec

total size is 16.18M  speedup is 1.10

[root@tecmint]# ll backup.tar

ls: backup.tar: No such file or directory

9. Do a Dry Run with rsync

If you are a newbie and using rsync and don’t know what exactly your command going do. Rsync could really mess up the things in your destination folder and then doing an undo can be a tedious job.

Use of this option will not make any changes only do a dry run of the command and shows the output of the command, if the output shows exactly same you want to do then you can remove ‘–dry-run‘ option from your command and run on the terminal.

root@tecmint]# rsync --dry-run --remove-source-files -zvh backup.tar /tmp/backups/

backup.tar

sent 35 bytes  received 15 bytes  100.00 bytes/sec

total size is 16.18M  speedup is 323584.00 (DRY RUN)

10. Set Bandwidth Limit and Transfer File

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

[root@tecmint]# rsync --bwlimit=100 -avzhe ssh  /var/lib/rpm/  root@192.168.0.100:/root/tmprpm/
root@192.168.0.100's password:
sending incremental file list
sent 324 bytes  received 12 bytes  61.09 bytes/sec
total size is 38.08M  speedup is 113347.05

Also, by default rsync syncs changed blocks and bytes only, if you want explicitly want to sync whole file then you use ‘-W‘ option with it.

[root@tecmint]# rsync -zvhW backup.tar /tmp/backups/backup.tar
backup.tar
sent 14.71M bytes  received 31 bytes  3.27M bytes/sec
total size is 16.18M  speedup is 1.10

That’s all with rsync now, you can see man pages for more options. Stay connected with Tecmint for more exciting and interesting tutorials in future. Do leave your comments and suggestions.

I am a linux server admin and love to play with Linux and all other distributions of it. I am working as System Engineer with a Web Hosting Company.

Linux Services & Free WordPress Setup

Our post is simply ‘DIY’ aka ‘Do It Yourself, still you may find difficulties and want us to help you out. We offer wide range of Linux and Web Hosting Solutions at fair minimum rates. Please submit your orders by Clicking Here.

44 Responses

  1. Hari says:

    hii
    thanks for the information
    can u plz tell about scp also
    :)

  2. Dumitru says:

    Nice post here. Highlighted a lot of rsync’s features.
    Might want to mention Grsync as a tool for beginners, since it offers a neat way to fiddle with rsync and even allows for generating a command line version of every option selected.

    • Tarunika shrivastava says:

      Hi Dumitru,

      Thanks, Grsync is a GUI tool for syncing data. I’ll cover this topic in more details in our future articles.

      • Dumitru says:

        Actually, Grsync is just a GUI wrapper around rsync. It only allows to select options for running rsync with, and nothing more. I mean, without having rsync already installed, Grsync does absolutely nothing in terms of syncing.
        A picky comment, I know, but I like to differentiate tools and tool handles.

      • Amulya says:

        Hi Tarunika,

        Thanks for sharing the examples of rsync. Could you please help me out in writing the script for below purpose using Rsync:

        – content sync from source to target machine, based on a extensive list of directories and hostnam/ip
        – search and replace of the mapped texts from source to target
        – replacement should be only in changed/updated files and to a configurable set of file extensions

        Thanks in Advance!!

  3. alameen says:

    Tarunika…..Thank u so much.!

  4. Bernardo Verda says:

    That was a pretty good introduction and quick overview of using the rsync command.

    I do have one suggestion:
    Perhaps it would be worthwhile to point out how including or leaving off a trailing slash for directory names/path names affects the behaviour of rsync?

  5. Interesting article. I would like to add that if you’re syncing data over ssh but ssh is listening on a non-default port you may use the following command:

    rsync -Waq -e ‘ssh -p ‘ /path/to/source/ :/path/to/target/

    or another nice option, especially if you’re syncing a VPS is the ‘–numeric-ids ‘ flag which will not map uid/gid by actuall user/group, for example:

    rsync -Waq -e ‘ssh -p ‘ –numeric-ids /path/to/source/ :/path/to/target/

  6. J. A. Boyd says:

    Very good !

    is also useful:

    rsync with alternative ssh port:

    rsync -avh -e ‘ssh -p 2222′ ~/local_directory/ user@servername:remote_diretory/

  7. Ajinkya says:

    Hey Tarunika shrivastava
    very useful post…..
    I am new to linux , will you please send me some pdf on my email id – ajinkya.joshi.d@gmail.com….

  8. Info says:

    Great article

    Currently I use rsync over 2 servers to backup a whole directory with some 20 sub directories… not all of them are needed/all files needs to be copied…

    Could rsync use files as input ? ie a file to exclude specific file types and/or to exclude specific folders as well ?

    thanks in advance

  9. Chaitanya says:

    Hi Tecmint,

    Found this blog today by chance. Lots of information. Kudos to the team.

    Coming to this topic, for rsync to work across servers , I believe the private key and public keys should be installed on the source and destination servers respectively. Isnt it so?
    If so, could you explain the process of generating keys and adding them to authorized_keys?

    Thanks
    Chaitu

  10. Andre says:

    Hello Tarunika:

    Excellent article! Well written. I also liked the practical examples that you presented. Now it’s time for me to start using this powerful and versatile tool. I will also take a look at some of your other articles you posted.

    Thanks again.
    Andre

  11. ClikPals says:

    Nice post, I am using rsync to backup my files locally. I am trying to use the find command with rsync so that it can find the latest file in a certain folder remote and sync it to my local folder but it is not working.

    Could you update your post to include such a situation.

    Thanks.

  12. Bruno says:

    Thanks for practical good examples on using rsync. One is almost what I need but it includes username and password to remote server. But in my case it is not required.
    How the rsync command should look like if I want to use it to periodically check for new radio podcasts on public web server and in case new podcasts are found download it to my local machine. Example: podcasts located at http://213.215.116.181:8001/fm/DATE/FILENAME.mp3 where the DATE usually updates weekly, and FILENAME allways stays the same.
    Real file example http://213.215.116.181:8001/fm/2013-12-10/951-Experimental_FM-22-00.mp3

  13. Linux hacker says:

    There is a common confusion in your synching folders. It is one I frequently make and is a gotcha in rysnc. You MUST add the trailing slash, otherwise it will put the folder in your local folder, rather than sync the two:

    rsync -avzh /root/rpmpkgs /tmp/backups/

    will place the rpmpkgs in your backups folder. If you want to synch two folders that already exist:

    rsync -avzh /root/rpmpkgs/ /tmp/backups/rpmpkgs

    This for me is a very important little detail!

  14. Benson says:

    need help on rsync an 2000Mb file . please note on 500mb no problem .. Thanks

  15. Arif says:

    I am a linux newbie, Please tell me “What is the best way to backup files ?”

    please help.

  16. Wintch says:

    Very complete tutorial, Thanks!

  17. vin says:

    Nice one, thanks

  18. Amulya says:

    Hi All,

    Thanks for sharing the examples of rsync. Could you please help me out in writing the script for below purpose using Rsync:

    – content sync from source to target machine, based on a extensive list of directories and hostnam/ip
    – search and replace of the mapped texts from source to target
    – replacement should be only in changed/updated files and to a configurable set of file extensions

    Thanks in Advance!!

  19. Amulya says:

    Hi All,
    Could you please provide a shell script for the below purpose using Rsync?

    - content sync from source to target machine, based on a extensive list of directories and hostnam/ip

    – search and replace of the mapped texts from source to target

    – replacement should be only in changed/updated files and to a configurable set of file extensions.

    Thanks.

  20. Rahul says:

    Hi, some one can help me in rsync.

    There are two servers, I have written a cron to rsync jpg file from A to B. if I modified some files in B and keep same name, When the cron run it over right files with A server’s files.

    I m using
    rsync -avz ASeverPath BServerPath

  21. Rahul says:

    I don’t want to over right A Server files to B files, if I made changes in B server files

  22. Dan Saint-Andre says:

    If you like the features of ‘rsync’ but are reluctant because of all of the command line options, consider LuckyBackup [see http://sourceforge.net/projects/luckybackup/?source=navbar ].
    You get an end-user and a super-user edition of scripts that wrap rsync commands for the most common file backup and restore operations.

    Version 0.4.7-2 can be found in the Linux Mint-15 and corresponding Ubuntu repositories.
    I feel certain it is available elsewhere.

  23. Mahesh says:

    Good article. #8 was exactly what i was looking for. And this page was result #5 in Google search. Keep it up. Good job.

  24. “2. Copy/Sync Files and Directory to or From a Server”

    This just saved me so much time!

    We tried doing it with tar but that didn’t work. cpio is too complicated.

    Rsync rocks! Thanks!

  25. Harish Mehra says:

    Such a nice way to describe in easy way……. Is there any documentation or posts for shell scripting.

  26. Luke Swart says:

    Great article, thank you. I have not found a more concise source that still contains Rsync’s core functionality.

    • Luke Swart says:

      As a suggestion, you may want to emphasize that the `-e` option should often be followed by `”ssh -i /path/to/key/”` instead of just plain `ssh`. This is required for accessing an EC2 instance using AWS’s .pem keys, although I’m not sure when just `ssh` is used. Was the `ssh` following the `-e` just for the example?

  27. Kamal says:

    Thank you very much, these Rsync tutorials really helped me out as I’m new to this stuff. Great blog and easy to understand explanations :) Thank you

  28. Chanan Berler says:

    Great article, found it very useful :)

  29. Agustin Martin says:

    Thanks, been a great help :)

  30. reeganvijay says:

    Really understandable I got the knowledge about RSYNC.

    Thanks to Author

  31. Mochamad Iqbal Dwi Cahyo says:

    this is awesome, thank you very much for breaking down the benefit of Rsync for us :)

  32. Nilanga says:

    Hi Taru (Stars in our Language ;))

    Thank you for the post. it was really informative. My question is how do I synchronise Source Folder & a Target folder? Source Folder is been in Ubuntu and the Target folder is in Windows BUT mapped in to the Source Server as a local folder. When I use freesync it only copies the folders but not the contents. Push out an error stating file failed to sync cos its not permitted :( NOW when i do a basic copy and paste everything gets copied. Now I do not want to be doing that like a prayer would I ? So any help/Idea?. Thanks for your help in advance…..Have a good one

  33. akheel says:

    Hi Tarunika,

    How can we copy the latest log file with rsync from remote server to our local dir?

Leave a Reply

This work is licensed under a (cc) BY-NC | TecMint uses cookies. By using our services, you comply to use of our cookies. More info: Privacy Policy.
© 2012-2014 All Rights Reserved.