Chapter 39: Bash Secure Copy (scp)
Bash Secure Copy (scp)! 😄
scp stands for Secure CoPy — it’s the secure version of the old cp command, but instead of copying files between folders on your own computer, scp copies files/folders between your local machine and a remote server (or between two remote servers) over an encrypted SSH connection.
In simple words: scp = cp + SSH security It’s like you taking a USB drive to your friend’s house in another city, copying files from/to it, but without ever leaving your room — and nobody can spy on what you’re copying because everything is encrypted.
scp uses the same authentication as ssh — so if you already set up passwordless SSH keys (like we talked about in the ssh lesson), scp will work without asking for a password every time. Super convenient!
Basic Syntax (Very Important – Memorize This Pattern)
|
0 1 2 3 4 5 6 |
scp [options] source destination |
But source or destination (or both) must include the remote part in this format:
|
0 1 2 3 4 5 6 |
username@hostname_or_ip:/path/to/file_or_folder |
Examples:
- Local → Remote
- Remote → Local
- Remote → Remote (less common)
1. Copy a File from Your Laptop to Remote Server (Most Common)
|
0 1 2 3 4 5 6 |
scp important_report.pdf webliance@123.45.67.89:/home/webliance/uploads/ |
→ Sends important_report.pdf from your current local folder to the remote server’s /home/webliance/uploads/ folder.
If you want same name and go to home directory:
|
0 1 2 3 4 5 6 |
scp my_script.sh ubuntu@server.example.com:~ |
~ = remote user’s home — very handy.
2. Copy a File from Remote Server to Your Laptop
Reverse direction — remote first, local second:
|
0 1 2 3 4 5 6 |
scp webliance@123.45.67.89:/var/log/nginx/error.log ~/Downloads/ |
→ Downloads the remote error log to your local Downloads folder.
Short version (current directory):
|
0 1 2 3 4 5 6 |
scp user@host:~/backup.zip . |
. = current local folder
3. Copy an Entire Folder (Must Use -r !)
Just like cp, you need -r (recursive) for directories:
|
0 1 2 3 4 5 6 |
scp -r my_project/ devuser@server.webliance.in:/var/www/my_project/ |
→ Copies the whole my_project folder (including subfolders and files) to the remote server.
Download whole folder from server:
|
0 1 2 3 4 5 6 |
scp -r ubuntu@123.45.67.89:/home/ubuntu/old_site ~/backup/old_site/ |
4. Most Useful & Important Options (Flags)
| Flag | Meaning | Example Command | When / Why to use |
|---|---|---|---|
| -r | Recursive (copy folders) | scp -r src/ user@host:dest/ | Must for directories |
| -P | Custom SSH port (capital P) | scp -P 2222 file user@host:~ | Non-default port 22 |
| -p | Preserve timestamps/permissions | scp -p file user@host:~ | Keep modification date |
| -v | Verbose (see progress & debug) | scp -v bigfile.zip user@host:~ | See what’s happening |
| -C | Compress during transfer | scp -C large.log user@host:~ | Faster on slow connections |
| -i | Use specific SSH key | scp -i ~/.ssh/my_special_key file user@host:~ | Multiple keys setup |
| -q | Quiet (no progress bar) | scp -q file user@host:~ | Scripts / cron |
| -l | Limit bandwidth (in KB/s) | scp -l 500 bigvideo.mp4 user@host:~ | Don’t kill internet |
Best everyday combo for folders:
|
0 1 2 3 4 5 6 |
scp -r -v -C my_folder/ user@host:/backup/ |
→ Recursive + verbose + compress = safe, visible, faster
5. Real-Life Examples You’ll Use in Hyderabad Life
Example 1: Upload website files to server
|
0 1 2 3 4 5 6 |
scp -r public_html/ webliance@yourdomain.com:/var/www/html/ |
Example 2: Download database backup
|
0 1 2 3 4 5 6 |
scp root@123.45.67.89:/root/backup_2026-02-26.sql.gz ~/Downloads/ |
Example 3: Copy config file and preserve permissions
|
0 1 2 3 4 5 6 |
scp -p /etc/nginx/nginx.conf user@staging.server:/etc/nginx/ |
Example 4: Copy between two remote servers (through your laptop)
|
0 1 2 3 4 5 6 |
scp user1@serverA:/data/bigfile.dat user2@serverB:/data/ |
(Your laptop acts as middleman — slower but works)
Example 5: Safe upload with custom port & key
|
0 1 2 3 4 5 6 |
scp -P 2222 -i ~/.ssh/prod_key app.tar.gz deploy@prod.example.com:/opt/app/ |
6. Practice Session Right Now (Safe & Simple)
If you have a server/VM:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# 1. Create test file locally echo "Test file from Hyderabad" > test_scp.txt # 2. Upload to remote home scp test_scp.txt webliance@your-server-ip:~ # 3. Download it back with different name scp webliance@your-server-ip:~/test_scp.txt ./downloaded_test.txt # 4. Check content cat downloaded_test.txt |
If no remote server yet — just read & visualize the commands.
7. Teacher Warnings (Very Important!)
- scp overwrites without asking by default — same as cp — be careful with destination!
- No -i flag like cp/mv — it will overwrite silently. → Always double-check paths!
- Never scp sensitive files over public WiFi without trusting the network (though SSH encrypts it)
- For huge folders → consider rsync over ssh later (smarter, resumable, delta transfer)
- Password prompt every time? → Set up SSH keys (from previous ssh lesson)!
Quick Summary Table – scp at a Glance
| Direction | Command Example | Notes |
|---|---|---|
| Local → Remote file | scp local.txt user@host:~/ | To home |
| Remote → Local folder | scp user@host:/remote/file.txt ~/Downloads/ | Download |
| Folder upload | scp -r my_folder/ user@host:/dest/ | Always -r |
| With custom port & key | scp -P 2222 -i key file user@host:~ | Secure setup |
| Preserve attributes | scp -p file user@host:~ | Timestamps/permissions |
| Compress transfer | scp -C bigfile user@host:~ | Slow connection |
Got it, boss? scp is your secure file bridge to any server in the world — once SSH keys are set up, moving files between Hyderabad and cloud servers becomes as easy as cp.
Any part confusing? Want next: “Teacher, explain rsync over ssh (better than scp)” or “how to use sftp instead” or “scp in cron jobs” or back to basic commands?
Tell your teacher — we’re building your Bash superpower step by step! 🐧🔒📤 From Hyderabad! 😄
