Updated March 23, 2023
Introduction to SCP Command in Linux
SCP is termed as “secure copy” which allows us to secure transferring of files between localhost and remote host or between two remote hosts. It uses Secure Shell (SSH) protocol for security and authentication and security purpose on which it is based. SCP is used for it’s secured, simplified & pre-installed facility availability. In this topic, we are going to learn about SCP Command in Linux.
This command allows us to copy files from to or even between different hosts/computer files to be copied to, from, or between different hosts. It is a command-line utility that allows you to copy files & directories between two locations securely.
Purpose
We can use SCP command for the below use:
- From our local systems to a remote system or host.
- From a remote system or server or host to our local system.
- Among two or more remote systems from our local system.
Security
As security is one of the aspects of any Linux based command and secure copy (SCP) is one of them, where both files and passwords are encrypted from the malware or even anyone trying to gain sensitive information on the traffic.
SCP Syntax
Scp command syntax takes the following form :
scp [OPTIONS] [username@][Source_HOST:]file_name1 [username@]Destination_HOST:]file_name2
where
1. OPTIONS: We have different options such as ssh configuration, cipher, limit, ssh port, recursive copy and many more.
2. [username@]Source_HOST:]file_name1: Source file with username & host.
3. [username@]Destination_HOST:]file_name2: Destination file with username & host.
Options
SCP command provides a variety of options. Let us discuss most commonly used:
- scp –P port: It Specify the port by which one can connect on the remote host.
- scp –p: This option is used to check access times, modification times and different modes from the original file.
- scp –q: This option is used to disable the progress done by the file while copying.
- scp –r: This option is used to recursively copy the files present in the entire directory.
- scp –S program: This option is used for encrypted connection with the name of the program, for which the program must understand the ssh(1) option.
- scp –v: This option will enable the command in verbose mode. So when we run the command with these options about the progress with different messages. This is helpful in authentication, debugging connection & configuration problems.
- scp -C: This option will enable the compression of files while copying, which increases the performance in file transfer.
- scp -i: This is option is used to file whether its a private key or a file.
Examples of SCP Command in Linux
Now let us discuss each option with different examples so that we can have a proper understanding of the SCP command concepts.
1. Copying of a file from the local system to the remote system
Let us consider, we want to copy a jdk file from our local Linux system to remote systems (172.20.11.32) by scp command then we will use the following command :
Command:
scp jdk-linux-x32.rpm [email protected]:/app
Output:
From the above output, the command will copy a file from the local system to our remote system under /app folder.
2. Copying a file from a remote system to a local system
In case there is a need to copy a file from a remote system to the local system under the /app folder then we need to execute the below command:
Command:
scp [email protected]:/root/Technical_file.txt /app
Output:
3. Copying of files with Verbose mode enabled
If we want to know what’s going behind the transfer of file then we can use verbose mode with the “-v” option.
Command:
scp -v jdk-linux-x32.rpm [email protected]:/app
Output:
From the above output, we can see how detail level information is displayed in verbose mode.
4. Transfering of multiple files to a remote system
There may be a requirement where we need to transfer multiple files in one go. So in this scp plays a vital role. By running the below command we can achieve this.
Command:
scp File_test.txt Demo_file.html jdk-linux-x32.rpm [email protected]:/app
Output:
5. Transferring files between two remote host
In order to copy files and directory between two remote hosts which from be from a local Linux system to two different remote Linux systems, below will help us to achieve this.
Command:
scp [email protected]:~/backup-file.zip [email protected]:/app
ssh [email protected] "ls -l /app/backup-file.zip" .
Output:
6. Copying of files and directory recursively (-r)
Option -r can be used to copy the files in a directory with the help of the below command recursively.
Command:
scp -r Document [email protected]:/app/
By the below command, we can verify whether the Document folder is copied to a remote system or not.
Output:
7. Increasing the file transfer speed by using compression option (-C)
To transfer the file quickly option -C can be used. This file is compressed and decompressed at the destination hosts.
Command:
scp -r -C Document [email protected]:/app
In the above, we are transferring the Document directory with the compression option enabled.
8. Limiting the bandwidth while copying of files ( -l )
By ‘-l’ option we can restrict bandwidth during file transfer. Bandwidth is measured in Kbit/s, shown by below example:
Command:
scp -l 500 jdk-linux-x32.rpm [email protected]:/app
9. Specifying different ssh port in scp command ( -P)
There may be some scenarios where ssh port gets changed on destination host so we can specify the port number using the ‘-P’ option.
Command:
scp -P 2030 jdk-linux-x32.rpm [email protected]:/app
In above ssh port for remote/destination host is ‘2030’
10. Transfer files in quiet/silent mode ( -q)
We can use the ‘-q’ option to hide the details of information and different messages.
The below command can easily do this.
Command:
scp -q -r Document [email protected]:/app/
Conclusion
In this way, we have learned about SCP command, different options, and application in the Linux environment for secure copying of data.
Recommended Articles
This is a guide to SCP Command in Linux. Here we discuss the basic concept, different examples along with the output to understand the SCP command concepts. You may also have a look at the following articles to learn more –