SCP stands for ‘Secure Copy’. scp is a command-line utility offered by Linux which allows the transfer of files and directories from one machine to another over an insecure network.

If you wish to copy files from one system to another, then scp can be a very good option to make a secure transfer of the files and directories. When the two communicating machines are connected over the same network, then using scp becomes possible.

You can heavily rely on the scp command for confidentiality and integrity as the file which is being transferred and the password used for the transfer are both encrypted. No sensitive information will be revealed even if anyone tries to snoop over the traffic while this transfer is ongoing.

In this tutorial, we will see different examples of the scp command. We will also look at some of the frequently used options with the scp command.

Getting started with the scp command

Using the scp command you can transfer files/directories:

  • From your local machine to a remote machine.
  • Between two remote machines.
  • From a remote machine to your local machine.

General syntax:

scp [Option] [source_file_name] [user@destination_Host]:destination_folder

Let us understand the basic attributes of this command one by one.

  • [source_file_name] This is the source file that you want to copy.
  • [user@destination_Host] This is the username of the remote system where you want to copy the file. The IP address of the remote machine is also used in this attribute after the ‘@‘ symbol.
  • [destination_folder] This is the directory where you want to save the copied file.

Note: The colon (:) symbol is used in the syntax as it differentiates between the local and remote locations. We use the colon (:) with the remote system to specify the directory to which the files should be copied. In case we do not specify the target directory, then the files will be copied to the home directory of the remote system user.


Options used with scp

Some of the most popular options used with the scp command are listed below.

OptionDescription
-Callow compression of the file to be transferred
-vgive the verbose output
-rcopy files and directories recursively
-ppreserve permissions, modes and access times of files
-Pchange the default port used by scp command
Frequently used options with scp command

We will see the examples of these options, further in the tutorial.


Copying file from local to a remote system

scp allows you to transfer files from your local system to a remote system using the following syntax. This allows you to transfer or upload your files to a remotely placed server.

General Syntax:

scp [file_name] remote_user@host:[destination_folder]

Example:

scp apache-tomcat-9.0.8.tar.gz root@143.110.178.221:gaurav

In this example, we are copying a file ‘apache-tomcat-9.0.8.tar.gz’ from the local system to the remote system whose IP address is ‘143.110.178.221’.

On the remote system, the file will be now copied on the directory named ‘gaurav’.

Output:

gaurav@ubuntu:~$ scp apache-tomcat-9.0.8.tar.gz root@143.110.178.221:gaurav
root@143.110.178.221's password: 
apache-tomcat-9.0.8.tar.gz                           100% 9589KB  79.8KB/s   02:00    
gaurav@ubuntu:~$ 

Let us check the output on the remote system for the file.

root@ubuntu-s-1vcpu-1gb-blr1-01:~/gaurav# ls
apache-tomcat-9.0.8.tar.gz
root@ubuntu-s-1vcpu-1gb-blr1-01:~/gaurav#

Thus, the file is successfully copied to the remote system using the scp command.


Copying multiple files to a remote system

In the previous example, we learnt to transfer a file to the remote system using the scp command. We will now see the method to transfer multiple files from your local system to a remote system using this command.

General Syntax:

scp [file 1] [file 2] [file n] remote_username@remote_host:[specific directory]

Let us understand this simple process with an example.

Example:

scp ath.html abc.txt ppa-purge_0.2.8+bzr56_all.deb root@143.110.178.221:gaurav

Here, multiple files are mentioned in the command to be copied on the remote system.

Output:

gaurav@ubuntu:~$ scp ath.html abc.txt ppa-purge_0.2.8+bzr56_all.deb root@143.110.178.221:gaurav 
root@143.110.178.221's password: 
ath.html                                      100%  199KB  94.7KB/s   00:02    
abc.txt                                       100%    0     0.0KB/s   00:00    
ppa-purge_0.2.8+bzr56_all.deb                 100% 4360    42.2KB/s   00:00    
gaurav@ubuntu:~$

On the remote system:

root@ubuntu-s-1vcpu-1gb-blr1-01:~/gaurav# ls -l
total 9800
-rw-r--r-- 1 root root       0 Oct  5 08:58 abc.txt
-rw-r--r-- 1 root root 9818695 Oct  5 08:35 apache-tomcat-9.0.8.tar.gz
-rw-r--r-- 1 root root  204057 Oct  5 08:58 ath.html
-rw-r--r-- 1 root root    4360 Oct  5 08:58 ppa-purge_0.2.8+bzr56_all.deb
root@ubuntu-s-1vcpu-1gb-blr1-01:~/gaurav#

All three files are now copied on the remote system.


