rsync Command in Linux — Sync Files with Examples
rsync (remote sync) is a fast, versatile file-copying tool for Linux. It copies files locally or to/from a remote host, transferring only the differences between source and destination — making repeated syncs extremely efficient.
What You’ll Learn
By the end of this tutorial, you’ll know how to sync files locally and remotely, use archive mode, enable compression, show progress, simulate with dry-run, exclude patterns, delete files on the destination, limit bandwidth, and use rsync over SSH.
Why rsync Matters
rsync is the backbone of backups, deployment, and file synchronization on Linux. It’s faster than scp for repeated transfers because it only sends differences. Durga Antivirus Pro uses rsync to distribute signature updates across servers, and DodaZIP uses it to sync build artifacts to staging environments.
rsync Learning Path
flowchart LR
A[tar Command] --> B[rsync Command<br/>You are here]
B --> C[SSH Command]
C --> D[Backup Strategies]
D --> E[Server Administration]
style B fill:#f90,color:#fff
Syntax Overview
rsync [options] source destination
rsync [options] source user@host:destination
rsync [options] user@host:source destination| Option | Description |
|---|---|
-a | Archive mode (preserve permissions, timestamps, etc.) |
-v | Verbose output |
-z | Compress during transfer |
-h | Human-readable sizes |
--progress | Show transfer progress |
-n | Dry-run (simulate without copying) |
--exclude=PATTERN | Exclude files matching pattern |
--delete | Delete files on destination not in source |
--bwlimit=RATE | Limit bandwidth (KB/s) |
-e ssh | Use SSH as transport |
10 Practical Examples
Create sample data:
mkdir -p source_dir target_dir
echo "File 1 content" > source_dir/file1.txt
echo "File 2 content" > source_dir/file2.txt
mkdir source_dir/subdir
echo "Nested file" > source_dir/subdir/file3.txt1. Local Sync
Copy everything from source to target:
rsync -av source_dir/ target_dir/sending incremental file list
./
file1.txt
file2.txt
subdir/
subdir/file3.txt
sent 263 bytes received 86 bytes 698.00 bytes/sec
total size is 34 speedup is 0.10Note the trailing slash on source_dir/ — it means “copy contents of”. Without the slash, it copies the directory itself.
2. Remote Push (Local to Remote)
Sync local directory to remote server:
rsync -avz /home/projects/ user@192.168.1.100:/backup/projects/sending incremental file list
./
src/
src/main.py
docs/
docs/guide.md
sent 1,234 bytes received 48 bytes 512.00 bytes/sec
total size is 12,345 speedup is 9.633. Remote Pull (Remote to Local)
Sync remote directory to local:
rsync -avz user@192.168.1.100:/var/www/html/ ./local-backup/receiving incremental file list
./
index.html
styles.css
images/
images/logo.png
sent 45 bytes received 12,345 bytes 4,956.00 bytes/sec
total size is 98,765 speedup is 7.974. Archive Mode (-a)
Archive mode preserves permissions, ownership, timestamps, symlinks, and recurses into subdirectories:
rsync -av source_dir/ target_dir/The -a flag is equivalent to -rlptgoD (recursive, links, permissions, times, group, owner, devices). This is the standard for backups.
5. Compression (-z)
Compress during transfer (speeds up network transfers):
rsync -avz source/ user@host:/dest/Without -z, data is sent uncompressed. Use -z for slow connections or large text files. Don’t bother for local transfers.
6. Progress and Dry-Run
Show progress for each file:
rsync -av --progress source_dir/ target_dir/sending incremental file list
./
file1.txt
14 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=2/4)
file2.txt
14 100% 0.00kB/s 0:00:00 (xfr#2, to-chk=1/4)
subdir/file3.txt
13 100% 6.50kB/s 0:00:00 (xfr#3, to-chk=0/4)Dry-run — see what would happen without copying:
rsync -avn source_dir/ target_dir/sending incremental file list
./
file1.txt
file2.txt
subdir/
subdir/file3.txt7. Exclude Patterns
Exclude .log files and the tmp directory:
rsync -av --exclude="*.log" --exclude="tmp/" source_dir/ target_dir/Exclude from a file:
cat > exclude.txt << 'EOF'
*.log
tmp/
*.bak
EOF
rsync -av --exclude-from=exclude.txt source_dir/ target_dir/8. Delete on Destination
Remove files on the destination that are not in the source (mirror mode):
rsync -av --delete source_dir/ target_dir/This makes target_dir/ an exact mirror of source_dir/. If file2.txt is deleted from source, it’s also deleted from target. Use with caution.
9. Bandwidth Limit
Limit transfer to 500 KB/s:
rsync -avz --bwlimit=500 source/ user@host:/dest/This prevents rsync from saturating your network connection, useful when syncing during business hours.
10. rsync over SSH with Custom Port
Sync over SSH on a non-standard port:
rsync -avz -e "ssh -p 2222" source/ user@host:/dest/Using SSH config (defined in ~/.ssh/config):
rsync -avz -e "ssh" source/ myserver:/dest/Common Use Cases
Backup Home Directory
rsync -avzh --delete --progress /home/alice/ /mnt/backup/alice/Sync Website to Production
rsync -avz --delete --exclude=".git" --exclude="node_modules" \
./build/ user@server:/var/www/html/Resume Interrupted Transfer
rsync automatically resumes incomplete transfers — just run the same command again. It compares source and destination and only transfers what’s missing.
Mirror a Directory with Exclusions
rsync -av --delete --exclude="cache/" --exclude="logs/" \
/source/ /destination/Common Mistakes
1. Trailing Slash Confusion
rsync -av source_dir/ target_dir/ copies contents. rsync -av source_dir target_dir/ copies the directory itself (creating target_dir/source_dir/).
2. Forgetting --delete When Mirroring
Without --delete, files removed from source remain on destination. The destination grows indefinitely. Use --delete only when you want an exact mirror.
3. Running Without Dry-Run First
Always run rsync -avn (dry-run) before destructive operations, especially with --delete. Verify what will happen before committing.
4. Not Using -z on Fast Local Networks
On local networks (gigabit+), compression wastes CPU time. Use -z only for WAN or slow connections.
Practice Questions
1. What’s the difference between source/ and source as the rsync source?
source/ copies the contents into destination. source copies the directory itself, creating dest/source/.
2. How do you simulate an rsync transfer without copying anything?
Use -n (dry-run): rsync -avn source/ dest/
3. What does --delete do in rsync?
It deletes files on the destination that do not exist on the source, making the destination an exact mirror.
4. How do you exclude multiple patterns?
Use multiple --exclude flags: rsync -av --exclude="*.log" --exclude="*.tmp" source/ dest/
5. Challenge: Write an rsync command that syncs /home/user to /backup preserving all attributes, excluding .cache and .local/share/Trash, with progress display.
rsync -av --progress --exclude=".cache" --exclude=".local/share/Trash" /home/user/ /backup/user/
Mini Project: Automated Backup Script
#!/bin/bash
# auto_backup.sh — Automated rsync backup with logging
# Usage: ./auto_backup.sh /path/to/source /path/to/destination
SOURCE="${1:?Usage: $0 /path/to/source /path/to/destination}"
DEST="${2:?Usage: $0 /path/to/source /path/to/destination}"
LOG_FILE="/var/log/backup-$(date +%Y%m%d).log"
EXCLUDE_FILE="/etc/backup_excludes.txt"
echo "=== Backup Started: $(date) ===" >> "$LOG_FILE"
rsync -avzh --delete \
--exclude-from="$EXCLUDE_FILE" \
--log-file="$LOG_FILE" \
--progress \
"$SOURCE" "$DEST"
if [ $? -eq 0 ]; then
echo "=== Backup Completed: $(date) ===" >> "$LOG_FILE"
echo "Success: Backup completed at $(date)"
else
echo "=== Backup FAILED: $(date) ===" >> "$LOG_FILE"
echo "ERROR: Backup failed. Check log: $LOG_FILE"
exit 1
fi
# Keep only 30 days of logs
find /var/log -name "backup-*.log" -mtime +30 -deleteExpected output:
=== Backup Started: Sat Jun 20 10:00:00 UTC 2026 ===
sending incremental file list
./
file1.txt
14 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=3/4)
file2.txt
14 100% 0.00kB/s 0:00:00 (xfr#2, to-chk=2/4)
subdir/
subdir/file3.txt
13 100% 0.00kB/s 0:00:00 (xfr#3, to-chk=0/4)
sent 263 bytes received 86 bytes 698.00 bytes/sec
total size is 41 speedup is 0.12
=== Backup Completed: Sat Jun 20 10:00:01 UTC 2026 ===FAQ
What’s Next
Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro