rsync stands for "remote sync" and is a powerful command line utility for synchronizing directories either on a local system or with remote machines. It's built into nearly every Linux system by default.

Some users mistakenly think of rsync as a file copying tool, like cp or scp. While there's some overlap, rsync excels in synchronization, specifically. In other words, it can take a source directory and make an identical destination directory. And when a file changes in the source directory, rsync can efficiently synchronize the contents to the destination directory, only transferring the bits that have changed. It's also a very secure utility, utilizing SSH for remote file transfers.

This makes rsync work very well as a backup tool, on top of file copying. Many backup utilities use rsync in some form or another, because some users don't bother to learn how to use the rsync command. In reality, it can be very complex, but rsync only gets complicated when you need to do specific things. If you learn about rsync from the basics, it's easy to wrap your head around.

In this guide, we'll learn how to use the rsync command through examples. Follow along on your own system if you have two directories that you'd like to keep in sync, and learn to master the rsync command.

In this tutorial you will learn:
  • rsync command line examples
rsync command on Linux
rsync command on Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software rsync
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # - requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ - requires given linux commands to be executed as a regular non-privileged user

rsync Command Examples

It's easiest to learn about rsync through examples. To get started, use some of the following commands on your own system, and you'll quickly have it mastered.

  1. There are a bunch of options that are really common to use with rsync. This includes recursive transfer, the transfer of file modification times, file permissions, symbolic links, etc. All these options combined ends up being -rlptgoD. However, rsync has combined all these options into the single -a (archive) switch, so we can use all the most common options with just one flag. Notice also the trailing slash on our directories, which will avoid creating an additional directory level at the destination.
    $ rsync -a /src/ /dst/
    

  2. SUBSCRIBE NEWSLETTER & RSS
    Subscribe to RSS and NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.


  3. The previous command won't produce much output, unless an error occurs. To get more information about the current transfer, you can add the -v (verbose) option to the command. The syntax below is probably the most common form of rsync that you will see. It's the one you should try hardest to remember, as you're likely to resort to it often.
    $ rsync -av /src/ /dst/
    
  4. By default, rsync won't delete any files from the destination directory. It will only transfer the new files and the changes made to current files. If you want to delete extraneous files drom the destination directory, you can add the --delete option to the command.
    $ rsync -av --delete /src/ /dst/
    
  5. If you want to see what changes rsync plans to make, before it actually transfers the changes, you can use the -n or --dry-run flag in your command. This is especially a good idea if you're also using the --delete option, since it will show you what files are going to be deleted.
    $ rsync -avn --delete /src/ /dst/
    
  6. Up until now, we've only been showing rsync examples that work for directories on the same system. One of rsync's most powerful features is that it can also be used with remote systems. To run rsync through SSH, we can add the -e ssh option in our command. Specify the remote SSH user and destination directory in the command as well. You'll be prompted for the SSH password after entering the command.
    $ rsync -av -e ssh /src/ user@remote:/path/to/dst/
    
  7. If the remote server is accepting SSH connections on a different port than the default (22), you can use the following command syntax to instruct rsync to connect to that port. This example assumes that SSH is running on port 2200.
    $ rsync -av -e 'ssh -p 2200' /src/ user@remote:/path/to/dst/
    


  8. When using rsync for remote transfers, the --partial and --progress options are very helpful. They will resume partial transfers in case a previous sync is interrupted, and show you the current transfer progress of all files, respectively. You can use the -P option to combine both of these features into a single flag.
    $ rsync -avP -e ssh /src/ user@remote:/path/to/dst/
    
  9. Another useful option that only becomes relevant with remote transfers is the -z flag, which enables file compression. This can save a little bit of bandwidth and speed up data transfers, but will cost your system a bit of CPU usage to compress files before transferring them.
    $ rsync -avPz -e ssh /src/ user@remote:/path/to/dst/
    
  10. By default, rsync checks the sizes and modification times of files to detect if changes have been made. If a file in the source directory and destination directory have the same size and mtime, then rsync skips the file. In 99.99% of situations, this is a sufficient way to detect whether changes have been made to a file. But there's also the -c option which will check each file's checksum. This is a foolproof way to verify whether a file in the source is different than the corresponding file in the destination, however, it'll slow down the entire rsync process tremendously, since the CPU spends a lot of time determining each file's checksum.
    $ rsync -avc /src/ /dst/
    
  11. It's common to have some files or directories that you don't want rsync to transfer to the destination - a common example would be a cache directory. You can exclude files or directories with the --exclude option and the following command syntax. Note that you need to use the relative path of the source destination from your rsync command (in other words, the following example means that our excluded directory is located in /src/.cache/.
    $ rsync -av --exclude .cache /src/ /dst/
    
  12. You can also list multiple directories and files in a text file, one on each line. Then, use the --exlude-from option to tell rsync the location of the text file that contains your exclusion list.
    $ rsync -av --exclude-from exclusions.txt /src/ /dst/
    


  13. Remember that you can also use wildcards to match certain patterns for file names that you wish to exclude. For example, you could exclude all .jpg and .txt files with the following rsync command.
    $ rsync -av --exclude *.jpg --exclude *.txt /src/ /dst/
    
  14. rsync gives us some statistics about our file transfer when we use the -P option, as shown in a previous example. It also gives us a summary of stats when the entire transfer is finished. These can be a little hard to read, as everything is shown in bytes. To make file sizes more human readable, we can append the -h flag to our command.
    $ rsync -avh /src/ /dst/
    
  15. In our rsync's output, there's no distinction between files that are being created, updated, having their permissions changed, etc. To see itemized changes for each file, which can be really handy if you need to sift through logs later, you can use the -i option.
    $ rsync -avi /src/ /dst/
    
  16. You can redirect rsync's output by using the usual bash operators such as >, >>, etc. The following would save all of rsync's output to a text file, rather than your terminal screen. However, errors would still show in your terminal, if any are encountered.
    $ rsync -av /src/ /dst/ > rsync.log
    
  17. If you'd like to see rsync's output, while simultaneously logging it to a file for later reference, you can pipe your command to tee.
    $ rsync -av /src/ /dst/ > | tee rsync.log
    
  18. rsync has a lot of granular features that let you control what exactly is transferred. The -a option, which nearly every rsync command includes, can be overridden by various flags. For example, see the following command where we instruct rsync not to update the owner, group, and permissions of files, despite using the -a option.
    $ rsync -av --no-perms --no-owner --no-group /src/ /dst/
    
  19. Use the -H option with rsync in order to preserve hard links. Without this option, rsync will transfer hard links as files.
    $ rsync -avH /src/ /dst/
    

  20. SUBSCRIBE NEWSLETTER & RSS
    Subscribe to RSS and NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.


  21. Use the -l option to copy symbolic links as symbolic links, or the -L option to transform symbolic links into their referent file or directory in the remote destination. Note that the -a option automatically implies -l.
    $ rsync -avL /src/ /dst/
    
  22. If you need to save some bandwidth, you can instruct rsync to only transfers files below a certain size by using the --max-size option. The following example will only transfer files that are below 500 KB in size.
    $ rsync -av -e ssh --max-size='500k' /src/ user@remote:/path/to/dst/
    

Closing Thoughts

In this guide, we learned 20 different practical command examples for the rsync command in Linux. As you can see just from the sheer amount of examples, rsync is an extremely versatile tool that can take very specific instructions from the user. This makes it very ideal as a backup or file transfer tool, since we can configure it exactly as we need. But it also means there are a plethora of options to learn. This tutorial showed you some of the most common and useful options to use with rsync, and should cover you for most situations you find yourself in.

FIND LATEST LINUX JOBS on LinuxCareers.com
Submit your RESUME, create a JOB ALERT or subscribe to RSS feed.
LINUX CAREER NEWSLETTER
Subscribe to NEWSLETTER and receive latest news, jobs, career advice and tutorials.
DO YOU NEED ADDITIONAL HELP?
Get extra help by visiting our LINUX FORUM or simply use comments below.