Copying a directory to the remote system

You can use scp command to copy a directory from your local system to the remote system. The process is similar to that of copying a file. To copy the content of the directory as well, you can use the -r option with the scp command.

The -r option is used to copy a directory recursively. That means, all the sub-folder and files inside the directory will also be copied.

General syntax:

scp -r [directory path] remote_username@remote_host:[target_directory]

Example:

scp -r PycharmProjects root@143.110.178.221:gaurav

Output:

gaurav@ubuntu:~$ scp -r PycharmProjects root@143.110.178.221:gaurav
root@143.110.178.221's password: 
__main__.py                                   100%  623     7.8KB/s   00:00    
__init__.py                                   100%   23     0.4KB/s   00:00    
completion.py                                 100% 2929    28.1KB/s   00:00    
search.py                                     100% 4728    38.7KB/s   00:00    
uninstall.py                                  100% 2963    32.5KB/s   00:00    
hash.py                                       100% 1681    21.3KB/s   00:00    
check.py                                      100% 1430    16.8KB/s   00:00    
configuration.py                              100% 7125    50.4KB/s   00:00    
show.py                                       100% 6289    49.8KB/s   00:00    
download.py                                   100% 6623    48.2KB/s   00:00  
gaurav@ubuntu:~$  

Using the -r option with the scp command copies all the sub-folders and files within the directory from the local machine to the remote system.


Displaying detailed information about the scp process

You can use the -v (lowercase v) option to display the information about the files that are being copied on either a remote or your local system. This type of output is also called as the verbose output.

When this option is used, the complete debug information about the file is displayed onto the screen.

General Syntax:

scp -v [file_name] user_name@user_host:<folder>

Example:

scp -v apache-tomcat-9.0.8.tar.gz root@159.89.170.11:team

Output:

gaurav@ubuntu:~$ scp -v apache-tomcat-9.0.8.tar.gz root@159.89.170.11:team
Executing: program /usr/bin/ssh host 159.89.170.11, user root, command scp -v -t team
OpenSSH_7.6p1 Ubuntu-4ubuntu0.3, OpenSSL 1.0.2n  7 Dec 2017
debug1: Reading configuration data /home/gaurav/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to 159.89.170.11 [159.89.170.11] port 22.
debug1: Connection established.
debug1: key_load_public: No such file or directory
debug1: identity file /home/gaurav/.ssh/id_rsa type -1
apache-tomcat-9.0.8.tar.gz                                                                                          100% 9589KB  99.8KB/s   01:36    
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug1: channel 0: free: client-session, nchannels 1
debug1: fd 0 clearing O_NONBLOCK
debug1: fd 1 clearing O_NONBLOCK
Transferred: sent 9826736, received 4016 bytes, in 97.2 seconds
Bytes per second: sent 101133.9, received 41.3
debug1: Exit status 0
gaurav@ubuntu:~$

Here, in the output, you can see that the debug information of the file is being displayed on your terminal when the scp command is used with the -v option.


Transferring files between two remote hosts

Linux allows you to connect to multiple remote hosts. You can transfer files and directories between two remotes hosts using the scp command.

General Syntax:

scp remote_user_1@host_1:/[file_name] remote_user_2@host_2:[folder_to_save]

The syntax may seem a bit wider but is pretty simple. Here, the the first part of the command gives the input about the remote user from where the file is to be copied. colon (:) and / is used to specify the file name or the directory name which is to be transferred between the two remote machines.

The second part gives information about the target remote system where the file is to be copied.

Example:

scp -r root@68.183.82.183:gaurav root@159.89.170.11:/team

Here, we will be copying a directory named ‘gaurav’ recursively from the local system to a remote system. The file will be copied to a folder ‘team’ on the remote system.

Output:

gaurav@ubuntu:~$ scp -r root@68.183.82.183:/gaurav root@159.89.170.11:/team 
root@68.183.82.183's password: 
1.py                                          100%  134   261.3KB/s   00:00    
variables.py                                  100%  377   949.2KB/s   00:00    
abc.txt                                       100%    0     0.0KB/s   00:00    
ath.html                                      100%  199KB  41.8MB/s   00:00    
gaurav@ubuntu:~$

Here, we used the scp command on the local system to transfer a directory named ‘gaurav’ from one remote server to another.


Transfer files from the remote system to your local system

You can easily transfer the files or directories from the remote system to your local system using the scp command. In simpler words, you can download multiples files or directories from the remote server onto your local system using the scp command.

General Syntax:

scp remote_username@user_host:/files/file.txt /[folder_of_local_system]

Output:

