Linux SCP – Copy Files Between Systems

SCP (Secure Copy Protocol) is a useful command to copy files between systems, we’ll explore a few example scenarios that you’ll commonly use. I’m not going into great detail about all the various arguments and options you can use, these you can find in other articles elsewhere. Instead this is going to be a very high level and quick “cheat sheet”.

In all cases you’ll need to ensure that the user account you are using on the remote host actually has permissions to read/write/change files in the location, or the copy may fail.

Copy File from Local Host to a Remote Host (i.e. Upload)

Copy the file myfile.txt from my local host to a remote host 192.168.1.10 into the remote directory: /home/bobsmith (i.e. Bob Smith’s home directory on the remote host), logging on as the user bob.

$ scp myfile.txt bob@192.168.1.10:/home/bobsmith

Or putting that in vanilla terms:

$ scp <localfile> <username>@<hostname>:<remotedirectory>

Copy File from Remote Host to Local Host (i.e. Download)

Copy the file myfile.txt from the remote host 192.168.1.10 into my current local directory (that’s the dot “.” at the end of the command), from Bob Smith’s home directory /home/bobsmith on the remote host, and logging on as the user bob.

$ scp bob@192.168.1.10:/home/bobsmith .

Or putting that in vanilla terms:

$ scp <username>@<hostname>:<remotedirectory> <localdirectory>

Copy File from Remote Host 1 to Remote Host 2

Ok, so now for a potentially useful one where you can copy a file between two remote hosts. In this example we are copying the myfile.txt from the remote host 192.168.1.10 to the second remote host 192.168.1.11 where we are copy from Bob Smith’s home directory on each of the remote hosts. Bear in mind that you must be able to logon to both of the remote hosts in order to copy the file.

scp bob@192.168.1.10:/home/bobsmith/myfile.txt bob@192.168.1.11:/home/bobsmith/myfile.txt

In vanilla terms, that is:

scp <username>@<hostname1>:<remotedirectory> <username>@<hostname2>:<remotedirectory>

So bear in mind that since OpenSSH 8.7 that the data is actually routed through your local machine, so you are in effect logging into host1, copying the file via your machine to host2.

You may use a method whereby host1 copies direct to host2, by adding the “-R” argument, for example:

scp -R bob@192.168.1.10:/home/bobsmith/myfile.txt bob@192.168.1.11:/home/bobsmith/myfile.txt

However, be aware that for this to work, you must be able to logon to the second (destination) host without a password prompt, i.e. using an SSH key authentication.

Common Extra Options

Some useful extra options:

-r = Copy a directory and all of its contents (recursive).

$ scp -r /local/directory <username>@192.168.1.10:/remote/directory

Copy a number of files in a single command:

$ scp myfile.txt secondfile.log thirdfile.bin bob@192.168.1.10:/home/bobsmith

-p = Preserve the file’s metadata (modification time, access time etc.):

$ scp -r myfile.txt  bob@192.168.1.10:/home/bobsmith

And if you want to use SSH key for authentication, it’s as simple as specifying the key with “-i”, obviously you need to have already pre-staged the key on the remote host!

$ scp -i ~/.ssh/id_ed25519 myfile.txt  bob@192.168.1.10:/home/bobsmith

Extra Example

Here’s an extra example, if you wanted to only copy specific file types from a remote host, for example just the log files within a directory, something like the following can be used, but you need to remember that without single quoting the path, your local shell tries to pattern match.

$ scp bob@192.168.1.0:'/var/log/*.log' .

You can also escape the slashes in the pathname, that has the same effect.

Leave a comment