How to Transfer a File or Directory Using SCP Command in Linux

Secure Copy (SCP) is a command-line tool used to transfer files and directories securely between two Linux systems. SCP uses the Secure Shell (SSH) protocol to encrypt data during transmission. In this guide, we will show you how to transfer a folder or directory using the SCP command in Linux.

Step 1: Connect to the remote server

The first step is to connect to the remote server using SSH. You can do this by running the following command in the terminal:

ssh username@remote_host_ip_address

Replace “username” with your username on the remote server and “remote_host_ip_address” with the IP address of the remote server.

Step 2: Transfer the folder or directory

To transfer a folder or directory using SCP, you need to use the “-r” option, which recursively copies the entire directory tree. The basic syntax of the SCP command for transferring a folder is as follows:

scp -r /path/to/local/folder username@remote_host:/path/to/remote/folder

Replace “/path/to/local/folder” with the path to the folder you want to transfer from your local machine, “username” with your username on the remote server, “remote_host” with the hostname or IP address of the remote server, and “/path/to/remote/folder” with the path to the folder on the remote server where you want to transfer the folder.

See also  How to Install MySQL Server on CentOS 5.8

For example, to transfer a folder named “myfolder” located in the home directory of the local machine to the remote server in the home directory of the user “user1”, you can run the following command:

scp -r ~/myfolder user1@remote_host:~/

This will transfer the “myfolder” directory and all its contents to the home directory of the user “user1” on the remote server.

Step 3: Verify the transfer

After the transfer is complete, you can verify that the folder was transferred correctly by logging in to the remote server and navigating to the directory where you transferred the folder.

See also  How to Fix "-bash: hdparm: command not found" on Linux CentOS 6.3

Some Examples

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 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 ~]#

Commands Mentioned:

  • ssh – connects to a remote server using SSH
  • scp – transfers files and directories securely using SCP
See also  How to enable PHP MBString Extension on Ubuntu?

Conclusion:

In this guide, we have shown you how to transfer a folder or directory using the SCP command in Linux. By using SCP, you can securely transfer files and directories between two Linux systems over the network. Remember to use the “-r” option to transfer a folder and verify the transfer after it is complete.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *