Last updated
Basic Local Directory Sync
Synchronize two local directories:
rsync -av /source/directory/ /destination/directory/
Options:
-a Archive mode (preserves permissions, timestamps, symlinks, etc.)
-v Verbose output (shows files being transferred)
Note: Trailing slash on source means "sync contents of this directory"
No trailing slash means "sync the directory itself"
Dry Run — Preview Without Transferring
See what would be transferred without actually doing it:
rsync -avn /source/directory/ /destination/directory/
-n Dry run (no files are transferred)
Output shows:
sending incremental file list
new-file.txt
modified-file.txt
sent 1,234 bytes received 89 bytes 2,646.00 bytes/sec
total size is 45,678 speedup is 17.32 (DRY RUN)
Sync to Remote Server via SSH
Transfer files to a remote server:
rsync -avz /local/path/ user@server.example.com:/remote/path/
Options:
-z Compress data during transfer (reduces bandwidth)
With custom SSH port:
rsync -avz -e 'ssh -p 2222' /local/path/ user@server.example.com:/remote/path/
With specific SSH key:
rsync -avz -e 'ssh -i ~/.ssh/deploy_key' /local/path/ user@server.example.com:/remote/path/
Sync from Remote Server (Download)
rsync -avz user@server.example.com:/remote/path/ /local/path/
Exclude Files and Directories
Skip specific files and directories during sync:
# Exclude node_modules and .git
rsync -av --exclude='node_modules' --exclude='.git' /source/ /destination/
# Exclude multiple patterns
rsync -av \
--exclude='node_modules/' \
--exclude='.git/' \
--exclude='*.log' \
--exclude='*.tmp' \
--exclude='.DS_Store' \
/source/ /destination/
# Use an exclude file
rsync -av --exclude-from='.rsyncignore' /source/ /destination/
# .rsyncignore contents:
node_modules/
.git/
*.log
*.tmp
.DS_Store
dist/
Website Deployment
Deploy a built website to a web server:
rsync -avz --delete \
--exclude='.git' \
--exclude='node_modules' \
--exclude='src' \
./dist/ \
deploy@webserver.example.com:/var/www/html/
Options used:
-a Preserve file attributes
-v Verbose output
-z Compress during transfer
--delete Remove files deleted from dist/
Incremental Backup
Create a backup that only transfers changed files:
rsync -avz --delete \
--backup \
--backup-dir=/backups/$(date +%Y-%m-%d) \
/data/ \
/backups/current/
--backup Keep copies of overwritten/deleted files
--backup-dir=DIR Store backup copies in this directory
Result:
/backups/current/ — latest version of all files
/backups/2026-03-17/ — files that changed on March 17
/backups/2026-03-16/ — files that changed on March 16
Bandwidth Limiting
Limit transfer speed to avoid saturating the network:
# Limit to 1 MB/s (1024 KB/s)
rsync -avz --bwlimit=1024 /source/ user@server:/destination/
# Limit to 500 KB/s for background backup
rsync -avz --bwlimit=500 /data/ backup@nas:/backups/
Common values:
--bwlimit=512 512 KB/s (background, minimal impact)
--bwlimit=1024 1 MB/s
--bwlimit=5120 5 MB/s
--bwlimit=10240 10 MB/s
Resume Interrupted Transfer
Resume a large file transfer that was interrupted:
rsync -avz --partial --progress /large-files/ user@server:/destination/
--partial Keep partially transferred files (enables resume)
--progress Show transfer progress for each file
Output:
large-video.mp4
1,234,567,890 100% 45.23MB/s 0:00:26 (xfr#1, to-chk=0/1)
Checksum Verification
Force comparison by file content rather than modification time:
rsync -avc /source/ /destination/
-c Checksum mode — compare files by MD5 hash, not mtime+size
Use when:
- File modification times may be unreliable
- You need to verify file integrity after transfer
- Files were copied without preserving timestamps
Note: Slower than default comparison but more thorough
With Logging
Log all transferred files for auditing:
rsync -avz \
--log-file=/var/log/rsync/backup-$(date +%Y%m%d).log \
--stats \
/data/ \
backup@nas:/backups/
--log-file=FILE Write log to this file
--stats Show summary statistics after transfer
Log output includes:
Number of files transferred
Total bytes sent/received
Transfer speed
Any errors encountered
SSH Configuration Options
Customize the SSH connection for remote rsync:
# Custom port + specific key + compression
rsync -avz \
-e 'ssh -p 2222 -i ~/.ssh/deploy_key -C' \
/source/ \
deploy@server.example.com:/destination/
# Disable host key checking (for automated scripts — use carefully)
rsync -avz \
-e 'ssh -o StrictHostKeyChecking=no' \
/source/ \
deploy@server.example.com:/destination/
- Local and remote (SSH) directory synchronization
- Dry run mode to preview changes before transferring
- --delete flag to mirror source exactly
- Exclude patterns for node_modules, .git, logs, and more
- Bandwidth limiting for background operations
- --partial and --progress for large file transfers
- Checksum verification for thorough file comparison
- Logging and statistics for backup auditing
Examples
Delete Files Not in Source
Mirror the source exactly, removing files deleted from source:
rsync -av --delete /source/directory/ /destination/directory/
--delete Remove files from destination that no longer exist in source
Warning: Always do a dry run first when using --delete:
rsync -avn --delete /source/directory/ /destination/directory/