Ubuntu scp/rsync Basics: File Transfer Between Servers
What You'll Learn
- How to use scp for simple file transfers
- How to use rsync for efficient synchronization
- When to use which tool
- Copy-paste templates for common scenarios
Quick Summary
- scp: Simple copy over SSH. Good for single files or small transfers.
- rsync: Efficient sync. Good for large/repeated transfers, only sends differences.
Table of Contents
1. scp Basics
scp (secure copy) transfers files over SSH. Simple and straightforward.
Local → Remote
$ scp /local/file.txt user@remote:/remote/path/
Remote → Local
$ scp user@remote:/remote/file.txt /local/path/
Copy directory (-r)
$ scp -r /local/dir/ user@remote:/remote/path/
Non-standard port (-P)
$ scp -P 2222 /local/file.txt user@remote:/remote/path/
2. rsync Basics
rsync is efficient for large transfers - it only sends differences.
Local → Remote
$ rsync -avz /local/dir/ user@remote:/remote/path/
Remote → Local
$ rsync -avz user@remote:/remote/dir/ /local/path/
Key Options
-a: Archive mode (preserves permissions, timestamps, etc.)-v: Verbose output-z: Compress during transfer-n: Dry run (preview without changes)--delete: Delete files on destination not in source--progress: Show progress
Dry run first (recommended)
$ rsync -avzn /local/dir/ user@remote:/remote/path/
3. When to Use Which
| Scenario | Recommended |
|---|---|
| Single file, one-time | scp |
| Large directory sync | rsync |
| Repeated backups | rsync |
| Resume interrupted transfer | rsync |
4. Common Errors and Fixes
Permission denied
- Check SSH key authentication
- Verify destination directory permissions
- Try with sudo if needed
Connection timeout
- Check SSH connectivity first:
ssh user@remote - Verify firewall/security group settings
No space left on device
- Check destination disk space:
df -h - Clean up before transfer
5. Copy-Paste Templates
scp Templates
# Single file upload scp /local/file.txt user@remote:/remote/path/ # Single file download scp user@remote:/remote/file.txt /local/path/ # Directory upload scp -r /local/dir/ user@remote:/remote/path/ # With non-standard port scp -P 2222 /local/file.txt user@remote:/remote/path/
rsync Templates
# Sync directory (dry run first) rsync -avzn /local/dir/ user@remote:/remote/path/ rsync -avz /local/dir/ user@remote:/remote/path/ # With progress display rsync -avz --progress /local/dir/ user@remote:/remote/path/ # Mirror (delete extra files on destination) rsync -avz --delete /local/dir/ user@remote:/remote/path/ # Non-standard SSH port rsync -avz -e "ssh -p 2222" /local/dir/ user@remote:/remote/path/
Test Environment
Commands in this article were tested on Ubuntu 24.04 LTS / bash 5.2.