{"id":1788,"date":"2022-12-13T13:51:00","date_gmt":"2022-12-13T20:51:00","guid":{"rendered":"https:\/\/ioflood.com\/blog\/?p=1788"},"modified":"2023-11-07T21:09:46","modified_gmt":"2023-11-08T04:09:46","slug":"copy-directory-with-ssh-three-methods","status":"publish","type":"post","link":"https:\/\/ioflood.com\/blog\/copy-directory-with-ssh-three-methods\/","title":{"rendered":"Copy Files and Directories in Linux with SSH using SCP, CP, and Rsync"},"content":{"rendered":"\r\n<div class=\"wp-block-image\"><figure class=\"alignright size-large is-resized\"><img decoding=\"async\" src=\"https:\/\/ioflood.com\/blog\/wp-content\/uploads\/2023\/08\/folder-copy-stylized-300x300.jpg\" alt=\"artistic document folder\" class=\"wp-image-1891\" width=\"300\" height=\"300\" title=\"\"><\/figure><\/div>\r\n\r\n\r\n\r\n<p>As a server admin, even simple tasks can benefit from a fresh perspective. To that end, today&#8217;s article talks about multiple ways you can copy an entire directory and its files in linux. To do so, we&#8217;ll be exploring three commands: <strong>cp, scp, <\/strong>and <strong>rsync<\/strong>.<\/p>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\">cp<\/h2>\r\n\r\n\r\n\r\n<p>You can copy files and folders within the same file system in linux using the cp command. The <strong>-r<\/strong> argument tells cp that you wish to copy a directory recursively, which copies the folder contents as well. For instance:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\"><code><strong>cp -r \/path\/to\/source\/directory \/path\/to\/destination\/directory\r\n<\/strong><\/code><\/pre>\r\n\r\n\r\n\r\n<p>The result is that the directory, including all of its contents and subdirectories, will be copied to a different folder on the local server.<\/p>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\">scp<\/h2>\r\n\r\n\r\n\r\n<p>Meanwhile, the <strong>scp<\/strong> command will allow you to copy files and directories using an SSH encrypted connection. This allows for greater security and data integrity, and is commonly used to copy directories between remote servers, although it can also be used locally. To copy an entire directory using <strong>scp<\/strong>, you can also use the <strong>-r<\/strong> flag the same way you did with the <strong>cp <\/strong>command. Example:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\"><code><strong>scp -r \/path\/to\/source\/directory user@remotehost:\/path\/to\/destination\/directory\r\n<\/strong><\/code><\/pre>\r\n\r\n\r\n\r\n<p>The result here is that the directory &#8220;directory&#8221; will be copied securely to the destination folder on &#8220;remotehost&#8221;<\/p>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\">rsync<\/h2>\r\n\r\n\r\n\r\n<p>The <strong>rsync <\/strong>command enables you to replicate files and directories while keeping ownership and permissions. It also offers extra capabilities like compression and incremental updates. The <strong>-a<\/strong> switch enables &#8220;archive&#8221; mode, which allows you to use rsync to transfer a directory while preserving ownership and rights. For instance:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-preformatted\"><code><strong>rsync -a \/path\/to\/source\/directory \/path\/to\/destination\/directory\r\n<\/strong><\/code><\/pre>\r\n\r\n\r\n\r\n<p>This example shows how to use rsync to copy directories on the local machine. You can also use it the same way shown for scp if you want to use rsync to copy directories to remote servers.<\/p>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\">That&#8217;s it!<\/h2>\r\n\r\n\r\n\r\n<p>The above is one of the more basic and common ways to use these commands. We would encourage you to use linux man pages for each command in order to learn about their more advanced features.<\/p>\r\n\r\n\r\n\r\n<p>In conclusion, it is helpful for a server administrator to be able to copy a complete directory and all of its contents using a variety of techniques. For copying files and directories, the <strong>cp<\/strong>, <strong>scp<\/strong>, and <strong>rsync <\/strong>commands offer specific options and features; depending on your\u00a0circumstance, one may be preferable to the others.<\/p>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\">Do you love servers?<\/h2>\r\n\r\n\r\n\r\n<p>We do! If you have questions related to Linux and servers, large or small, IOFLOOD can help. Our dedicated servers offer a range of solutions to fit your needs, and our team of experts is always available to provide support and guidance for all your server-related questions.<\/p>\r\n\r\n\r\n\r\n<p>To learn more about our dedicated serve<strong>rs and how we can help you with your specific needs, contact us at sales[at]ioflood.com<\/strong> or visit our website at <a href=\"https:\/\/ioflood.com\">https:\/\/ioflood.com<\/a>. Our team is ready to answer your questions and help you find the best server for your needs.<\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>As a server admin, even simple tasks can benefit from a fresh perspective. To that end, today&#8217;s article talks about multiple ways you can copy an entire directory and its files in linux. To do so, we&#8217;ll be exploring three commands: cp, scp, and rsync. cp You can copy files and folders within the same [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1891,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,4,5,3,115,9,6],"tags":[],"class_list":["post-1788","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-backups","category-centos","category-debian","category-linux","category-redhat-enterprise-linux-rhel","category-sysadmin","category-ubuntu","cat-13-id","cat-4-id","cat-5-id","cat-3-id","cat-115-id","cat-9-id","cat-6-id","has_thumb"],"_links":{"self":[{"href":"https:\/\/ioflood.com\/blog\/wp-json\/wp\/v2\/posts\/1788","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ioflood.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ioflood.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ioflood.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ioflood.com\/blog\/wp-json\/wp\/v2\/comments?post=1788"}],"version-history":[{"count":13,"href":"https:\/\/ioflood.com\/blog\/wp-json\/wp\/v2\/posts\/1788\/revisions"}],"predecessor-version":[{"id":9284,"href":"https:\/\/ioflood.com\/blog\/wp-json\/wp\/v2\/posts\/1788\/revisions\/9284"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ioflood.com\/blog\/wp-json\/wp\/v2\/media\/1891"}],"wp:attachment":[{"href":"https:\/\/ioflood.com\/blog\/wp-json\/wp\/v2\/media?parent=1788"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ioflood.com\/blog\/wp-json\/wp\/v2\/categories?post=1788"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ioflood.com\/blog\/wp-json\/wp\/v2\/tags?post=1788"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}