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
- Basic Syntax
- Flags And Options
- Examples
- Local To Local
- Setting Ownership And Permissions
- Do A Dry Run
- Local To Remote
- Local To Remote With Bandwidth Limit
- Remote To Local
- Remote To Local Using Custom Port
- Show Stats After Transfer
- Sync Files Smaller Than 1GB And Larger Than 1MB
- Update Files And Directories
- Ignore Existing Files
- Delete Files After Transfer
- Tips And Tricks
- Conclusion
- Additional Links
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.
–bwlimit=RATE allows you to set a bandwidth limit on transfers
–human-readable, -h output numbers in a human-readable format
–progress show progress during the transfer
–max-size=SIZE the maximum size of a file you want to transfer
–min-size=SIZE the minimum size of a file you want to transfer
–remove-source-files sender removes synchronized files (non-dir)
–dry-run, -n perform a trial run with no changes made
–compress, -z compress data during the transfer
–perms, -p preserve source permissions
–quiet, -q suppress non-error messages
–checksum, -c skip based on checksum, not mod-time & size
–archive, -a archive mode is equivalent of -rlptgoD
–recursive, -r recurse into directories
–update, -u skip files that are newer on the destination
–ignore-existing skip updating files that exist on the destination
–verbose, -v increases the verbosity of the output
–stats when the command finishes transfer statistics will be displayed
–delete delete extraneous files from the destination
–chmod=CHMOD allows for setting permissions during the transfer
–chown=USER:GROUP allows for setting ownership during the transfer
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?
Embracing a lifelong passion for technology since childhood, CJ delved into the intricate workings of systems, captivated by the desire to understand the unknown. This innate curiosity led to his discovery of Linux, a revelation that resonated deeply. With more than 7 years of on the job experience, he’s honed his technical skills as a Geek and Senior Linux Systems Administrator.
Leave a Reply