Transfer files using scp

2013-10-01

scp File Transfers

scp is an excellent command line program for secure file transfer through the SSH protocol, and it is distributed together with SSH on (most?) modern Unix based distributions, including OSX and Ubuntu. It is especially useful for file transfers when you are remote controlling a machine without any graphical user interface.

The basic syntax of the program is as following:

scp [flags] <source> <destination>

A more specific syntax:

scp [flags] [<source username>@<source ip/domain>:]<source directory/file> <destination username>@<destination ip/domain>:<destination directory/file></pre>

Things betwen the brackets [] are optional. All standard Unix file system conventions can be used for the directories and files.

Example: transferring files

Transfer file from local machine to an external machine

scp ./my/local/source/file.txt username@externalmachine:/destination/directory/

Transfer file from external machine to local machine

scp username@externalmachine:./source/file.txt ./my/local/destination

Example: transferring directories

Transfer directory from local machine to an external machine:

scp -r ./my/local/directory username@externalmachine:/destination/directory/

Transfer directory from external machine to local machine:

scp -r username@externalmachine:./source/directory ./my/local/destination

The -r option stands for «recursive» and ensures a recursive copy of the entire directory, including sub-directories.

Transferring between two external machines

scp can also transfer files between two remote/external hosts. The following illustrates a simple file transfer between two external machines, combine it with the previous examples to transfer directories.

scp user1@sourcemachine:~/source/file.c user2@destinationmachine:/destination/

This does however require that the source machine can connect to destination machine through SSH without a password prompt. It can be done by using SSH keys for authentication, or by having the same user with the same password on both machines. This is because the command (under the hood) creates a SSH connection to the first remote machine and invokes the scp command there.

Limiting bandwidth

The -l option allows you to limit the bandwidth usage of the transfer, which can be useful on limited connections and when transferring huge files. The following example limits the file to a maximum of 8 kilobits (1 kilobyte) per second:

scp -l 8 ./my/local/source/file.txt username@externalmachine:/destination/directory/

Want more?

Check out the manual; open a terminal and type «man scp». It might be too cryptic for some, which you in that case should Google it. I recommend using FileZilla if you're not custom to the terminal and command line programs, and you're looking for a SSH/FTP/Unix file transfer program with a GUI.