The rsync Command for File Synchronization
Overview
rsync is a fast, versatile file synchronization tool that copies only the differences between source and destination. Unlike scp, which transfers entire files every time, rsync performs incremental transfers — making it ideal for backups, deployments, and keeping directories in sync.
Basic Syntax
rsync [options] source destination
For remote transfers:
rsync [options] source user@host:/path
rsync [options] user@host:/path destination
The Essential Flags: -avz
The most common rsync invocation uses three flags together:
rsync -avz /local/folder/ user@server.example.com:/remote/folder/
| Flag | Meaning |
|---|---|
-a | Archive mode — preserves permissions, timestamps, symlinks, and directory structure |
-v | Verbose — shows files being transferred |
-z | Compress data during transfer |
Common Operations
Sync a local directory to a remote server
rsync -avz /home/user/my-site/ user@server.example.com:/home/user/public_html/
Important: The trailing slash on the source (
my-site/) means “copy the contents of this directory.” Without it, rsync copies the directory itself into the destination.
Pull files from a remote server to local
rsync -avz user@server.example.com:/home/user/public_html/ ./local-backup/
Specify a custom SSH port
rsync -avz -e "ssh -p 2222" /local/folder/ user@server.example.com:/remote/folder/
Excluding Files and Directories
Exclude specific files or patterns
rsync -avz --exclude="*.log" --exclude=".git" /source/ /destination/
Exclude from a file
Create an exclude list:
# exclude-list.txt
*.log
.git/
node_modules/
.env
rsync -avz --exclude-from="exclude-list.txt" /source/ /destination/
Dry Run — Preview Before Syncing
Always test with --dry-run first to see what will change without actually modifying anything:
rsync -avz --dry-run /source/ user@server.example.com:/destination/
Useful Flags Reference
| Flag | Description |
|---|---|
-a | Archive mode (preserves permissions, timestamps, etc.) |
-v | Verbose output |
-z | Compress data during transfer |
-P | Show progress and allow partial/resumed transfers |
--delete | Delete files in destination that don’t exist in source |
--dry-run | Simulate the transfer without making changes |
--exclude=PATTERN | Exclude files matching the pattern |
--exclude-from=FILE | Read exclude patterns from a file |
-e "ssh -p PORT" | Specify SSH command with custom port |
--bwlimit=KBPS | Limit bandwidth in KB/s |
-n | Same as --dry-run |
--stats | Show detailed transfer statistics |
Practical Hosting Examples
Create an incremental backup
rsync -avz /home/user/public_html/ /home/user/backups/site-backup/
Only changed files are copied on subsequent runs — fast and efficient.
Mirror a directory (delete extra files at destination)
rsync -avz --delete /source/ /destination/
Caution:
--deleteremoves files from the destination that no longer exist in the source. Always run with--dry-runfirst.
Deploy a website with progress
rsync -avzP --exclude=".git" --exclude="node_modules" \
./build/ user@server.example.com:/home/user/public_html/
Bandwidth-limited transfer
rsync -avz --bwlimit=1000 /large-backup/ user@server.example.com:/backups/
Limits the transfer to 1000 KB/s.
Tips
- Always use
--dry-runbefore any rsync with--deleteto avoid accidental data loss. - The trailing slash on paths matters — test with a small directory first if you’re unsure.
- For automated backups, combine rsync with
cronto run on a schedule. - Use
-Pfor large transfers — it shows progress and allows resuming interrupted transfers.