Rapyd Cloud is now Levamo - read the announcement
Hosting

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

Salman · · 5 min read
What Is the scp Command in Linux? [Examples Provided]
Share

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.

Hosting built for WordPress

Forget server management and enjoy lightning-fast, fully managed WordPress hosting. Focus on growing your site while we handle the rest.

Try for free

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!

Frequently Asked Questions

What does the scp command do in Linux?
scp stands for secure copy. It transfers files between machines over the SSH protocol, so the data is encrypted while it is in transit. The syntax is scp followed by options, a source path, and a destination path.
How do I copy an entire folder with scp?
Add the -r flag for recursive copying, for example scp -r /local/directory/ username@remote_host:/remote/directory/. Include the trailing slash on the source so scp copies the contents rather than creating an extra subdirectory.
Can scp copy files between two remote servers?
Yes. You can give scp two remote paths, such as scp username1@remote_host1:/path/file.txt username2@remote_host2:/path/, and the file moves between the servers without being downloaded to your local machine first.
How do I use scp when SSH runs on a non-standard port?
Use the capital -P flag with the port number, for example scp -P 2222 /local/file.txt username@remote_host:/remote/path/. Note that scp uses -P for the port, unlike ssh which uses lowercase -p.
Why does my scp transfer fail with a permissions error?
The usual causes are a typo in the file path or a user account that lacks write permission in the target folder on the remote server. Verify both, and consider setting up SSH key authentication for smoother and more secure transfers.
Share

Hosting for WordPress

Put your WordPress site on Levamo

We migrate your WordPress site for you, free, with minimal downtime.

  • Free white-glove migration
  • Free 3-day trial, no risk
  • 14-day money-back guarantee
Start for free

There's More to Read

Hosting for WordPress

Put your WordPress site on Levamo

We migrate your WordPress site for you, free, with minimal downtime.

Start for free
Fleet, the Levamo mascot

Ready for a faster, more reliable WordPress site?

Start your free trial and see the difference instantly. When you're ready to move, our team handles your full migration for free - minimal downtime, fully optimized.