scp (secure copy): reference and examples

Last updated:
Table of Contents

scp (secure copy) is a very widely used linux command for copying files across machines over the network.

You can upload files from your machine to remote servers and/or download files from remote server to your local machine, using standard SSH authentication.

Simply put, scp enables your to run cp (default copy command) to/from a remote machine, the same way ssh enables you to log in and run a shell on a remote machine.

scp is to cp as ssh is to sh

Upload file to remote server

$ scp myfile.txt <username>@<ip_or_hostname>:/path/to/directory/on/remote/server

Upload file using key

the colon (":") at the end is not a typo

$ scp -v -i <identityFileLocation> <fileLocation> <remoteUsername>@<remoteHostOrIP>:

Where all values within "<" and ">" should be replaced with your own values;

  • Example: send a single file using a .pem key

    $ scp -v -i ~/.ssh/key.pem data.txt  my_username@my_hostname:
  • Example: send a directory using a .pub key

    $ scp -r -v -i ~/.ssh/ ~/a_directory/ my_username@my_hostname:

Download file from remote server to local machine

$ scp my_username@my_hostname:foobar.txt /some/local/directory

Download file using key

$ scp -i ~/.ssh/<your_key_name> my_username@my_hostname:a_sql_dump.sql ~/backup/

Download directory recursively from remote server

Use -r modifier. -p is optional; it forces the downloaded files to keep the same "last-modified-timestamps" as the original files.

$ scp -rp my_username@my_hostname:/path/to/remote/directory/ /path/to/local/target

Dialogue & Discussion