How to transfer files using SCP command

How to transfer files using SCP command


SCP command is a powerful tool shipped with Linux and macOS distributions. If you landed on this page, chances are you've used the scp command before and you're looking for a quick syntax reference. Here's a quick syntax you can use:

scp [options] [user@]origin_host:]path/to/origin/file [user@]destination_host]:path/on/destination

Continue reading if you wish to deep dive into the scp command capabilities.

SCP Overview

The word scp stands for Secure Copy Protocol which is a command-line tool that you can use via a shell program such as Terminal. It behaves in a similar manner to the more familiar cp "copy" command, but scp is used to copy files from one host machine to another while the cp command is used to copy files on the same machine.

As the name suggests, you can safely assume that all files transmitted through scp command are encrypted along with any passwords provided - in case you're worried about anyone sniffing packets on your network.

With that said, you can expect three use cases for the scp command:

  • Copy files from your local machine to a remote machine
  • Copy files from a remote machine to your local machine
  • Copy files from a remote machine to another remote machine

Notice that I always mention the word "files" but not "directories" as there is no special command to copy directories. A directory is copied by repeating the scp command as many times as the number of files in said directory. We'll explore this later in this tutorial.


The scp command requires ssh to be installed on both machines. This also means that you'll need to have the password to the remote host or have the private SSH-Key for it readily available.

Additionally, be mindful of the destination you're copying to as scp will overwrite any file with the same name without a warning.

SCP Options

Here are some of the most commonly used options when using the scp command:

  • -r : Short for recursive. Allows scp to repeat itself as many times as there are files in the source location. This is probably the most used option as you attempt to copy directories instead of files.
  • -i : To specify the private key file to a remote machine in order to copy files without using a password. Make sure to specify the path to key file right after this option.
  • -p : Preserves the original file metadata such as when it was last modified. Without this option a brand new file will be created in the destination and will appear to be last modified at the time when you executed the scp command.
  • -q : Short for quiet. Using this option will remove all progress information and any other information except for error messages.
  • -P : Use this option to specify a host ssh port in case it uses a port other than the default 22.
  • -C : Short for compression. Use this option to compress the data while it's being transmitted in case you're copying large files.
  • -3 : To route the traffic through the machine that executed the command.

Usage Examples

We're going to explore 5 practical scenarios of the use of scp. We'll begin by exploring the most basic scenario of copying a single file and slowly moving our way up to more advanced scenarios:

  • Copying a single file from your local machine to a remote machine
  • Copying a single file from a remote machine to your local machine
  • Copying a single file from one remote machine to another, directly or through your local machine
  • Copying multiple files or an entire directory using a single command
  • Using SSH-Keys to prevent SCP from asking for passwords every time

Copying a file from your local machine to a remote host

Let's start with the simplest example of copying a single file from your home directory to a remote host destination. The command syntax would be something like this:

scp /path/to/file user@remote_host:path/to/file

scp ~/Documents/example.txt dexter@

If either origin or destination host requires a password you'll be asked to enter the password upon executing the command unless you've setup SSH-Keys between the two systems to allow for a password-less login.

After hitting the 'Enter' key, upon entering the remote host password, you should see something as follows:

dexter@'s password:
example.txt                             100%    0     0.0KB/s   00:00

The original file name will change when you specify a file name at the end of the destination path (the last part of the command). If you end your destination with a / to indicate a directory instead, the file will then be copied with the same name.

The part after the : in the remote host IP address indicates a remote location which starts from the home folder of the given user. In this example, the user is "dexter" and its home folder is located in /home/dexter on an Ubuntu OS. Copying a file without specifying a location using the : sign would result in the file being copied to the home folder of the user.

Copying a file from a remote host to your local machine

This operation is very similar to the previous example, all we need to do is to swap the source and the destination location in the command:

scp user@remote_host:path/to/file /path/to/file

scp dexter@ ~/Documents/example.txt

Similarly, you'll be prompted to enter the remote host password, and upon hitting 'Enter' the resulting output would be similar.

Copying a file from one remote host to another

This use case can go in two ways depending on the options your specify. When no option is specified, the file will be transmitted from the source host to the destination host directly as follows:

scp [user@]remote_host:]path/to/file [user@]remote_host:]/path/to/file

scp dexter@ dexter@

When hitting 'Enter' for such command, you’ll be prompted to enter both host passwords, beginning with the source host password followed by destination host password.

The other scenario involves transmitting the file through the machine on which the command is executed (the file will not be stored on the issuing machine). This can be useful when copying a file between two machines on two separate networks that are invisible to each other but are accessible through the machine that is executing the command. To do so, you can use the -3 option as follows:

scp -3 dexter@ dexter@

Copying a directory

To copy an entire directory containing multiple files, you can use any of the examples above with the addition of the -r option to instruct the scp command to repeat itself as many times as there are files in said directory. Without it, you will receive an error message telling you that the source file is a directory instead of a file.

scp -r /path/to/directory/ user@remote_host:path/to/destination/

scp -r ~/Documents/ dexter@


dexter@'s password:
file_01.txt                             100%    0     0.0KB/s   00:00
file_02.txt                             100%    0     0.0KB/s   00:00
file_03.txt                             100%    0     0.0KB/s   00:00
file_04.txt                             100%    0     0.0KB/s   00:00

Copying files using an SSH-Key

All the examples above would prompt you to enter the remote host password which can be time consuming especially when using strong passwords. If you happen to have the private key of the remote machine, you can use the -i option to specify its location and this will stop scp from asking for the remote host password:

scp -i /path/to/key /path/to/file user@remote_host:path/to/file

scp -i ~/keys/remote_host_key ~/Documents/example.txt dexter@

Alternatively, if you've setup authorized_keys you can escape passwords and keys altogether when using scp command.


We've learnt that scp command is a command-line tool used in Linux systems. In this tutorial, we explored the most popular use cases of the scp command to copy both files and directories from one host machine to another.

Do keep in mind that scp will overwrite any files with the same name without any warning so be careful when specifying the destination you're copying to.

If you're going to repeat this operation frequently between two hosts, I recommend setting up ssh-keys to avoid keying in passwords repeatedly.

Was this page helpful?

Most Viewed Articles