Linux Commands

How to Use SCP Command in Linux

Linux and Unix systems are well-known for their security and allow the transfer of files securely. You can use its SCP (secure copy) command-line tool for transferring the files and directories across the system in a secure manner. The SCP command will create an ssh connection in the backend while copying the files/directories from the local system to the remote system or vice versa. It means that the SCP works on the same mechanism as the SSH security, like password-protected or authentication using keys.

SCP commands are used effortlessly on Linux/Unix systems. You can use the SCP command in many cases; we have mentioned some practical examples.

Syntax of SCP Command

You can use the below syntax while running the SCP commands on the command-line tool. The syntax below helps transfer the required files or directories from the local device to the remote system.

# scp <options> <files_or_directories> user@target_host:/<folder>

The syntax below helps to transfer the files or directories from the remote location to the local system.

# scp <options> user@target_host:/files   <folder_local_system>

SCP Command Options

Following are some options are used along with the SCP command for various use.

  • -C this option enables the Compression
  • -i this option will help in identifying File or private key
  • -l this option will help in limiting the bandwidth while copying
  • -P this option will help in specifying the specific ssh port number of the target host
  • -p this option will help in preserving the required permissions, modes, and access time for the desired files while copying
  • -q This option will help in suppressing the warning message of SSH
  • -r this option will help in copying the files and directories recursively
  • -v This option will provide the verbose output

Example of SCP Command

1. Copying Required Files or Directories From the Local Device to the Remote Device

Suppose you want to copy the existing jdk rpm package from the local device to the remote device, then run the below command.

scp jdk-linux-x64_bin.rpm root@170.20.10.8:/opt

This command will copy the package file to the remote system under the /opt folder.

2. Copy Files From Remote to the Local System.

Run the below-mentioned command to copy the file from the remote device to the local device.

scp root@172.20.10.8:/root/Techical-Doc-RHS.odt /tmp

You can confirm the file transfer using the below command:

ls -l /tmp/Technical-Doc-RHS.odt

3. Getting Verbose Output During File Transfer

While using the SCP command, one can get the verbose output stating every step and understand what is happening in the background. Such output will help in troubleshooting the error, debugging, and fixing the error. Use the below-mentioned command or syntax for getting the verbose output on the screen.

scp -v jdk-linux-x64_bin.rpm root@170.20.10.8:/opt

4. Transferring Multiple Files to the Remote System Simultaneously

You can use the SCP command for transferring or copying several files at a single time. For this, you need to specify various file names separated by space. Use the below example for doing so.

scp install.txt index.html jdk-linux-x64_bin.rpm \root@172.20.10.8:/mnt

5. Transferring Files Across Two Remote Systems.

Using SCP will allow you to transfer a file across two remote systems. For that, use a Linux system connected to two other remote systems to arrange the transfer of files. Use the below syntax for doing so.

# scp user@remote_hosts1:/<files_to_transfer>  user@remote_host2:/<folder>

6. Copying Files or Directories Recursively

You can specify the “-r” option along with the copy command if you want to recursively transfer the required files or directories from one system to another over the network. You can consider the below example.

scp -r Downloads root@172.20.10.8:/opt

Run the below command to confirm the transfer.

ssh root@172.20.10.8 “ls -ld /opt/Downloads”

7. Increasing File Transfer Speed.

Use the “-c” option for compressing the files or folders for speeding up the transfer. It will automatically compress the transferring files at the source and decompress the files at the destination system.

scp -r -C Downloads root@172.20.10.8:/opt

8. Limit the bandwidth of the network while transferring or copying the file.

Use the SCP command and the “-l” option to limit the bandwidth while copying. You can specify the bandwidth in Kbit/s. Consider the below example for doing so.

scp -l 500 jdk-linux-x64_bin.rpm root@170.20.10.8:/var

9. Specifying Different SSH Port

You can specify the different ssh ports using the SCP command with the help of the “-P” options that will specify the port. Here we are specifying the port as “2022”.

scp -P 2022 jdk-linux-x64_bin.rpm root@170.20.10.8:/var

10. Preserving Permissions, Modes, and Access Time of Files.

Using the SCP command, you will be able to preserve the file’s permissions, mode, and access time using the “-p” option. Consider the below example for doing so.

scp -p jdk-linux-x64_bin.rpm root@170.20.10.8:/var/tmp

11. Transferring Files in a Quiet Mode.

If you want to suppress the transfer progress, you can use the SCP command along with the “-q” option. Consider the following example for doing so.

scp -q -r Downloads root@172.20.10.8:/var/tmp

12. Identify File While Transferring.

Linux environments mostly prefer key-based authentications. Using SCP, you can specify the identity file or private key file using the “-i” option.

scp -i my_key.pem -r Downloads root@172.20.10.8:/root

13. Using Different “ssh_config” Files.

In SCP, you can use the “-F” option for using the different ssh_config files. Use the below syntax for doing so.

scp -F /home/new_ssh_config -r Downloads \root@172.20.10.8:/root

Conclusion

The SCP is the most commonly used command-line tool in Linux and Unix systems to securely transfer the files or directories. This command comes with various options that can be used for multiple purposes and make your work easier in many cases, like compressing files, increasing bandwidth, and more. If you are regularly working on a Linux system, most of the work includes transferring files that must be kept secure while transferring, as they may contain crucial data.

About the author

Simran Kaur

Simran works as a technical writer. The graduate in MS Computer Science from the well known CS hub, aka Silicon Valley, is also an editor of the website. She enjoys writing about any tech topic, including programming, algorithms, cloud, data science, and AI. Travelling, sketching, and gardening are the hobbies that interest her.