Chapter 40: Bash File Sync (rsync)
Bash File Sync (rsync) day! ☕
This is the real upgrade after we learned cp, mv, and scp. Many people call rsync the king of file copying/syncing in Linux/Bash world — especially in 2026 when everyone has cloud servers, backups, websites, photos, code repos, and slow internet in Hyderabad sometimes.
rsync = remote sync It’s not just “copy” — it’s smart synchronization. It compares source and destination, then only transfers what’s different (changed, new, or missing) — that’s why it’s super fast for backups, website deploys, mirroring folders, or moving huge data over SSH.
Key superpowers:
- Only sends differences (delta transfer) — even inside big files
- Preserves permissions, timestamps, owners, symlinks
- Compresses during transfer (saves bandwidth)
- Deletes extra files on destination (true mirror)
- Dry-run (preview without changing anything — lifesaver!)
- Works locally or remote (over SSH by default)
Think of it as: cp = photocopy everything every time scp = secure photocopy everything every time rsync = smart assistant who checks “what changed?” and only updates those parts — like Google Drive sync but in terminal.
Basic Syntax (Memorize This!)
|
0 1 2 3 4 5 6 |
rsync [options] source/ destination/ |
Note the trailing / after source — very important! It means “copy the contents of source into destination”, not the folder itself.
Without / on source → copies the folder as a subfolder.
1. Most Important Combo Everyone Uses: -avz (or -aP)
|
0 1 2 3 4 5 6 |
rsync -avz source_folder/ destination_folder/ |
What each letter means:
- -a → archive mode = -rlptgoD (recursive + links + permissions + timestamps + group + owner + devices/specials) → Almost perfect copy — preserves everything important
- -v → verbose = shows what files are transferred
- -z → compress during transfer = saves bandwidth (great on Jio/Airtel)
Real example — backup your Documents:
|
0 1 2 3 4 5 6 |
rsync -avz ~/Documents/ ~/backup/Documents_2026/ |
→ Only new/changed files go to backup
Add -P (progress + partial) for big transfers:
|
0 1 2 3 4 5 6 |
rsync -avzP ~/Videos/family_trip/ /mnt/external_drive/videos/ |
- Shows progress bar
- Resumes if interrupted (Ctrl+C then run same command again)
2. Remote Sync Over SSH (Most Powerful Use)
rsync uses SSH automatically when you give remote path like scp.
Local → Remote (upload website):
|
0 1 2 3 4 5 6 |
rsync -avz --delete public_html/ webliance@yourdomain.com:/var/www/html/ |
Remote → Local (download logs):
|
0 1 2 3 4 5 6 |
rsync -avzP ubuntu@123.45.67.89:/var/log/nginx/ ~/nginx_logs/ |
Mirror exactly (delete extras on destination):
|
0 1 2 3 4 5 6 |
rsync -avz --delete source/ user@host:/dest/ |
→ If you deleted a file in source → it deletes on destination too (careful!)
3. Dry Run – ALWAYS Do This First! (–dry-run or -n)
Preview what will happen — no changes made.
|
0 1 2 3 4 5 6 |
rsync -avz --dry-run --delete ~/projects/myapp/ server:/var/www/myapp/ |
→ Shows “would transfer these files”, “would delete those” — super safe before real run.
4. Other Very Useful Options Table
| Option | What it does | Example Use Case |
|---|---|---|
| –dry-run / -n | Simulate only — no actual changes | Test before dangerous sync |
| –delete | Delete files on dest that are gone from source | True mirror / clean backup |
| –exclude=’pattern’ | Skip files/folders matching pattern | –exclude=’*.tmp’ –exclude=’node_modules/’ |
| –include=’pattern’ | Force include certain files (overrides exclude) | Selective sync |
| –progress / -P | Show progress bar + allow resume | Big files over slow net |
| –human-readable / -h | Sizes in KB/MB/GB instead of bytes | Easier to read |
| –stats | Summary stats at end | How many files, bytes transferred |
| –bwlimit=KB/s | Limit bandwidth (e.g. 500) | Don’t kill internet for family |
| -e “ssh -p 2222” | Custom SSH options (port, key, etc.) | Non-standard port |
Super common full command for website deploy:
|
0 1 2 3 4 5 6 7 8 9 10 |
rsync -avz --delete \ --exclude='.git' \ --exclude='node_modules' \ --exclude='*.log' \ ./ webliance@server:/var/www/site/ |
5. Practice Session Right Now (Safe!)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# Create playground mkdir -p rsync_test/source rsync_test/dest cd rsync_test echo "Old file" > source/old.txt echo "New file" > source/new.txt mkdir source/sub echo "Sub file" > source/sub/data.txt # First sync rsync -avz --dry-run source/ dest/ # See preview # Real sync rsync -avz source/ dest/ # Change something echo "Changed!" >> source/new.txt # Sync again — only changed file transfers! rsync -avz source/ dest/ # Add delete test rm source/old.txt rsync -avz --dry-run --delete source/ dest/ # See it would delete old.txt # Real mirror rsync -avz --delete source/ dest/ |
See how smart it is? Only transfers changes!
6. rsync vs scp vs cp (Quick Teacher Comparison)
| Feature | cp | scp | rsync |
|---|---|---|---|
| Speed for repeat sync | Full copy always | Full copy always | Only differences (fastest) |
| Resume interrupted | No | No | Yes (-P) |
| Preserve everything | With -p/-a | With -p | Best with -a |
| Delete extras | No | No | Yes (–delete) |
| Compress transfer | No | No | Yes (-z) |
| Remote support | Local only | Yes (SSH) | Yes (SSH by default) |
| Dry-run / preview | No | No | Yes (–dry-run) |
| Best for | Local quick copy | One-time secure copy | Backups, deploys, mirrors |
In 2026 — use rsync for almost everything except tiny one-time copies.
7. Teacher Warnings
- Trailing / matters — practice it!
- –delete is dangerous — always –dry-run first!
- Never rsync / or important system folders without extreme care
- For huge datasets → add –bwlimit or run at night
- SSH keys make it passwordless & script-friendly
Got the power now? rsync is your smart backup/deploy/sync friend — once you start using -avz –delete –dry-run, you’ll never go back to plain cp/scp for serious work.
Any part confusing? Want next: “Teacher, show rsync cron job for daily backup” or “exclude patterns deep dive” or “rsync daemon mode” or back to other commands?
Tell me — teacher is ready in Hyderabad! Keep syncing smartly! 🐧🔄📂 😄
