Using SCP to Copy and Securely Transfer Files and Folders - Stack Abuse

Using SCP to Copy and Securely Transfer Files and Folders


SCP stands for Secure Copy Protocol. It is a tool that can be used to transfer files from a local host to a remote host, from a remote host to a local host, or between two remote hosts. In this article, we'll examine how to use SCP to copy between local and remote hosts.

SCP is almost exclusively run from the command-line using the scp command. It uses the ssh (Secure Shell) to transfer data to and from remote hosts. As such, it has a set of options that specify the authentication parameters, hosts and port like SSH.

By default, the SCP protocol operates on port 22 unless overridden by a command-line option. All scp commands follow the form:


Let's look at how SCP allows us to transfer a file on our local computer to a remote one.

Transferring a Local File to a Remote Destination

Uploading a file from our local computer to a remote location is a common scenario for IT professionals. With scp, we can accomplish this with a command like:

$ scp path/to/local/file.ext [email protected]:path/to/remote/file.ext

This command will copy the local file file.ext to the specified path (after the colon) on the remote-host.

  • The user supplied in the command is the username. The username has to belong to a user of the remote machine.
  • The remote-host supplied is the domain name or the IP address of the remote machine we are trying to connect to.
  • We then specify where we want to copy the file to on the remote machine after the colon (:).

After running this command, a prompt will display for the password corresponding to the remote host's user account:

$ [email protected]'s password:

Once the password is entered, the file will be copied.

This user account must have access to the remote path specified in the command. If you can't use your credentials to log in remotely with ssh, then those credentials will not work when using scp.

Now that we know how to transfer a remote file to our local machine, let's discuss the reverse scenario - transferring a file from a remote host to the local host.

Transferring a Remote File to a Local Destination

In a very similar fashion, we can copy a file from a remote computer to our local machine:

$ scp [email protected]:path/to/remote/file.ext path/to/local/file.ext

This works essentially in the same way, except that the remote user, host, and path are now specified before the local path.

A prompt will still be displayed for the password belonging to the remote host's user account. But, when complete, you should have a new file in the folder you specified.

Let's have a look at transferring a file between two remote hosts.

Transferring a Remote File to a Remote Destination

Finally, the following command format is used to transfer a file between two remote hosts:

$ scp [email protected]:path/to/remote/file.ext [email protected]:path/to/remote/file.ext

Notice that in this case, two remote users must be specified. Each has to have access to their respective remote server. A password prompt will be presented to accept login credentials for each user.

From all these variations of file copying we see that the scp command is flexible on what can be the source or destination path. This versatility makes it very useful for scripts.

We may transfer files between our local and remote machines to test and update the server or application configurations. We may transfer files between our main remote host and a backup server with scp. The simplicity and flexibility of SCP has made it popular with system administrators.

The SCP command also has configurations for greater flexibility in what and how we copy. Let's see how we can use command-line options to modify its behavior.

Common SCP Command-Line Options

The scp command has some useful options (also known as flags) that can alter aspects of how it connects to a remote host.

Changing the Port

As mentioned before, SCP operates on port 22 by default. However, this can be overridden by supplying the -P flag, followed by the port number.

Git Essentials

Check out this hands-on, practical guide to learning Git, with best-practices and industry-accepted standards. Stop Googling Git commands and actually learn it!

This is how we copy a file to a remote destination connecting to port 44 instead of 22:

$ scp -P 44 path/to/local/file.ext [email protected]:path/to/remote/file.ext

Modification/Access Time

The difference between the -P and -p flag is worth noting. The -p flag preserves the file modification times, access times, and modes while transferring. This can be useful if keeping the file properties unchanged is desired:

$ scp -p path/to/local/file.ext [email protected]:path/to/remote/file.ext

Copying Directories

The -r flag can be used to recursively copy a folder and its contents instead of a single file:

$ scp -r path/to/local/folder [email protected]:path/to/remote/folder

This is cleaner than copying files from the folder using the * or ? wildcards.

Supressed Mode

The -q flag supresses the progress meter and non-error messages so your terminal stays clean:

$ scp path/to/local/file.ext [email protected]:path/to/remote/file.ext
file.ext                                100%    0     0.0KB/s   00:00

$ scp -q path/to/local/file.ext [email protected]:path/to/remote/file.ext

Notice that the second command we ran in this example, the transfer percentage is not displayed due to the -q flag.

Authentication Keypair File

The -i flag can be used to authenticate the connection using a cryptographic keypair stored in a file instead of a username and password. This is common practice for authenticating against remote cloud servers, like those in AWS or Digital Ocean.

You can specify a keypair file like this:

$ scp -i path/to/local/keypair.pem path/to/local/file.ext [email protected]:path/to/remote/file.ext

Using Multiple SCP Options

Flags can be used with each other as well. Here is an example that implements multiple flags to copy a folder from a remote host to our local machine using a keypair file for authentication on port 44, while preserving file properties and supressing output:

$ scp -p -q -P 44 -i path/to/local/keypair.pem -r path/to/local/folder [email protected]:path/to/remote/folder

With this foundation, you're equipped to use SCP for a variety of situations!


In this article, we discussed SCP, a protocol which can be used to conveniently transfer files between hosts. We covered transferring files from the local host to a remote host, from a remote host to the local host, and between two remote hosts.

We also touched on a few important command-line options which may be used in specific scenarios.

About the Author

This article was written by Jacob Stopak, a software developer and consultant with passion for helping others improve their lives through code. Jacob is the author of the Coding Essentials Guidebook for Developers, an introductory book that covers essential coding concepts and tools. It contains chapters on basic computer architecture, the Internet, Command Line, HTML, CSS, JavaScript, Python, Java, databases/SQL, Git, and more.

Last Updated: July 9th, 2020

Improve your dev skills!

Get tutorials, guides, and dev jobs in your inbox.

No spam ever. Unsubscribe at any time. Read our Privacy Policy.

Jacob StopakAuthor

Jacob Stopak is a software developer and creator of - a site dedicated to teaching people how popular programs are coded. Its main project helps people learn Git at the code level.

Want a remote job?

    Prepping for an interview?

    • Improve your skills by solving one coding problem every day
    • Get the solutions the next morning via email
    • Practice on actual problems asked by top companies, like:

    Git Essentials

    Check out this hands-on, practical guide to learning Git, with best-practices and industry-accepted standards. Stop Googling Git commands and actually learn it!

    © 2013-2021 Stack Abuse. All rights reserved.