[ Team LiB ] Previous Section Next Section

Recipe 6.3 Copying Files Remotely

6.3.1 Problem

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

6.3.2 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 .

6.3.3 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 if a hostname is specified.) If omitted, the value is the username of the user invoking scp.

  2. The hostname of the host containing the file or directory, followed by a colon. (Optional if the path is present.) If omitted, the local host is assumed.

  3. The path to the file or directory. Relative pathnames are assumed relative to the default directory, which is the current directory (for local paths) or the remote user's home directory (for remote paths). If omitted entirely, the path is assumed to be the default directory.

Although each of the fields is optional, you cannot omit them all at the same time, yielding the empty string. Either the hostname (item 2) or the directory path (item 3) must be present.

Whew! Once you get the hang of it, scp is pretty easy to use, and most scp commands you invoke will probably be pretty basic. If you prefer a more interactive interface, try sftp , which resembles ftp.

If you want to "mirror" a set of files securely between machines, you could use scp -pr, but it has disadvantages:

  • scp follows symbolic links automatically, which you might not want.

  • scp copies every file in its entirety, even if they already exist on the mirror machine, which is inefficient.

A better alternative is rsync with ssh, which optimizes the transfer in various ways and needn't follow symbolic links:

$ rsync -a -e ssh mydir remotehost:otherdir

Add -v and —progress for more verbose output:

$ rsync -a -e ssh -v --progress mydir remotehost:otherdir

6.3.4 See Also

scp(1), sftp(1), rcp(1), rsync(1).

    [ Team LiB ] Previous Section Next Section