Copying Files Between Systems Using the scp and sftp Commands

Using scp

Note:

In Oracle Linux 9 and Oracle Linux 10, the scp utility defaults to SSH File Transfer Protocol. For more information, see the Security section under the Deprecated Features chapter in Oracle Linux 9: Release Notes for Oracle Linux 9 and the scp(1) manual page.

With the scp command, you can copy files or directories between systems. scp establishes a connection, copies the files, and then closes the connection.

The following examples show how you can use the scp command.

  • Uploading a local file to a remote system

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

    For example:

    • Copy testfile to the 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
  • Downloading a file from a remote system to the local system

    scp [options] [user@]host[:remote_file] local_file
  • Copying files recursively

    The -r option recursively copies the contents of directories.

    For example, to copy the directory remdir and its contents from the home directory on remote host04 to the local home directory, you would type:

    scp -r host04:/home/user/remdir $HOME

    Note that shell expansion of the $HOME variable can result in unintended file paths if used with the scp command to specify a path on a remote host. Always use the full path to the remote file in an scp command.

Using sftp

The sftp command is a secure alternative to the ftp command that's used to transfer files between systems. Unlike the scp command, the sftp command provides functionality for you to browse the file system on the remote server before copying any files.

To open an FTP connection to a remote system over SSH, use the following command:

sftp [options] [user@]host

For example, you would open an FTP connect to the system, host04, as follows:

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

Type sftp commands at the sftp> prompt.

In the following example, the put command is used to upload the file newfile from the local system to the remote system, then the ls command is used to list it:

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

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

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