6.3. Copying Files Remotely

Problem

You want to copy files securely from one computer to another.

Solution

For one file:

$ scp myfile remotehost:
$ scp remotehost:myfile .

For one file, renamed:

$ scp myfile remotehost:myfilecopy
$ scp remotehost:myfile myfilecopy

For multiple files:

$ scp myfile* remotehost:
$ scp remotehost:myfile\* .

To specify another directory:

$ scp myfile* remotehost:/name/of/directory
$ scp remotehost:/name/of/directory/myfile\* .

To specify an alternate username for authentication:

$ scp myfile smith@remotehost:
$ scp smith@remotehost:myfile .

To copy a directory recursively (-r):

$ scp -r mydir remotehost:
$ scp -r remotehost:mydir .

To preserve file attributes (-p):

$ scp -p myfile* remotehost:
$ scp -p remotehost:myfile .

Discussion

The scp command has syntax very similar to that of rcp or even cp:

scp name-of-source name-of-destination

A single file may be copied to a remote file or directory. In other words, if name-of-source is a file, name-of-destination may be a file (existing or not) or a directory (which must exist).

Multiple files and directories, however, may be copied only into a directory. So, if name-of-source is two or more files, one or more directories, or a combination, then specify name-of-destination as an existing directory into which the copy will take place.

Both name-of-source and name-of-destination may have the following form, in order:

  1. The username of the account containing the file or directory, followed by “@". (Optional; permitted only ...

Get Linux Security Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.