How to Use SCP Command to Securely Transfer Files

SCP is a Linux command-line utility that provides functionality for securely copying data between machines. The name of the utility stands for "secure copy" and you can use it to both download and upload files between computers on a local network or to a remote server.


This tool is most commonly used for transferring files securely from your local development environment to a remote web server, so in this tutorial, we will cover how to do this among other things.


SCP Syntax


Firstly let's have a look at the SCP command syntax, which you can get by typing:

scp ?
usage: scp [-346BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]
   [-l limit] [-o ssh_option] [-P port] [-S program] source ... target


We have our options followed by the location of the source and finally the destination:

scp [options] [user@source] [user@target]


Here are some of the most common scp options:


  • -r recursively copy directoy contents
  • -p port number to connect on
  • -C compress files as they are sent to the target
  • -p keep file last modification time


Copy a Single Local File to a Remote Directory


The most basic way to use scp is to copy a single file to a remote location. We can do this using this following command:


scp file.txt [email protected]:/var/www/


After pressing enter you will be prompted to type in your password. Warning: check that you will not be overwriting files at the destination as scp will do this without prompting you.


Firstly we pass the filename in and then the username at the remote server address. The colon indicates the beginning of the target path which will start at the root path.


You will get a permission denied error if you don't have read permissions on your local directory and write permissions on the remote directory.


If the remote machine is only listening on something other than port 22 you can pass in the -P option to specify it:


scp -P 8000 file.txt [email protected]:/var/www/


Copy Directories and all Files from Remote to Local


To copy directories and the files within them you will need to pass the -r option into scp command, which tells scp to recursively copy everything.


scp -r directory1 directory2 file1.mp4 [email protected]:/var/www/


Once again just be careful to ensure you are not going to overwrite any files at the destination you don't intend to.


Copy a Remote File to a Local Directory


To copy files from a remote server to you local machine you just have to pass the login and location of the files and then the directory the should be downloaded to.


scp -r [email protected]:/var/www/ local/directory


Copy Files between Two Remote Servers


To copy between two remote machines you can pass to sets of logins for each server. The first being the source and the second being the target.


scp -r [email protected]:/source/directory [email protected]:/target/directory


You will be prompted to enter passwords for both servers after hitting enter.


If you would like the data to be transferred VIA your machine you can pass in the -3 flag.


scp -3 -r [email protected]:/source/directory [email protected]:/target/directory
server terminal