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

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

Copy file to remote machine using key

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

  • Example 1 send single file to a remote machine, place it at <username> home directory.

    $ scp -i /path/to/key file.txt <username>@<hostname-or-ip>:
    
  • Example 2 send single file using .pem key

    $ scp -i ~/.ssh/key.pem data.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/id_rsa.pub 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 using ssh 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

Upload file to remote machine

See above: Copy file to remote machine

Dialogue & Discussion