Home / Hosting / What Is the scp Command in Linux? [Examples Provided]

What Is the scp Command in Linux? [Examples Provided]

Rapyd's Mascot - Fleet The Heartbroken Companion
Horizontal Banner
Loading the Elevenlabs Text to Speech AudioNative Player...

Moving files securely between systems isn’t always easy—especially when you’re dealing with sensitive data and can’t afford any slip-ups.. That’s where the scp command in Linux comes into play. Short for “secure copy,” scp encrypts your files during transit (thanks to the SSH protocol) so that nosy third parties can’t snoop on your data. Whether you’re migrating server configs, sending scripts to colleagues, or just moving a folder of cat memes to your VPS, scp has you covered. Below, we’ll walk through how scp works, why it’s useful, and some handy examples to kickstart your learning.


Why Use scp Instead of Other Methods?

  • Encryption Built-In: Unlike plain copy commands, scp automatically safeguards your files with SSH encryption.
  • Remote to Remote: You can shuffle files between two remote servers without downloading them to your local machine first.
  • User-Friendly: The syntax feels familiar if you’ve ever used cp for local copies—just add your SSH credentials and paths.

Basic scp Syntax

The general format looks like this:

scp [options] [source] [destination]

  • options: Special flags you can enable for debugging, limiting speed, recursive copying, etc.
  • source: Where your file currently lives (either locally or on a remote server).
  • destination: Where you want the file to end up (again, can be local or remote).

1. Copying a Local File to a Remote Server

So you’ve got a file named notes.txt on your local machine, and you want to upload it to a remote server. Here’s how:

scp /path/to/local/notes.txt username@remote_host:/remote/path/
  • Replace username with your actual username on the remote server.
  • remote_host can be an IP address or a domain name.
  • If the file is small, this will happen almost instantly; for larger files, you’ll see a progress bar.

2. Copying a Remote File to Your Local System

Sometimes you need to grab something from a server—maybe logs or a config file. Reverse the direction:

scp username@remote_host:/path/to/remote/backup.tar.gz /local/path/

Now scp will fetch backup.tar.gz from the remote server and plop it into your local /local/path/.


3. Copying a Directory (Recursively)

Transferring one file is fine, but what if you’ve got an entire directory of files you need to move? Just tack on the -r flag for recursion:

scp -r /local/directory/ username@remote_host:/remote/directory/
  • This grabs every file, folder, and subfolder nested inside /local/directory/.
  • Make sure you include the trailing slash / so that scp copies the contents instead of creating an extra subdirectory.

4. Copying Between Two Remote Systems

A lesser-known trick: you can move files between two remote hosts without touching your local machine:

scp username1@remote_host1:/path/to/file.txt username2@remote_host2:/path/to/destination/

Think of it like telling one server to pass your file directly to another. Handy if your local bandwidth is limited.


Extra Options You Should Know

Custom SSH Port with -P
If your server runs on, say, port 2222 instead of the standard 22, you can specify that like so

scp -P 2222 /local/file.txt username@remote_host:/remote/path/

Limit Bandwidth with -l
On a slow or shared connection? Avoid hogging all the bandwidth

scp -l 200 /local/file.txt username@remote_host:/remote/path/

 The 200 here means 200 Kbps—enough to get the job done without saturating the network.

Verbose Output with -v
Having trouble diagnosing a stalled transfer? Verbose mode spills all the behind-the-scenes details

scp -v /local/file.txt username@remote_host:/remote/path/

 This prints out each step, which can be a lifesaver for debugging.


Common Pitfalls and Pro Tips

  • Check Your Paths: Typos in file paths can lead to errors like “No such file or directory.” Always verify you’ve spelled everything correctly.
  • Permissions: Make sure your user account on the remote server has write permissions in the target folder.
  • SSH Keys: Setting up public key authentication (instead of password auth) makes scp both more secure and more convenient.
  • Security: Even though scp uses SSH, always keep an eye on which users have access to your server. A strong SSH setup plus consistent updates is a must for safe file transfers.

Why System Administrators Love scp

  • Reliability: It rarely fails if your SSH credentials are in order and your network is stable.
  • No Third-Party Tools: No need to install separate clients; scp comes standard on most Linux distros (and macOS).
  • Automation: You can script scp commands (with SSH keys) for automated backups, deployments, or log transfers.

Wrapping Up

When it comes to moving files around safely and easily, the scp command in Linux is a real MVP. It’s simple, gets the job done, and leans on SSH to protect your data in transit. Whether you’re archiving log files, distributing scripts, or just tinkering around with personal projects, scp is a staple tool you’ll want in your arsenal.

Ready to try it out? The next time you need to shuffle files between machines, open your terminal and give scp a spin. The secure, efficient transfer might just become your new go-to method—no extra steps, no third-party apps, just a single command that does it all. Happy copying!

author avatar
Salman
Salman Saleem is a Media Buyer at Rapyd Cloud. With expertise in WordPress, SaaS, and performance marketing, he specializes in creating and editing technical blogs. His diverse background, with a Bachelor's degree in Software Engineering and an MBA in Marketing, highlights his versatile writing abilities.
Share this article
0
Share
Shareable URL
Prev Post

What are the top 8 WordPress Mistakes [Community Comments]

Next Post

TLS vs SSL: What’s the Difference?

Leave a Reply

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

Horizontal Banner
Read next