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
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
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Other||Privileged access to your Linux system as root or via the |
|Conventions|| # - requires given linux commands to be executed with root privileges either directly as a root user or by use of |
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.
- 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/
- 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/
- 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
--deleteoption to the command.
$ rsync -av --delete /src/ /dst/
- If you want to see what changes rsync plans to make, before it actually transfers the changes, you can use the
--dry-runflag in your command. This is especially a good idea if you're also using the
--deleteoption, since it will show you what files are going to be deleted.
$ rsync -avn --delete /src/ /dst/
- 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 sshoption 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/
- 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/
- When using rsync for remote transfers, the
--progressoptions 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
-Poption to combine both of these features into a single flag.
$ rsync -avP -e ssh /src/ user@remote:/path/to/dst/
- Another useful option that only becomes relevant with remote transfers is the
-zflag, 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/
- 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
-coption 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/
- 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
--excludeoption 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
$ rsync -av --exclude .cache /src/ /dst/
- You can also list multiple directories and files in a text file, one on each line. Then, use the
--exlude-fromoption to tell rsync the location of the text file that contains your exclusion list.
$ rsync -av --exclude-from exclusions.txt /src/ /dst/
- 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/
- rsync gives us some statistics about our file transfer when we use the
-Poption, 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
-hflag to our command.
$ rsync -avh /src/ /dst/
- 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
$ rsync -avi /src/ /dst/
- 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
- 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
$ rsync -av /src/ /dst/ > | tee rsync.log
- rsync has a lot of granular features that let you control what exactly is transferred. The
-aoption, 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
$ rsync -av --no-perms --no-owner --no-group /src/ /dst/
- Use the
-Hoption with rsync in order to preserve hard links. Without this option, rsync will transfer hard links as files.
$ rsync -avH /src/ /dst/
- Use the
-loption to copy symbolic links as symbolic links, or the
-Loption to transform symbolic links into their referent file or directory in the remote destination. Note that the
-aoption automatically implies
$ rsync -avL /src/ /dst/
- If you need to save some bandwidth, you can instruct rsync to only transfers files below a certain size by using the
--max-sizeoption. 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/
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.