The rsync command is one of the most powerful and popular tools in Linux for synchronizing files and directories between different locations. It’s widely used for backups, remote file transfers, and directory synchronization, offering features such as incremental transfers, compression, and file permission preservation.

In this guide, we’ll cover rsync basics along with practical examples to help you master its usage.

Basic Syntax of rsync

rsync [OPTION]... SRC [SRC]... DEST
  • SRC: Source file(s) or directory
  • DEST: Destination file(s) or directory

Commonly Used rsync Options

OptionDescription
-aArchive mode (preserves symbolic links, permissions, timestamps, etc.)
-vVerbose output
-zCompress file data during transfer
-PShow progress and allow resuming partial transfers
--deleteDelete files from the destination that no longer exist in the source
-rRecursively transfer directories

Examples of rsync in Action

1. Basic File Synchronization

rsync -av /source/directory/ /destination/directory/
  • -a: Preserves all file attributes
  • -v: Displays detailed progress

2. Synchronizing Files Over SSH

rsync -avz -e ssh /source/directory/ user@remote:/destination/directory/
  • -e ssh tells rsync to use SSH for secure transfer, ideal for remote backups.
READ 👉  Best Linux Distributions for Developers in 2026

3. Excluding Files and Directories

rsync -av --exclude 'file.txt' /source/directory/ /destination/directory/
  • Excludes file.txt while syncing everything else.

4. Resuming Incomplete Transfers

rsync -avP /source/directory/ /destination/directory/
  • -P combines --partial and --progress to resume interrupted transfers.

5. Deleting Files in Destination

rsync -av --delete /source/directory/ /destination/directory/
  • Ensures the destination mirrors the source by removing files that no longer exist in the source.

6. Syncing Files Between Local and Remote Systems

Local to Remote:

rsync -avz /local/directory/ user@remote:/destination/directory/

Remote to Local:

rsync -avz user@remote:/source/directory/ /local/directory/

7. Backing Up a Directory

rsync -av /source/directory/ /backup/directory/$(date +%F)
  • Creates a timestamped backup (e.g., /backup/directory/2026-03-07).

8. Testing Commands with Dry Run

rsync -av --delete --dry-run /source/directory/ /destination/directory/
  • Preview changes without making them. Remove --dry-run to execute.

9. Limiting Bandwidth During Transfers

rsync -avz --bwlimit=5000 /source/directory/ user@remote:/destination/directory/
  • Limits transfer speed to ~5 MB/s to avoid saturating network bandwidth.

10. Space-Efficient Incremental Backups

rsync -av --link-dest=/backup/previous/ /source/directory/ /backup/$(date +%F)/
  • Hard-links unchanged files to previous backups, saving disk space while making each backup appear full.

Watch Out for Trailing Slashes

The presence or absence of a trailing slash on the source path changes rsync behavior:

rsync -av /source/directory/ /destination/directory/
  • Copies contents of /source/directory/ into /destination/directory/.
rsync -av /source/directory /destination/directory/
  • Copies the directory itself into /destination/directory/, creating /destination/directory/directory/.

Complementary Commands

  • iostat: Monitor disk I/O during large transfers
  • df / du: Check available disk space and disk usage before syncing

Conclusion

rsync is a versatile tool that simplifies file synchronization and backup tasks on Linux. By combining the right options, you can efficiently manage local and remote transfers, preserve file integrity, and secure your data.

READ 👉  Slurm: Open-Source Job Scheduler for Clusters and Supercomputers

For secure remote transfers, also check out scp, and for archiving, consider tar.

Related Commands:

  • scp – Secure copy files between hosts
  • cp – Copy files and directories
  • tar – Archive files and directories

For more advanced options, refer to the manual:

man rsync
Did you enjoy this article? Feel free to share it on social media and subscribe to our newsletter so you never miss a post!

And if you'd like to go a step further in supporting us, you can treat us to a virtual coffee ☕️. Thank you for your support ❤️!
Buy Me a Coffee

Categorized in:

Tagged in:

,