Copying Files With Secure Copy

The secure copy command (run using scp) is a Linux command that allows the transfer of files between two computers. This can be locally to a remote server, from a remote server to a local location, or even between two remote servers.

When copying to or from a remote host scp uses ssh for the data transfer. This means that authentication is required, but the files are copied in a secure fashion. When starting a scp request the command first sets up an ssh connection to the remote location, which is then used for the rest of the transfer.

It is also possible to copy the files on a local hard drive, but in this case you should probably use the standard cp command.

Secure copy is an easy command to use and has a pretty straightforward syntax. I tend to use scp more than other remote file copying tools (like rsync) as I can easily remember the flags and syntax for the command. It is also much more secure than other transfer mechanisms like the insecure FTP.

scp has the syntax of the 'source' followed by the 'destination'. Either the target or the destination can be written as a remote reference. For example, to copy a local file to a remote server your would use the following.

sci source.txt user@host:/destination/directory/target.txt

Using the above will also change the name of the file from source.txt to target.txt so care must be taken if you want the file to have the same name. An alternative to the above mechanism is to leave out the target filename, this will then use the original filename specified in the copu.

sci source.txt user@host:/destination/directory

When copying to a remote server it is also a good idea to use the full directory of the destination. The default directory used is the ssh user's home directory. A full path is needed to set the destination to be elsewhere.

If you want to copy a file from a remote server to a local directory you just need to put the remote part first.

scp user@host:/source/directory/source.txt target.txt

To copy the contents of an entire directory use the -r flag. This recursively copies the directory and any sub-directories. This will copy the source directory into the destination directory and so you don't need to add the directory name here.

scp -r source/directory user@host:/destination

If your ssh server is using a non-standard port then you can use the -P flag to let scp know what port to use for the remote connection. The following uses the port 6001 to connect to the remote host.

scp -P 6001 source.txt user@host:/destination/source.txt

Note that the port flag is a capital 'P' because the lower case -p flag is used to preserve the modification times, access times and modes from the original file.

When you run a scp command each file that is copied will be given a progress meeter showing the time left to complete the transfer. This is useful for larger files but is pointless when copying lots of smaller files as each files progress meeter will only be seen for an instant. To turn off the progress meeter use the -q flag.

If you want to save on bandwidth you have the option of limiting the bandwidth to a citation threshold with the -l flag (specified in Kbit/s). Alternatively, you can try to allow compression to be used on the files before they are transmitted with the -C flag, although not all versions of ssh support this.

These are the basic controls for using the scp command. There are a few more flags available that control how the ssh connection is used and are only really useful if you have a special ssh setup.

Add new comment

The content of this field is kept private and will not be shown publicly.
1 + 3 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.