The Linux Rsync Command: Usage and Practical Examples

In this guide, we show you how to will transfer files with the Linux Rsync command, a very flexible tool for your file syncing needs

On this page
  1. Installing Rsync
  2. Basic Syntax
  3. Flags And Options
  4. Examples
  5. Tips And Tricks
  6. Conclusion
  7. Additional Links

Unix Like systems including Linux are composed of many files and folders. So it isn’t surprising that there are many times when synchronizing files and directories is needed. This is where the rsync command in Linux shines as it’s used to transfer data between two locations. It supports transferring files between local and remote directories. The rsync command is my go-to command line tool for transferring many files.

In this blog post, we will transfer files with this extremely flexible tool, having just the right options for your file sync needs.

#Installing Rsync

On most Linux Distributions the package is rsync so for example with dnf and apt the install command would be.

Code
dnf install rsync
Code
apt install rsync

#Basic Syntax

The basic syntax for rsync is as follows:

Code
rsync [options] source destination

If local it will be the path of the file or directory you want to sync.

If transferring from or to a remote location this will be in the format user@host:/path/to/destination.

#Flags And Options

The options for rsync are vast, after going through them here are the ones I believe everyone should know.

If you don’t see a particular option in this section review the man page using the bellow command or refer to an online version.

Code
man rsync

#Examples

In the below examples, the flags discussed are shown in practical examples. All the examples include multiple flags, most commonly the v (verbose output) and h (human-readable format) only affect the output and I recommend using them.

Local To Local

Code
rsync -ahv /source/path/ /destination/path

If you need to copy files and directories from one place to another locally use this command.

Setting Ownership And Permissions

Code
rsync -vh --chown=user:group --chmod=755 /source/path/ /destination/path

Instead of using the archive flag, we set the desired ownership and file permissions manually.

Do A Dry Run

Code
rsync -avh --dry-run /source/path/ /destination/path

In this example, nothing is being transferred but the output can be used to confirm the command is structed correctly.

Local To Remote

Code
rsync -avhz /source/path/ [email protected]:/destination/path

Transfers from local to remote machine using compression.

Local To Remote With Bandwidth Limit

Code
rsync -avhz --bwlimit=100M /source/path/ [email protected]:/destination/path

Transfers from remote to local using compression and a bandwidth limit.

Remote To Local

Code
rsync -avhz [email protected]:/source/path/ /destination/path

Remote sync from remote to local using compression.

Remote To Local Using Custom Port

Code
rsync -avhz -e 'ssh -p <port-number>' [email protected]:/source/path/ /destination/path

Transfers from remote to local using compression over a non-standard ssh port.

Show Stats After Transfer

Code
rsync --stats -avh /path/to/source/ /path/to/destination/

Update files that are newer and show transfer stats

Sync Files Smaller Than 1GB And Larger Than 1MB

Code
rsync --max-size=1G --min-size=1M -avh /path/to/source/ /path/to/backup/

This example sets a maximum and minimum file size for the files to transfer.

Update Files And Directories

Code
rsync -avhu /path/to/source/ /path/to/destination/

Files that are newer in the source will overwrite the older versions, in addition, files that don’t exist on the destination will be copied over.

Ignore Existing Files

Code
rsync --ignore-existing -avh /path/to/source/ /path/to/destination/

All the files that already exist are skipped and any missing files from the source are copied over.

Delete Files After Transfer

Code
rsync -ahv --remove-source-files /source/path/ /destination/path

In this example, the command removes only the files from the source after transfer.

#Tips And Tricks

  • Always do a dry run using the verbose flag first to confirm everything works as expected.
  • If transferring between a remote system consider using a bandwidth limit.
  • If transferring between a remote system consider using compression.
  • If using a delete or remove be certain you understand the flag before using.
  • Unless you know the permission needs to be different use archive mode to preserve them.
  • If you need to filter files consider using rsync together with the find command.
    • Although rsync does have filtering capabilities my recommendation is to couple it with the find command like the example below:find source -type f -exec rsync -avh --progress {} destination ;

In this example, the find command is being used to filter out only the files and transfer them

#Conclusion

In conclusion, we started with installing the rsync command and touched on syntax and options. From there, I have included many different examples using the options which I feel everyone should know. Whether you need remote syncor just need to sync directories on your local machine rsync has got you covered.

Done reading and want additional resources, why not check out these?

Illustrated avatar of CJ Saathoff
Written by

CJ Saathoff

CJ Saathoff writes for the NameHero blog.

More from CJ →