{"id":743,"date":"2011-03-26T17:48:25","date_gmt":"2011-03-26T09:48:25","guid":{"rendered":"https:\/\/webhostinggeeks.com\/howto\/?p=743"},"modified":"2023-04-28T09:50:33","modified_gmt":"2023-04-28T09:50:33","slug":"how-to-transfer-folder-or-directory-using-scp-command-in-linux","status":"publish","type":"post","link":"https:\/\/webhostinggeeks.com\/howto\/how-to-transfer-folder-or-directory-using-scp-command-in-linux\/","title":{"rendered":"How to Transfer a File or Directory Using SCP Command in Linux"},"content":{"rendered":"<p>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.<\/p>\n<h2>Step 1: Connect to the remote server<\/h2>\n<p>The first step is to connect to the remote server using SSH. You can do this by running the following command in the terminal:<\/p>\n<pre>\r\nssh username@remote_host_ip_address\r\n<\/pre>\n<p>Replace &#8220;username&#8221; with your username on the remote server and &#8220;remote_host_ip_address&#8221; with the IP address of the remote server.<\/p>\n<h2>Step 2: Transfer the folder or directory<\/h2>\n<p>To transfer a folder or directory using SCP, you need to use the &#8220;-r&#8221; option, which recursively copies the entire directory tree. The basic syntax of the SCP command for transferring a folder is as follows:<\/p>\n<pre>\r\nscp -r \/path\/to\/local\/folder username@remote_host:\/path\/to\/remote\/folder\r\n<\/pre>\n<p>Replace &#8220;\/path\/to\/local\/folder&#8221; with the path to the folder you want to transfer from your local machine, &#8220;username&#8221; with your username on the remote server, &#8220;remote_host&#8221; with the hostname or IP address of the remote server, and &#8220;\/path\/to\/remote\/folder&#8221; with the path to the folder on the remote server where you want to transfer the folder.<\/p>\n<p>For example, to transfer a folder named &#8220;myfolder&#8221; located in the home directory of the local machine to the remote server in the home directory of the user &#8220;user1&#8221;, you can run the following command:<\/p>\n<pre>\r\nscp -r ~\/myfolder user1@remote_host:~\/\r\n<\/pre>\n<p>This will transfer the &#8220;myfolder&#8221; directory and all its contents to the home directory of the user &#8220;user1&#8221; on the remote server.<\/p>\n<h2>Step 3: Verify the transfer<\/h2>\n<p>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.<\/p>\n<h2>Some Examples<\/h2>\n<p>The definition for every syntax that I will use:<\/p>\n<ul>\n<li> local <strong>server1<\/strong> (currently ssh) = The <strong>server1<\/strong> ip (192.168.2.2) that i ssh from my notebook.<\/li>\n<li> remote <strong>server2<\/strong> = The <strong>server2<\/strong> ip (192.168.2.5)<\/li>\n<li> another remote <strong>server3<\/strong> = The <strong>server3<\/strong> ip (192.168.2.6)<\/li>\n<\/ul>\n<p>There are three ways to use the scp command which are :<\/p>\n<ol>\n<li> To copy from local<strong> server1<\/strong> (current ssh) to a remote <strong>server2<\/strong>. <strong>(server1&#8211;&gt;server2)<\/strong><\/li>\n<li> To copy from a remote <strong>server2<\/strong> to local <strong>server1<\/strong> (currently ssh). <strong>( server2 &#8211;&gt; server1)<\/strong><\/li>\n<li> To copy from a remote <strong>server2<\/strong> to another remote<strong> server3<\/strong>. <strong>( server2&#8211;&gt;server3)<\/strong><\/li>\n<\/ol>\n<p>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&#8217;s have a look at the syntax of this command:<\/p>\n<p>1. To copy directory from local<strong> server1<\/strong> (currently ssh) to a (remote) <strong>server2<\/strong>.<br \/>\n<strong> server1 : 192.168.2.2<\/strong><br \/>\n<strong> server2 : 192.168.2.5<\/strong><\/p>\n<pre class=\"code\">[root@server1 ~]# scp -r \/tmp\/scpdirdemo root@192.168.2.5:\/tmp\r\nroot@192.168.2.5's password:\r\nwebmin-1.530-1.noarch.rpm                     100%   15MB 350.1KB\/s   00:45\r\nVMwareTools-8.3.2-257589.tar.gz               100%   46MB 362.3KB\/s   02:10\r\n[root@server1 ~]#\r\n<\/pre>\n<p>2. To copy directory  from a remote <strong>server2<\/strong> to local <strong>server1 <\/strong>(currently ssh)<br \/>\n<strong> server1 : 192.168.2.2<\/strong><br \/>\n<strong> server2 : 192.168.2.5<\/strong><\/p>\n<pre class=\"code\">[root@server1 ~]# scp -r root@192.168.2.5:\/tmp\/scpdirdemo \/tmp\r\nroot@192.168.2.5's password:\r\nVMwareTools-8.3.2-257589.tar.gz               100%   46MB   3.8MB\/s   00:12\r\nwebmin-1.530-1.noarch.rpm                     100%   15MB   1.7MB\/s   00:09\r\n[root@server1 ~]#\r\n<\/pre>\n<p>3. To copy directory from a remote <strong>server2<\/strong> to another remote<strong> server3<\/strong>.<br \/>\n<strong> server1 : 192.168.2.2<\/strong><br \/>\n<strong> server2 : 192.168.2.5<\/strong><br \/>\n<strong> server3 : 192.168.2.6<\/strong><\/p>\n<pre class=\"code\">[root@server1 ~]# scp -r root@192.168.2.5:\/tmp\/scpdirdemo root@192.168.2.6:\/tmp\r\nroot@192.168.2.5's password:\r\nroot@192.168.2.6's password:\r\nVMwareTools-8.3.2-257589.tar.gz                    100%   46MB  11.5MB\/s   00:04\r\nwebmin-1.530-1.noarch.rpm                          100%   15MB   7.7MB\/s   00:02\r\nConnection to 192.168.2.5 closed.\r\n<\/pre>\n<pre class=\"code\">[root@server2 ~]# ls \/tmp\/scpdirdemo\r\nVMwareTools-8.3.2-257589.tar.gz  webmin-1.530-1.noarch.rpm\r\n[root@server2 ~]#\r\n<\/pre>\n<h2>Commands Mentioned:<\/h2>\n<ul>\n<li><span class=\"fw-bold\">ssh<\/span> &#8211; connects to a remote server using SSH<\/li>\n<li><span class=\"fw-bold\">scp<\/span> &#8211; transfers files and directories securely using SCP<\/li>\n<\/ul>\n<h2>Conclusion:<\/h2>\n<p>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 &#8220;-r&#8221; option to transfer a folder and verify the transfer after it is complete.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8230;.<\/p>\n","protected":false},"author":6,"featured_media":323,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"wds_primary_category":0,"footnotes":""},"categories":[996],"tags":[1536],"class_list":["post-743","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","tag-linux"],"_links":{"self":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts\/743","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/comments?post=743"}],"version-history":[{"count":0,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts\/743\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/media\/323"}],"wp:attachment":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/media?parent=743"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/categories?post=743"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/tags?post=743"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}