Using SCP to copy / transfer files

You are here:
< Back

Description

In Unix or Linux, SCP (the scp command) securely copies files and directories between remote hosts without starting an FTP session or logging into the remote systems explicitly. The scp command uses SSH to transfer data, so it encrypts the communication between the endpoints.  A password or passphrase is required for authentication, but unlike rcp or FTP, scp encrypts both the file and any passwords exchanged so that anyone snooping on the network cannot view them.

Syntax

The syntax for the scp command is:

scp [options] username1@source_host:directory1/filename1 username2@destination_host:directory2/filename2

where the location of the source file is specified by:

username1@source_host:directory1/filename1

The components of the source location section of the command are explained below.

  • username1 is the name of the account on the host computer
  • source_host is the hostname of the computer on which the source file resides
  • directory1 is the name of the directory containing the source file
  • filename1 is the filename of the source file

and the location where the file will be copied is specified by:

username2@destination_host:directory2/filename2

The components of the destination location section of the command are explained below and are similar to the source location section.

  • username2 Name of the account on the destination computer
  • destination_host Hostname of the computer to which the source file will be copied
  • directory2 Name of the directory to which the source file will be copied
  • filename2 Filename of the copy

Note: Make sure to include a space between the source and destination paths. Also, be careful when copying files that share the same name on both hosts; you may accidently overwrite data you intended to keep.

For more about scp, consult its manual page. At the command prompt, enter the following:

man scp

Examples

For the following examples, assume your username is myuser, and you are logged into your account on the computer server1.com:

To copy a file called myfile.txt from your home directory on server1.com to a directory called data in your account on the computer server2.com, the following would be entered:

scp ~/testfile.txt myuser@server2.com:~/data

A prompt for the password for myuser on the destination system (server2.com) will be displayed.  The command won’t work unless the user account exists on the target server and the correct password is entered.

To copy a directory (and all the files it contains), use scp with the -r option. This tells scp to recursively copy the source directory and its contents to the destination.  To copy the entire data directory from server2.com to server1.com, enter the following command:

scp -r myuser@server2.com:~/data ~/data

A prompt for the password for myuser on the source system (server2.com) will be displayed. The command will only be successful if the user account myuser exists and the correct password is entered.  Also, notice the source server was the remote system in this scenario.

To copy multiple files within a directory, you can use wildcards (e.g.,  *  or  ? ). However, to use wildcards for copying multiple source files from a remote system, you need to place quotes ( ” ” ) around the path to the source files. This is necessary because the Unix shell, not the scp command, expands unquoted wildcards.  Therefore, to copy all the .txt files from the data directory on your server2.com account to your data directory on server1.com, enter:

scp myuser@server2.com:"data/*.txt" ~/data/

Again, a prompt for the password on the source system (server2.com) will be displayed. The command will only be successful if the user account myuser exists and the correct password is entered.

The following example assumes the account (myuser) is logged into another computer (i.e., a workstation for example or any system not server1.com or server2.com). To copy example.txt from the home directory on server1.com to the data directory on server2.com, enter:

scp user1@server1.com:~/example.txt user1@server2.com:~/data

A prompt for both passwords will be displayed: one for the source system (server1.com) and one for the destination system (server2.com). The command will only be successful if the user account user1 exists on both systems and the password is entered correctly for both.

scp can be used with ssh keys to automate the process of authenticating to the servers.  See Setting up SSH Public Key Authentication in Linux for details on SSH key use.

Last Updated On October 24, 2017