Top 10 Most Useful rsync Commands

The rsync Linux command is a versatile tool for incremental file transfers. Specifically, it keeps two directories in sync, whether they be local directories or remote. As you can imagine, this works incredibly well for backups or for directories that receive gradual file changes.

It’s almost impossible to overstate the versatility of rsync. The command is packed with options that allow for granular control over file transfers. rsync is the type of utility that you can use for a long time, while still discovering new things you didn’t know it could do.

And that’s why we’ve created this guide – to help you understand some of rsync’s most popular and powerful features. In the following examples, you’ll see 10 of the most useful rsync commands that every Linux user ought to know. Let’s dive in!

1. The Most Common Switches

rsync has a ton of options. All of the most common ones have been wrapped into a single flag, which is -a (archive). This option is equal to -rlptgoD, which means recursive, copy symlinks as symlinks, preserve permissions, preserve modification times, preserve group, preserve owner, and preserve device and special files.

In the vast majority of situations, you’ll want all of those options, and that’s why rsync has combined them all into the single -a flag.

The other option you’ll want to use all the time is -v (verbose). This just shows you what rsync is doing, otherwise there’s not any output unless an error occurs.

$ rsync -av /src /dst

2. Trailing Slash

The previous example is going to synchronize the source directory with /dst/src as the destination. To change this behavior, we can add a trailing slash to the source directory in our rsync command. This will avoid creating the /dst/src folder, and instead just synchronize our /src and /dst directories.

$ rsync -av /src/ /dst

3. Exclude Files or Directories

If there are one or more files and directories that you wish to exclude from the rsync transfer, you can specify their paths as exclusions in your command. Use the --exclude option to accomplish this, and specify as many files or directories that you need to exclude.

$ rsync -av --exclude 'file1' --exclude 'dir2' /src/ /dst

Or, you can put your exclusions inside a text file, and tell rsync to exclude all files and folders listed in that file.

$ rsync -av --exclude-from 'exclusions.txt' /src/ /dst

For more exclusion examples and detailed explanation, see our guide on how to exclude files and directories from rsync.

4. Remote Transfers via SSH

It’s possible to run rsync as a daemon, but an easier way to accomplish remote file transfers is by tunneling rsync through SSH. All you need to do is use the -e ssh option in your command. Then, your files are securely transferred with SSH encryption.

$ rsync -av -e ssh /src/ user@10.10.150.3:/path/to/dst

You’ll be prompted for the remote user’s password after executing the command, unless you set up RSA key authentication. It’s also possible to specify your SSH password in the rsync command, or use rsync on a non-default SSH port.

5. Delete Extraneous Files From Destination

If the destination directory contains files that are no longer present in the source, rsync won’t touch them unless you specify the --delete option. Specifying this option means you can delete a file from the source directory, and then run rsync and have it deleted from the destination directory as well.

$ rsync -av --delete /src/ /dst

6. Perform a Dry Run

If you’re wary of using the --delete option mentioned above, or you just want to see what the rsync command will transfer, you can use the -n or --dry-run flag to check. This will simulate a transfer and show you the results, without actually performing any synchronization.

$ rsync -av --delete --dry-run /src/ /dst

7. Use File Checksums

Normally, rsync decides what files to transfer by analyzing modification times and file sizes for differences. It’s possible to make rsync use file checksums instead, with the -c flag. That means it will send incremental changes when it detects that a file in the source directory has a different checksum from the same file in the destination directory.

This type of transfer takes substantially longer, since generating checksums is a CPU-intensive process. This is a nice feature to use every once in a while, as it can help you detect silent data corruption, or “bit rot.”

$ rsync -avc /src/ /dst

If you’re just checking for data corruption – to make sure all checksums match – you’ll probably want to include the --dry-run option as well.

$ rsync -avc --dry-run /src/ /dst

8. Resume Transfers and Show Progress

For remote file transfers, the --partial flag comes in handy in case there’s an interruption. It will allow you to resume right where you left off. And the --progress option will show a sync’s progress in terminal, allowing you to see the current percentage and speed of each file transfer.

Both of these options have been combined into the single -P flag, since they are usually used together.

$ rsync -avPe ssh /src/ user@10.10.150.3:/path/to/dst

9. Log rsync Transfers

By default, rsync’s output and errors will show in your terminal. You can automatically save this information to a log file by using the --log-file option. This is especially handy if you have configured scheduled rsync transfers (for example, in cron), and need to see the results later.

$ rsync -av --log-file=transfer.log /src/ /dst

See more examples in our guide on how to log rsync transfers.

10. Preserve Hard Links and Symlinks (or Don’t)

The -a option already copies symlinks as symlinks. To change this, use the -L option to transform a symlink into its referent file or directory.

$ rsync -avL /src/ /dst

By default, hard links are not preserved. This means that a hard link will be transferred as its referent file into the destination directory. This behavior can be changed with -H, which will copy hard links as hard links.

$ rsync -avH /src/ /dst

Conclusion

These 10 examples will help you get the most out of using rsync. There are many other options available, although most are reserved for very unique situations. Do you have any favorites that you didn’t see in our list? Let us know in the comments below.

If our content helped you, please consider buying us a coffee. We appreciate your support!

Buy me a coffee donation button

2 thoughts on “Top 10 Most Useful rsync Commands”

  1. Trailing slashes are not required on the destination directory. It works as you say it does with trailing slashes on the source directory and with or without trailing slashes on the destination directory.

Leave a Comment

Your email address will not be published. Required fields are marked *