scp Command in Linux/Unix with Examples

The scp (secure copy) command in Linux is a command-line utility that is used to securely transfer files between two hosts over a network. It is similar to the cp command, which is used to copy files locally, but scp allows you to copy files between remote hosts. The scp command uses the SSH (Secure Shell) protocol to encrypt the data being transferred, making it a secure way to transfer files over an insecure network.

The basic syntax of the scp command is:

scp [options] [source] [destination]

The "source" is the file or directory that you want to copy, and the "destination" is the location where you want to copy the file or directory.

The most basic use of the scp command is to transfer a single file from one computer to another. For example, to transfer a file called "fruits.txt" from the local computer to a remote computer with IP address 192.168.1.100, you would use the following command:

scp fruits.txt [email protected]:/home/user/

This command will transfer the "fruits.txt" file from the local computer to the remote computer's home directory for the user "user".

The scp command can also be used to transfer a directory and its contents from one computer to another. To transfer a directory called "fruits" from the local computer to a remote computer, you would use the following command:

scp -r fruits [email protected]:/home/user/

This command will transfer the "fruits" directory and its contents from the local computer to the remote computer's home directory for the user "user". The -r option is used to transfer the directory and its contents recursively.

The scp command also has several options that can be used to modify its behavior. Some of the most commonly used options include:

  • -P: specify the port number to connect to on the remote host.
  • -r: recursive, transfers the directory and its contents recursively.
  • -v: verbose, provides detailed information about the progress of the transfer.
  • -C: enables compression, which can speed up the transfer.
  • -p: preserves the modification and access times, and permissions of the files during the transfer.

The scp command can also be used in combination with other commands to perform more complex tasks. For example, you can use the scp command in combination with the ssh command to transfer files between computers without the need to enter a password. This can be done by setting up a ssh key pair and copying the public key to the remote computer.

It is important to note that the scp command requires that the SSH service is running on both the local and remote computers, and that the user has the necessary permissions to transfer the files.

Another important feature of the scp command is the ability to transfer files between remote servers, without the need to download them to your local machine first. This can be done by specifying the source and destination files with the format of user@host:path. For example, the following command will transfer a file called "fruits.txt" from a remote server with IP address 192.168.1.100 to another remote server with IP address 192.168.1.200:

scp [email protected]:/home/user/fruits.txt [email protected]:/home/user/

Another important feature of the scp command is the ability to limit the maximum transfer rate. This can be done by using the -l option. The -l option is followed by a number that represents the maximum transfer rate in kilobits per second. For example, the following command will transfer a file called "fruits.txt" from the local computer to a remote computer with IP address 192.168.1.100 at a maximum rate of 100 kilobits per second:

scp -l 100 fruits.txt [email protected]:/home/user/

It is also important to note that the scp command encrypts the data during the transfer, which ensures that the files are transferred securely. It also verifies the authenticity of the remote host using the SSH host key, which helps to prevent man-in-the-middle attacks.

In addition to the scp command, there are also other similar command-line utilities that can be used to transfer files securely over a network connection such as rsync and sftp. Both of these utilities also use the SSH protocol to transfer files, but they have different features and uses. The rsync command is used to synchronize files and directories between computers, and the sftp command is used for interactive file transfers over an SSH connection.

In summary, the scp command in Linux is a powerful command-line utility that is used to securely transfer files between computers using the SSH protocol. It has several options that can be used to modify its behavior, such as specifying the port number, transferring files recursively, and preserving the modification and access times, and permissions of the files during the transfer. It also allows to transfer files between remote servers, limit the maximum transfer rate, and has built-in encryption and host key verification. It is an essential command for anyone who needs to transfer files securely over a network connection. It is important to be aware of other similar command-line utilities such as rsync and sftp, which can also be used for secure file transfer, but have different features and uses.