Use SCP to securely transfer files between two Unix computers
In Unix, you can use SCP (the scp
command) to securely copy 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 requires a password or passphrase for authentication. 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
The location of the source file is specified by username1@source_host:directory1/filename1
, which includes the:
- Name of the account on the host computer (
username1
) - Hostname of the computer on which the source file resides (
source_host
) - Name of the directory containing the source file (
directory1
) - Filename of the source file (
filename1
)
The location to which the source file will be copied is specified by username2@destination_host:directory2/filename2
, which includes the:
- Name of the account on the destination computer (
username2
) - Hostname of the computer to which the source file will be copied (
destination_host
) - Name of the directory to which the source file will be copied (
directory2
) - Filename of the copy (
filename2
)
For more about scp
, consult its manual page. At the Unix prompt, enter:
man scp
At Indiana University, for personal or departmental Linux or Unix systems support, see Get help for Linux or Unix at IU.
Examples
For the following examples, assume your username is dvader
, and you are logged into your account on the computer empire.gov
:
- To copy a file called
rebels.txt
from your home directory onempire.gov
to a directory calledrevenge
in your account on the computerdeathstar.com
, enter:scp ~/rebels.txt dvader@deathstar.com:~/revenge
You'll be prompted for your password on the destination system (
deathstar.com
). The command won't work unless you enter the correct password. - To copy a directory (and all the files it contains), use
scp
with the-r
option. This tellsscp
to recursively copy the source directory and its contents.To copy the entire
revenge
directory from yourdeathstar.com
account to yourempire.gov
account, enter:scp -r dvader@deathstar.com:~/revenge ~/revenge
You'll be prompted for your password on the source system (
deathstar.com
). The command won't work unless you enter the correct password. - To copy multiple files within a directory, you can use wildcards (for example,
*
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 thescp
command, expands unquoted wildcards.Therefore, to copy all the
.txt
files from therevenge
directory on yourdeathstar.com
account to yourrevenge
directory onempire.gov
, enter:scp dvader@deathstar.com:"revenge/*.txt" ~/revenge/
You'll be prompted for your password on the source system (
deathstar.com
). The command won't work unless you enter the correct password.
For the following example, assume you (dvader
) are logged into another computer (that is, some other computer that's not empire.gov
or deathstar.com
). To copy luke.txt
from your home directory on empire.gov
to your revenge
directory on deathstar.com
, enter:
scp dvader@empire.gov:~/luke.txt dvader@deathstar.com:~/revenge
You'll be prompted to enter two passwords: one for the source system (empire.gov
) and one for the destination system (deathstar.com
). The command won't work unless you correctly enter both passwords.
This is document agye in the Knowledge Base.
Last modified on 2023-01-18 11:44:52.