Quick Links
The Linuxrsynccommand is a powerful file copying and folder synchronization tool. Here are ten common use-cases that you’ll be able to put to good use on your own systems.
The rsync Tool
Thersynctool copies files and directories between two computers. It uses asophisticated algorithmthat scans directory trees to find files on the source computer that don’t exist on the destination computer. These files are transmitted to the destination computer. What makes
so clever is it can figure out which pieces of existing files have been modified, and it only sends the changed portions.

You can usersyncto copy files to a different location on your hard drive, to a different hard drive in the same computer, to anexternally connected USB drive, or any other network-accessible location.
Related:How to Back Up Your Linux System With rsync
On top of that,rsynccan optionally preserve symbolic links, hard links, and filemetadatasuch as file ownership, permissions, and access times. To support all this functionality,rsynchas many options and figuring them all out takes time. We’ve collected these 10 examples to help you get started. We’ve already written about doing backups withrsync, so we’re concentrating on other uses here.
For all of its many options, the structure of anrsynccommand is simple. We need to provide the source, the destination, and the options we want to use. You’ll probably find thatrsyncis already installed on your Linux computer—it was, on all of our test machines—but if it isn’t it’ll definitely be in your distribution’s repositories.

1. Copy Files to a Different Directory
Here’s a simple example to get us going. We’re going to copy the files from the “project-files” directory to the “Documents” directory. We’re using two options, the-a(archive) option and the-v(verbose) option. The verbose option tellsrsyncto explain what its doing as it does it. The
option preserves file ownership and some other items we’ll look at shortly.

The format of the command is
Usinglson “Documents” folder shows the files have been copied.
Whilersyncis working, the files are listed as they’re copied. We’re told:
We modified the text file in the source directory and repeated thersynccommand.

This time the only file that needs to be updated is the text file we modified. The speedup figure is now 30,850. This is how much more efficient it is to copy the modified portion of that single file, than to copy all of the files.
The-a(archive) option actually represents a collection of other options. It’s the same as using all of these options:

This is such a frequently used combination thatrsyncprovides the-a(archive) option as a shorthand way to invoke them all.
2. Copy a Directory to a Different Directory
If you look at the previousrsynccommand you’ll see a trailing forward slash “/” on the file path of the source directory. This is significant. It tellsrsyncto copy the contents of the directory. If you don’t provide the trailing forward slash,rsyncwill copy the directory and its contents.
This time the directory name is added to the file name as they are listed. If we look inside the destination directory we’ll see the source folder has been copied across with the files inside it.

3. Copy a Directory to a Different Drive
Copying files to another location on the same hard drive doesn’t give you protection against adrive failure. If that drive gives up the ghost, you’ve lost the source and destination copies of those files. Copying them to another hard drive is a much more robust way to protect your data. All we need to do is provide the correct path to the destination drive.
Looking at the other hard drive shows us the directory and files were copied over to it.

Related:What to Do When Your Hard Drive Fails
4. Doing a Dry Run First
Before we look at howrsynccan usefully delete files for us, let’s look at how we can makersyncperform a dry run.
In a dry run,rsyncgoes through the motions of performing the actions we’ve asked for, but doesn’t actually do them. It reports on what would have happened if the command had been executed. This way, we can make sure that the command does exactly what we expect.

To force a dry run we use the–dry-runoption.
The files that would have been copied are listed for us, and we get the usual statistics, followed by the message(DRY RUN)so that we know nothing actually took place.
5. Deleting Files in the Destination Directory
The–deleteoption tellsrsyncto deletefiles and directoriesin the destination directory that are not in the source directory. This means the destination directory will be an exact copy of the source directory. We’ll be prudent and use the–dry-runoption first.
We’re informed that two files will be deleted. If we’re sure we don’t care that they’ll be deleted, we can remove the–dry-runoption and carry out the command for real.

This time the contents of the directories are synchronized and the two extra files are deleted.
6. Deleting The Source Files
You can choose to delete the source files after a successful transfer, makingrsyncoperate more like a move than a copy. If the transfer wasn’t successful, the source files are not deleted. The option we need to use is–remove-source-files.
Note that the files can be deleted even if no files are transferred. That’s because ifrsyncchecks, and all the files are already in the destination directory and there’s nothing forrsyncto do,rsyncconsiders that a successful transfer.

Also,rsynconly deletes the files from the source directory. It doesn’t delete the source directory nor any sub-directories, just the files in them. We can see this by using the-R(recursive) option withls, on the source directory.
7. Copy Files to a Remote Computer
To synchronize folders with a remote computer,rsyncmust be installed on both computers. Set up SSH communication between the two computers before you attempt to usersyncto the remote computer.
Related:How to Create and Install SSH Keys From the Linux Shell
You need to be able to log in remotely as a regular user on the remote computer forrsyncto work. It doesn’t matter whether you use an ID and password to log in, or if you’ve set up SSH keys for securepassword-lessaccess, but if you can’t log in as a user, thenrsyncwon’t work either.
If you log in with a password,rsyncwill prompt you for the password. If you use SSH keys to log, in the process is seamless.
The only extra thing you need to do is to add the user account name and the IP address of the remote computer to the start of the destination file path. Use an at sign “@” to separate the user name from the computer name or IP address, and a colon “:” to separate the computer name orIP addressfrom the directory path.
We get the same information reported to us as we do when we’re copying files locally.
8. Include or Exclude Files or Directories
You may have files and directories in the source directory that you don’t want to copy to the destination computer. You canexclude themusing the–excludeoption. In a similar way, you’re able to choose to include specific files and directories with the–includeoption.
The quirk is that if you use the–includeoption on its own, all files are copied, as normal—including your specifically included files. To only copy your included files you have to–excludeeverything else.
You can use as many–includeand–excludeoptions in your command as you like, but make sure you put your–includeoptions before your–excludeoptions. Also, make sure you have a trailing forward slash on your source file path.
This command copies only C source code files andCSV data filesto the destination computer.
The only files copied are the ones we specifically included.
9. Compress Files in Transfer
The-z(compress) option causesrsyncto compress the transferred files. They’re not stored as compressed files on the destination computer though, they’re only compressed during the transfer itself. This can speed up lengthy transfers.
10. Monitoring Progress
Speaking of lengthy transfers, we can add some statistics so that we can see the progress of the transfer.
The-P(partial, progress) option actually adds two options,–partialand–progress. The–partialoption tellsrsyncto keep partially transferred files if a transfer fails. This saves time when the transfer is restarted.
The–progressoption prints, for each file, the data transferred in bytes and as a percentage, the speed of the transfer, the time taken, the number of the file being transferred, and the count of the remaining files.
The output scrolls past pretty quickly, and it is difficult to read. You can improve things slightly by removing the-v(verbose) option from the command. Even then it is still difficult to read as it whizzes by.
It is often more useful to monitor the progress of the overall transfer. You can do this with the –info option, and passing it “progress2” as a parameter.
This gives a progress report that is actually useful.
Like cp on Steroids
Thersynccommand is fast, flexible, and well worth the time it takes to familiarize yourself with it. The basic form of anrsynccommand with the-avoptions, source directory, and destination directory isn’t hard to remember at all.
For many use-cases, that’s all you need. Get comfortable with that and the rest will come easily.