Transfer files to and from linux using secure copy (scp)

Published: {ts '2014-02-11 00:00:00'}
Author: Steven Neiland
Site Url: http://www.neiland.net/article/transfer-files-to-and-from-linux-using-secure-copy-scp/

Based on the Secure Shell (SSH) protocol, Secure Copy (SCP) is a method for transferring files securely between two computers. You can transfer between a local host and a remote host, or between two remote hosts.

The format of the scp command is as follows:

scp [flags] [source location] [destination location]

Download from remote

The first use for scp is obviously to download a file from a remote server.

scp remoteuser@remotehost:remotefile localfile

Upload to remote

The exact opposite of downloading, uploading just reverses the order of the source,destination paths.

scp localfile remoteuser@remotehost:remotefile

Copy between two remote machines

In this scenario your local machine only tells the two remote hosts to perform the transfer. The data itself is transferred directly between the two remote hosts. For this reason it is important that they are able to communicate directly to each other.

scp remoteuser1@remotehost1:remotefile1 remoteuser2@remotehost2:remotefile2

Tips

In addition to the basic operations, here are some tips and tricks for using scp.

Single and double dots

This is more of a general tip but a useful one regardless.

Using a single dot (.) in place of a file path is equivalent to specifying the current local directory. In addition specifying two dots (..) gives the parent directory of the current local directory.

SCP supports asterix

No not the little viking dude. Like ssh you can use the asterix symbol to specify all files within a directory like this.

//copies all files inside "/some/directory/" to the current local directory scp remoteuser@remotehost:/some/directory/* .

SCP supports recursive operation

In addition to supporting the asterix, you can also specify the "-r" recursive flag to recursively copy all files and directories with the specified directory. scp -r remoteuser@remotehost:/some/directory/* .

Use a different port number

While not too common, it may happen that you need to use scp on a host that has it configured to be on a different port number. In this situation you use the "-P" flag to specify the port number to use.

Important

Unlike ssh, we use the uppercase "-P" flag not the lowercase "-p", and this flag must be directly in front of the (remote) host. The reason for this is because scp can be used to transfer between two remote hosts it needs to know which host is operating on with port number.

scp -P newport remoteuser@remotehost:/some/directory/* .