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)

Bash

But source or destination (or both) must include the remote part in this format:

text

Examples:

  • Local → Remote
  • Remote → Local
  • Remote → Remote (less common)

1. Copy a File from Your Laptop to Remote Server (Most Common)

Bash

→ 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:

Bash

~ = remote user’s home — very handy.

2. Copy a File from Remote Server to Your Laptop

Reverse direction — remote first, local second:

Bash

→ Downloads the remote error log to your local Downloads folder.

Short version (current directory):

Bash

. = current local folder

3. Copy an Entire Folder (Must Use -r !)

Just like cp, you need -r (recursive) for directories:

Bash

→ Copies the whole my_project folder (including subfolders and files) to the remote server.

Download whole folder from server:

Bash

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:

Bash

→ Recursive + verbose + compress = safe, visible, faster

5. Real-Life Examples You’ll Use in Hyderabad Life

Example 1: Upload website files to server

Bash

Example 2: Download database backup

Bash

Example 3: Copy config file and preserve permissions

Bash

Example 4: Copy between two remote servers (through your laptop)

Bash

(Your laptop acts as middleman — slower but works)

Example 5: Safe upload with custom port & key

Bash

6. Practice Session Right Now (Safe & Simple)

If you have a server/VM:

Bash

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! 😄

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *