27.5.2 Using scp and sftp to Copy Files Between Systems

The scp command allows you to copy files or directories between systems. scp establishes a connection, copies the files, and then closes the connection.

To upload a local file to a remote system:

$ scp [options] local_file [user@]host[:remote_file]

For example, copy testfile to your home directory on host04:

$ scp testfile host04

Copy testfile to the same directory but change its name to new_testfile:

$ scp testfile host04:new_testfile

To download a file from a remote system to the local system:

$ scp [options] [user@]host[:remote_file] local_file

The -r option allows you to recursively copy the contents of directories. For example, copy the directory remdir and its contents from your home directory on remote host04 to your local home directory:

$ scp -r host04:~/remdir ~

The sftp command is a secure alternative to ftp for file transfer between systems. Unlike scp, sftp allows you to browse the file system on the remote server before you copy any files.

To open an FTP connection to a remote system over SSH:

$ sftp [options] [user@]host

For example:

$ sftp host04
Connecting to host04...
guest@host04’s password: password
sftp>

Enter sftp commands at the sftp> prompt. For example, use put to upload the file newfile from the local system to the remote system and ls to list it:

sftp> put newfile
Uploading newfile to /home/guest/newfile
foo                                           100% 1198     1.2KB/s   00:01    
sftp> ls foo
foo        

Enter help or ? to display a list of available commands. Enter bye, exit, or quit to close the connection and exit sftp.

For more information, see the ssh(1) and sftp(1) manual pages.