How to Transfer Folder or Directory Using SCP Command in Linux

Share this Article :

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)
Also Read :   How to install Red Hat Enterprise Linux 6 (RHEL) Server

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:

Also Read :   How to Start, Stop, Restart PostgreSQL Database Server on Linux CentOS 6.2 Server

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

    [[email protected] ~]# scp -r /tmp/scpdirdemo [email protected]:/tmp
    [email protected]'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
    [[email protected] ~]#
    

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

    [[email protected] ~]# scp -r [email protected]:/tmp/scpdirdemo /tmp
    [email protected]'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
    [[email protected] ~]#
    

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

    [[email protected] ~]# scp -r [email protected]:/tmp/scpdirdemo [email protected]:/tmp
    [email protected]'s password:
    [email protected]'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.
    
    [[email protected] ~]# ls /tmp/scpdirdemo
    VMwareTools-8.3.2-257589.tar.gz  webmin-1.530-1.noarch.rpm
    [[email protected] ~]#
    


Leave a Reply