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
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.
dnf install rsync
apt install rsync
#Basic Syntax
The basic syntax for rsync is as follows:
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.
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
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
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
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
rsync -avhz /source/path/ [email protected]:/destination/path
Transfers from local to remote machine using compression.
Local To Remote With Bandwidth Limit
rsync -avhz --bwlimit=100M /source/path/ [email protected]:/destination/path
Transfers from remote to local using compression and a bandwidth limit.
Remote To Local
rsync -avhz [email protected]:/source/path/ /destination/path
Remote sync from remote to local using compression.
Remote To Local Using Custom Port
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
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
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
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
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
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 ;
- Although rsync does have filtering capabilities my recommendation is to couple it with the find command like the example below:
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.
#Additional Links
Done reading and want additional resources, why not check out these?



