scp command in Linux with examples

In this article, you will learn about scp command in Linux with examples.

scp stands for secure copy. It is a command-line utility in Linux and Unix-like operating systems. The major use of the scp command is copy files or directories between servers securely.

The scp command uses the ssh protocol for copy or transfer files or directories between servers. So, the data transfer with scp is encrypted, the middle man can not get any sensitive pieces of information.

  • You can copy a file or directory from a local system to a remote system.
  • You can copy a file or directory from a remote system to a local system.
  • Also, you can copy a file or directory between two remote systems using your local system.

How to get help for the scp command

I always suggest reading the manual page and help. This will give you a deep introduction to the command.

So, to get the manual page type the below command,

man scp

Also, to get the only command options, you can type the below command.

scp --help

Syntax of the scp command


As we know now, how to get some information about the scp command using the above command with arguments. Now let’s talk about scp syntax. The syntax of the scp command is mentioned below,

scp [-346BCpqrTv] [-c cipher] [-F ssh_config] [-i identity_file] [-J destination] [-l limit] [-o ssh_option] [-P port] [-S program] source … target

The most frequently used option with scp command

There is the list of most frequently used options, you can find the list below.

  • -P: port, Specifies the (ssh port) port to connect to on the remote host. This is the capital P.
  • -p: Preserve, Preserves modification times, access times, and modes from the original file.
  • -q: Quiet mode, disables the progress meter as well as warning and diagnostic messages from ssh.
  • -v: Verbose mode. it shows the progress message.
  • -C: Compression, You can use this option for compression of the data. This is capital C.
  • -c: cipher, This is the option to use for a cipher to encrypt the data transfer.
  • -r: copying recursively.
  • -i: identity file, Selects the file from which the identity (private key) for public-key authentication is read.
  • -l: Limits the used bandwidth, specified in Kbit/s.

Note: Some points

  • scp command uses ssh for data transfer. So, it requires an ssh key or password authentication on the remote systems.
  • To be able to copy files, you must have at least read permissions on the source file and write permission on the target system
  • Also, if you have a file with the same name on the destination system which you are sending then the file will be overwritten on the destination system. However, you will not get any warning messages.

Copying a file from a local system to a remote system using scp

Firstly, we are going to copy a file from a local system to a remote system. The file name which I want to transfer is demofile.txt and it is located in /root and the remote system IP address is 192.168.43.10 and I want to put this file in /tmp. So, the command will look like the below.

scp /root/demofile.txt 192.168.43.10:/tmp

Therefore, the output you can see in the below picture.

Secondly, If ssh port is not the default port 22 then you have to specify the port number along with scp command. For example, if the port number is 8012 the command will be as below.

scp -P 8012 /root/demofile.txt 192.168.43.10:/tmp

Thirdly, If you want to copy a directory then you can use -r option to copy recursively. For example, I am going to copy the /etc/sysconfig directory. So the command will look like as below.

scp -r /etc/sysconfig 192.168.43.10:/tmp

Copying a remote file to a local system using the scp

So, if you want to copy a file from a remote system to a local system then you use the remote location as a source and local location as the destination.

For example, to copy a file with the name demofile2.txt from a remote system to the local system use the below command.

scp 192.168.43.10:/root/demofile2.txt /tmp

Therefore, the output you can see in the below picture.

Copying a remote file to a local system using the scp command

Copy a file between two remote systems using the scp

Also, if you have a local system and you want to copy the file from one remote system to the other remote system then you can do that.

For example, I have two remote systems. And, the IP address of one computer is 192.168.43.10 and the IP address of the second computer is 192.168.43.20.

Here, if you want to copy a file with the name of demofile3.txt from the first remote computer to the second remote computer using the third or local computer then the command will be as below.

scp 192.168.43.10:/root/demofile3.txt 192.168.43.20:/tmp

It will ask the password of both remote computers if you are not using an ssh key or passwordless option. So, you have to enter the password for both the remote computer. And, this is done.

Therefore, the output you can see in the below picture.

Increasing the transfer speed using -C option

You can enable the compression while transferring the data using the -C option using scp command. It automatically compressed the data at the source side and decompresses the data at the destination.

scp -r -C /root/demofile4.txt root@192.168.43.10:/mnt

Therefore, the output you can see in the below picture.

Increasing the transfer speed using -C option using scp command

Conclusion

In this tutorial, you learn the command scp and its uses with examples. In conclusion, you can use the scp command now. Therefore, Keep practicing.

So, I hope, you understand but if you have any questions, you can ask in the comment section.

You can read about What is the rsync command & how to use it

You can also read about ssh from the link about ssh

2 Comments

  1. This is perfectly awesome. Very clear and easy to understand each and every step. Thanks for sharing this knowledge.

Leave a Reply