SCP Command in Linux: How to Use It, with Examples

Having essential skills like securely transferring data is critical if you're pursing jobs in the field.

The scp (Secure Copy) command uses SSH to transfer data from one host to another and uses SSH’s same authentication and security. However, the command relies on SSH for data transfer, requiring an ssh key or password to authenticate on the remote systems.

When transferring data with scp, both the files and password are encrypted so that anyone snooping on the traffic doesnโ€™t get anything sensitive. So, this is one of the most secure ways to transfer data on a network.

The scp command in Linux can be used in 3 ways:

  • To copy from a remote server to a local machine.
  • Also, to copy from a local machine to a remote server.
  • To copy from a remote server to another remote server.

SCP Command Syntax

The syntax for the scp command is:

scp [OPTION] [user@]SRC_HOST:]file1 [user@]DEST_HOST:]file2Code language: CSS (css)

Some of the most widely used scp command options include:

  • -P: Specify server SSH port
  • -p: Preserves permissions, modes, and access time of files (note the lower-case)
  • -q: Quiet mode, donโ€™t display progress or messages
  • -C: Compress the data during transmission
  • -r: Recursive – include subdirectories and their contents
  • -i: Identity file or private key

How to Use the SCP Command in Linux

Transfer Local File to Remote Server

Copy file.txt from the current directory of the local system to the remote serverโ€™s /tmp directory.

scp file.txt user@remotehost:/tmp/Code language: JavaScript (javascript)

Transfer File from Remote Server to Local Machine

The following command will copy /tmp/file.txt from the remote server to the local machine under the user’s home directory.

scp user@remotehost:/tmp/file.txt /home/userCode language: JavaScript (javascript)

Transfer Local Directory to Remote Server Recursively

You can use the -r option in the scp command in Linux to recursively copy the entire directory from one system to another.

The following command will copy the /home/user/myfiles directory from the local machine to the remote serverโ€™s /tmp directory.

scp -r /home/user/myfiles user@remotehost:/tmp/Code language: JavaScript (javascript)

Transfer Directory from Remote Server to Local Recursively

The following command will copy /tmp/serverfiles directory from the remote server to the local machine under the user’s home directory recursively.

scp -r user@remotehost:/tmp/serverfiles /home/userCode language: JavaScript (javascript)

Transfer Multiple Files to Remote Servers

In the following example, the files file1.txt and file2.txt from the source host are copied to the remote server’s /tmp directory.

scp file1.txt file2.txt user@remotehost:/tmp/Code language: JavaScript (javascript)

Increase Transfer Speed by Enabling Compression

You can increase the transfer speed by enabling the compression using the -C option. It will automatically allow compression at the source and decompression at the destination host.

The following command will copy the /home/user/myfiles directory from the local machine to the remote serverโ€™s /tmp directory recursively with enabled compression.

scp -r -C /home/user/myfiles user@remotehost:/tmp/Code language: JavaScript (javascript)

Specify Different SSH Port

There can be cases where the SSH port is changed on the destination host, so using the scp command in Linux, you can specify the SSH port number using the -P option.

The following command will copy file.txt from the current directory of the local system to the remote serverโ€™s /tmp directory using port 2222.

scp -P 2222 file.txt user@remotehost:/tmp/Code language: JavaScript (javascript)

Preserves Permissions, Modes, and Access Time of Files

Use the -p option in the scp command to preserve permissions, access time, and modes while copying files.

The following command will copy file.txt from the current directory of the local system to the remote serverโ€™s /tmp directory and will keep its properties.

scp -p file.txt user@remotehost:/tmp/Code language: JavaScript (javascript)

Use Identify File in SCP Command

When using an SSH Key instead of a password during the SSH session, the -i flag allows you to select the file from which the identity (private key) for public-key authentication is read.

The following command will copy file.txt from the current directory of the local system to the remote serverโ€™s /tmp directory using the my_second_indent.pem private key file.

scp -i my_second_indent.pem file.txt user@remotehost:/tmp/Code language: JavaScript (javascript)

Conclusion

In this tutorial, you learned how to use the scp command on Linux to copy files and directories. This is especially useful as a replacement for FTP, which is inherently insecure by default.

You may also want to set up an SSH key-based authentication and connect to your Linux servers without entering a password.

For more about the scp command in Linux, consult its manual page.

Bobby Borisov

Bobby Borisov

Bobby, an editor-in-chief at Linuxiac, is a Linux professional with over 20 years of experience. With a strong focus on Linux and open-source software, he has worked as a Senior Linux System Administrator, Software Developer, and DevOps Engineer for small and large multinational companies.

Think You're an Ubuntu Expert? Let's Find Out!

Put your knowledge to the test in our lightning-fast Ubuntu quiz!
Ten questions to challenge yourself to see if you're a Linux legend or just a penguin in the making.

1 / 10

Ubuntu is an ancient African word that means:

2 / 10

Who is the Ubuntu's founder?

3 / 10

What year was the first official Ubuntu release?

4 / 10

What does the Ubuntu logo symbolize?

5 / 10

What package format does Ubuntu use for installing software?

6 / 10

When are Ubuntu's LTS versions released?

7 / 10

What is Unity?

8 / 10

What are Ubuntu versions named after?

9 / 10

What's Ubuntu Core?

10 / 10

Which Ubuntu version is Snap introduced?

The average score is 69%