How to Transfer Folder or Directory Using SCP Command in Linux

In this post, I will show the basic syntax on how to use secure copy (SCP) command to transfer the directory between linux servers. SCP allows directory to be transferred to, from, or between Linux or Unix hosts. It uses ssh for data transfer and provides the same authentication and same level of security as ssh. Before start, there is the definition for every syntax that i will use:

  • local server1 (currently ssh) = The server1 ip (192.168.2.2) that i ssh from my notebook.
  • remote server2 = The server2 ip (192.168.2.5)
  • another remote server3 = The server3 ip (192.168.2.6)

There are three ways to use the scp command which are :

  1. To copy from local server1 (current ssh) to a remote server2(server1–>server2)
  2. To copy from a remote server2 to local server1 (currently ssh). ( server2 –> server1)
  3. To copy from a remote server2 to another remote server3( server2–>server3)

In the third case, the directory is transferred directly between the servers; your local server1 ( currently ssh) will only tell the servers what to be transfer. Let’s have a look at the syntax of this command:

1. To copy directory from local server1 (currently ssh) to a (remote) server2.
server1 : 192.168.2.2
server2 : 192.168.2.5

[root@server1 ~]# scp -r /tmp/scpdirdemo root@192.168.2.5:/tmp
root@192.168.2.5's password:
webmin-1.530-1.noarch.rpm                     100%   15MB 350.1KB/s   00:45
VMwareTools-8.3.2-257589.tar.gz               100%   46MB 362.3KB/s   02:10
[root@server1 ~]#

2. To copy directory from a remote server2 to local server1 (currently ssh)
server1 : 192.168.2.2
server2 : 192.168.2.5

[root@server1 ~]# scp -r root@192.168.2.5:/tmp/scpdirdemo /tmp
root@192.168.2.5's password:
VMwareTools-8.3.2-257589.tar.gz               100%   46MB   3.8MB/s   00:12
webmin-1.530-1.noarch.rpm                     100%   15MB   1.7MB/s   00:09
[root@server1 ~]#

3. To copy directory from a remote server2 to another remote server3.
server1 : 192.168.2.2
server2 : 192.168.2.5
server3 : 192.168.2.6

[root@server1 ~]# scp -r root@192.168.2.5:/tmp/scpdirdemo root@192.168.2.6:/tmp
root@192.168.2.5's password:
root@192.168.2.6's password:
VMwareTools-8.3.2-257589.tar.gz                    100%   46MB  11.5MB/s   00:04
webmin-1.530-1.noarch.rpm                          100%   15MB   7.7MB/s   00:02
Connection to 192.168.2.5 closed.
[root@server2 ~]# ls /tmp/scpdirdemo
VMwareTools-8.3.2-257589.tar.gz  webmin-1.530-1.noarch.rpm
[root@server2 ~]#