What is the difference between scp and rsync for copying files over SSH?
Both copy over SSH. scp sends the whole file every time, while rsync compares source and destination and sends only the differences, can resume, and can preserve permissions and delete extras. Use scp for one-off copies and rsync for repeated syncs and large folders.
Explain it simply
Imagine sending a 500-page document to a friend who already has 498 of the pages. scp prints and couriers all 500 again. rsync checks first and sends only the 2 that changed.
scp report.pdf amit@server:/home/amit/ copies one file. scp -r folder/ amit@server:/backup/ copies a directory. -P 2222 sets the port (capital P, unlike ssh's lowercase -p).
rsync -avz folder/ amit@server:/backup/folder/:
-aarchive: recursive and keeps permissions, times and links-vverbose,-zcompress in transit--progressand-Pshow progress and allow resuming--deleteremoves files from the destination that no longer exist at the source (use with care; try--dry-runfirst)
Trailing slashes matter in rsync: folder/ copies the contents, folder copies the folder itself.
Example
scp -P 2222 notes.txt amit@203.0.113.10:/tmp/
rsync -avz --dry-run site/ amit@203.0.113.10:/var/www/site/
rsync -avzP site/ amit@203.0.113.10:/var/www/site/What to say in the interview
"scp copies everything each time; rsync transfers only differences and can resume, so I use rsync -avz for backups and deployments and scp for a quick single file. I run rsync with --dry-run before using --delete."
Common mistakes
- Forgetting the trailing slash on the rsync source and creating a nested folder.