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.
The syntax below helps to transfer the files or directories from the remote location to the 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.
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.
You can confirm the file transfer using the below command:
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.
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.
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.
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.
Run the below command to confirm the transfer.
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.
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.
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”.
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.
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.
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.
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.
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.