scp (secure copy): Reference and Examples

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

Copy file to remote machine

To upload a file from your local machine to a remote server:

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

Copy file to remote machine, use key

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

  • Example 1 using RSA key.

    $ scp -i ~/.ssh/id_rsa file.txt <username>@<hostname-or-ip>:
  • Example 2 using PEM key

    $ scp -i ~/.ssh/key.pem file.txt <username>@<hostname-or-ip>:

Copy directory to remote machine, use key

Example send my-dir/ using .pub key, placing it at

$ scp -r -i ~/.ssh/ my-dir/ <username>@<hostname-or-ip>:

Copy file from remote machine

Example copy file foobar.txt to /some/local/directory

$ scp <username>@<username>@<hostname-or-ip>:foobar.txt /some/local/directory

Copy file from remote machine, use key

Example copy remote_file.txt on remote machine to local directory /path/to/dir/

$ scp -i ~/.ssh/<your_key_name> <username>@<hostname>:remote_file.txt /path/to/dir/

Copy directory from remote machine, recursively

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