rsync efficiently synchronizes files and directories, transferring only the differences between source and destination rather than copying everything every time — a core tool for backups, deployments, and file transfers.
Why rsync Over Simple cp or scp
rsync's delta-transfer algorithm only sends the parts of files that actually changed — dramatically faster than re-copying entire files for incremental syncs of large datasets, and it can resume interrupted transfers.
Basic Local Sync
rsync -av /source/directory/ /destination/directory/
The trailing slash on the source matters: with it, the contents of the directory sync into the destination; without it, the directory itself is copied as a subdirectory of the destination.
Common Flags Explained
| Flag | Meaning |
|---|---|
| -a | Archive mode: preserves permissions, timestamps, symlinks, and recurses into directories |
| -v | Verbose output |
| -z | Compress data during transfer (useful over slower network connections) |
| --delete | Remove files in destination that no longer exist in source (true mirroring) |
| -n | Dry run — shows what would happen without actually doing it |
Syncing to a Remote Server Over SSH
rsync -avz -e ssh /local/directory/ user@remote-server:/remote/directory/
Syncing from a Remote Server to Local
rsync -avz -e ssh user@remote-server:/remote/directory/ /local/directory/
Dry Run Before a Destructive Sync
rsync -avz --delete -n /source/ /destination/
Always test with -n before running with --delete for the first time on any new sync pair — verify the files it would delete are genuinely the ones you intend.
Excluding Files/Directories
rsync -av --exclude='node_modules' --exclude='.git' /source/ /destination/
Using an Exclude File for Multiple Patterns
rsync -av --exclude-from='exclude-list.txt' /source/ /destination/
Limiting Bandwidth Usage
rsync -avz --bwlimit=5000 /source/ user@remote:/destination/
Limits transfer to 5000 KB/s, useful to avoid saturating bandwidth needed for other services during a large sync.
Showing Transfer Progress
rsync -avz --progress /source/ /destination/
Automating Regular Syncs with Cron
0 2 * * * rsync -avz --delete /var/www/ /backup/www/ >> /var/log/rsync-backup.log 2>&1
Using rsync for Efficient Backups
See How to Set Up Automated VPS Backups — rsync's incremental transfer makes it well-suited for regular backup jobs, especially when combined with hard-link-based techniques for space-efficient historical snapshots.
Common Errors
Permission denied errors during sync — verify the SSH user has appropriate read/write permissions on both source and destination paths.
Unexpected files deleted after using --delete — always dry-run first (-n); this flag is powerful and unforgiving if source/destination paths are swapped by mistake.
Continue Reading
- How to Transfer Files To and From a VPS (SCP, SFTP & rsync)
- How to Set Up Automated VPS Backups
- How to Migrate an E-commerce Store to a New VPS Without Downtime
Browse more articles in Linux Server Administration.