Python File Synchronization with rsync
`rsync` (Remote Sync) is the gold standard for syncing files locally or over SSH. Its patented delta-transfer algorithm only transmits byte differences rather than re-uploading entire files, making backups and deployments fast.
"If you edit 2 lines in a 500-page book, `scp` photocopies and ships the entire 500-page book again; `rsync` sends only the 2 revised sentences and updates the book in place."
Deep Dive: How It Works
The Magic Flag `-avzP`: `-a` (archive: recursive, preserves permissions/timestamps/symlinks), `-v` (verbose), `-z` (compress in flight), `-P` (show progress + allow resuming interrupted transfers).
The Trailing Slash Rule: `rsync -a src/ dst/` copies the CONTENTS of src; `rsync -a src dst/` copies the FOLDER src itself into dst.
Mirroring with `--delete`: Deletes files in destination that no longer exist in source.
Syntax Blueprint
rsync -avzP source/ destination/ # Sync contents locally rsync -avzP -e ssh ./dist/ user@host:/app/ # Sync over SSH rsync -avzP --delete src/ backup/ # Exact mirror with cleanup rsync -avzP --dry-run src/ dst/ # Test run without touching disk
Always test destructive syncing with --dry-run before executing with --delete.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Using `--delete` without testing `--dry-run` first.Why it happens: If source path is wrong or empty, `--delete` wipes out the entire destination backup.
How to fix: Always verify with `rsync -avzP --dry-run --delete src/ dst/` first.
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Perform Dry-Run Synchronization
Create `/tmp/s_test` and `/tmp/d_test`.
Run `rsync -av --dry-run /tmp/s_test/ /tmp/d_test/`.
Clean up folders.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.