scp command in Linux with examples

The scp command in Linux is used to copy files and directories to or from a remote system. It works very similarly to the cp command, except that it copies files to or from other systems that are either on your local network or somewhere over the internet.

The scp command works through the SSH protocol. In other words, both systems must have SSH set up in order to use the scp command. As you may already know, the SSH protocol creates a secure and encrypted tunnel between your local system and another system, which means that files you copy with the scp command are not liable to be intercepted.

DID YOU KNOW?
The SCP command is based on the BSD RCP protocol. This protocol is used for file transfers between connected hosts on the local network or over the Internet. The actual data transfer and user authentication is done via Secure Shell (SSH). This in turn provides end-to-end encryption for all data transfers.

As you can imagine, being able to copy and paste files to or from remote systems is extremely useful. However, it’s not the only command in Linux that is capable of this. For complex file transfers or complete backups, it’s recommended you use something more like the rsync command. The scp command is more ideal for quick and simple transfers of one or a few files.

In this guide, you’ll learn how to use the scp command in Linux through examples. Follow along below to learn about the various options that you can use with this command.

In this tutorial you will learn:

  • How to use the scp command on Linux

"scp command in Linux to copy a file to a remote system

Using the scp command in Linux to copy a file to a remote system

scp command in Linux Basic Examples

  1. Let’s look at a simple example where we use the scp command to copy a local file named file.txt to a remote server with IP address 10.1.1.20.
    $ scp file.txt linuxconfig@10.1.1.20:/home/linuxconfig
    

    In this example, we are authenticating with the username linuxconfig on server 10.1.1.20. After supplying the password or authenticating with RSA keys, our file file.txt will be copied from the local system into remote directory /home/linuxconfig.

  2. If we had wanted our original file to adopt a new file name on the remote system, we can specify that in our command.
    $ scp file.txt linuxconfig@10.1.1.20:/home/linuxconfig/newname.txt
    
  3. To copy a directory instead of a file to the remote system, you will need to use the -r option in your scp command.
    $ scp -r Downloads linuxconfig@10.1.1.20:/home/linuxconfig/
    

    In this example, we are copying our local Downloads directory into the remote /home/linuxconfig/ directory.

  4. So far, we have seen how to copy files and directories from our local system to the remote system. But, what about the other way around? If we want to copy a remote file into our local system, all we need to do is reverse the order of the command, as seen below.
    $ scp linuxconfig@10.1.1.20:/home/linuxconfig/file.txt /path/to/destination
    

    In this example, we are copying remote file /home/linuxconfig/file.txt into our local directory at /path/to/destination.

NOTE
You can always use the man command to read more about the scp command and its official documentation. Click the previous link to see how to open the manual pages for any command on a Linux system.

Advanced Usage

In the section below, we’ve collected a few of the more advanced options that you can use with the scp command in Linux. Make no mistake, scp is pretty simple as far as Linux commands go. However, some of the options below are not as frequently used but still fill a certain niche and are handy to know.

scp command in Linux Advanced Examples

  1. If the remote system is not running SSH on the default port of 22, you will need to specify another port number with the -P option in your scp command. In this example, the remote system is running SSH on port 2210.
    $ scp -P 2210 file.txt linuxconfig@10.1.1.20:/home/linuxconfig
    
  2. Interestingly, you can also copy files from one remote system to yet another remote system. Just be prepared to authenticate with both servers before the transfer will initiate.
    $ scp user1@host1:/files/file.txt user2@host2:/files
    

Closing Thoughts

In this guide we have learned about the scp command. It’s a great tool to know when you already have an SSH connection between your local system and a remote server. It’s probably one of the easiest and painless ways to quickly copy files to or from a remote server. To make things even more convenient, you can always set up RSA key authentication, which will bypass the password prompt entirely while also increasing security.