gaurav@ubuntu:~$ scp root@159.89.170.11:how.txt .
root@159.89.170.11's password: 
how.txt                                                                                                             100%   11     0.1KB/s   00:00    
gaurav@ubuntu:~$

Here, I have downloaded (copied) the file from the remote server onto my home directory. Hence, I have used dot (.) to specify in the command to copy the file to my home directory.

Sample Output:

gaurav@ubuntu:~$ ls -l how.txt
-rw-r--r-- 1 gaurav gaurav 11 Oct  6 09:49 how.txt
gaurav@ubuntu:~$ 

Here, the file is now copied to my home directory from the remote server.

In the same way, you can download multiple files or directories from the remote server using the scp command with the appropriate options.


Compressing files to make faster transfers

Sometimes, transferring of large files may be time-consuming. This issue can be addressed while using the scp command with the -C (uppercase C) option.

Using the -C option, compresses the larger size files which facilitates a faster transfer and thus saves time.

One interesting fact about this option is that, the file is copied with it’s original size on the destination system but during the transfer process, the size is compressed to enable a faster transfer. Thus, the compression is only done on the network.

General Syntax:

scp -C [file_name] user_name@user_host:[target_folder]

Let us see a comparative example to understand the difference.

Transfer without -C option:

gaurav@ubuntu:~$ scp -rv dlink root@68.183.82.183:team
Executing: program /usr/bin/ssh host 68.183.82.183, user root, command scp -v -r -t team
OpenSSH_7.6p1 Ubuntu-4ubuntu0.3, OpenSSL 1.0.2n  7 Dec 2017
debug1: Reading configuration data /home/trinity/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to 68.183.82.183 [68.183.82.183] port 22.
debug1: Connection established.

debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug1: channel 0: free: client-session, nchannels 1
debug1: fd 0 clearing O_NONBLOCK
debug1: fd 1 clearing O_NONBLOCK
Transferred: sent 7516504, received 4008 bytes, in 74.6 seconds
Bytes per second: sent 100693.7, received 53.7
debug1: Exit status 0
gaurav@ubuntu:~$ 

From the above, output we can see that the time required for the transfer is 74.6 seconds. We will try transferring the same file using the -C option and observe the difference.

Transfer with -C option:

gaurav@ubuntu:~$ scp -Crv dlink root@68.183.82.183:team
Executing: program /usr/bin/ssh host 68.183.82.183, user root, command scp -v -r -t team
OpenSSH_7.6p1 Ubuntu-4ubuntu0.3, OpenSSL 1.0.2n  7 Dec 2017
debug1: Reading configuration data /home/trinity/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to 68.183.82.183 [68.183.82.183] port 22.
debug1: Connection established.
.
.
webupload.img                                                                                                       100% 1834KB  98.7KB/s   00:18    
Sending file modes: C0664 1877552 router.img
Sink: C0664 1877552 router.img
router.img                                                                                                          100% 1834KB 100.3KB/s   00:18    
Sink: E
Sending file modes: C0664 3754103 DSL-2750U-Release-IN-T-01.00.07.zip
Sink: C0664 3754103 DSL-2750U-Release-IN-T-01.00.07.zip
DSL-2750U-Release-IN-T-01.00.07.zip                                                                                 100% 3666KB 218.5KB/s   00:16    
Sink: E
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug1: channel 0: free: client-session, nchannels 1
debug1: fd 0 clearing O_NONBLOCK
debug1: fd 1 clearing O_NONBLOCK
Transferred: sent 7518864, received 3828 bytes, in 51.0 seconds
Bytes per second: sent 100245.4, received 51.0
debug1: Exit status 0
debug1: compress outgoing: raw data 7511925, compressed 7513136, factor 1.00
debug1: compress incoming: raw data 1467, compressed 999, factor 0.68
gaurav@ubuntu:~$

Here, we can easily observe that using the -C option with the scp command has allowed us to compress the file over the network thus proving to be a time-saver option.


Using a different ssh port for file transfer

While using the scp command the default port that is deployed is the port 22. The user has the freedom to customise this choice of port. You can use the -P (uppercase P option) with the scp command to use the port of your choice.

General Syntax:

cp -P [new_port_number] [file_name/directory_name] remote_user@host:[destination_folder]

Example:

scp -P 4248 dlink root@68.183.82.183:team

Using the above command, the file will be transferred to the remote server. But this time, the port used will be port 4248 instead of the default port 22.


Conclusion

After going through this tutorial, we have learnt about the dynamic nature of the scp command used to transfer or copy files from one system to another. This option can also be used to download files or directories from the remote server. Thus, we can conclude that scp command proves very helpful for file transfers when you have to handle more than one system as well as remote servers simultaneously.