{"id":460,"date":"2020-10-21T02:54:52","date_gmt":"2020-10-21T02:54:52","guid":{"rendered":"https:\/\/dft.wiki\/?p=460"},"modified":"2026-06-09T13:31:19","modified_gmt":"2026-06-09T17:31:19","slug":"using-scp-and-rsync-for-copy-and-directory-sync","status":"publish","type":"post","link":"https:\/\/dft.wiki\/?p=460","title":{"rendered":"Using SCP and RSYNC for Copying and Syncing"},"content":{"rendered":"<p>Both SCP and RSYNC use SSH to transfer files and directories.<\/p>\n<p>While SCP simply uploads or downloads files and folders, RSYNC can synchronize the content of two folders by copying only what is new.<\/p>\n<p>The basic syntax of both looks like this:<\/p>\n<pre>[scp\/rsync] [OPTION] \u2026 [<strong>SRC<\/strong>] \u2026 [USER@]HOST:<strong>DEST<\/strong>\r\n[scp\/rsync] [OPTION] \u2026 [USER@]HOST:<strong>SRC<\/strong> [<strong>DEST<\/strong>]<\/pre>\n<p><strong>SCP<\/strong><\/p>\n<pre><strong>scp \/home\/user\/file.zip user@200.100.50.3:\/home\/user\/file.zip<\/strong>\r\nscp file.zip user@200.100.50.3:\/home\/user\r\nscp file.zip 200.100.50.3:\/home\/user\r\nscp file.zip user@200.100.50.3:\r\nscp file.zip 200.100.50.3:\r\n\r\n<strong>scp user@200.100.50.3:\/home\/user\/file.zip \/home\/user\/file.zip<\/strong>\r\nscp user@200.100.50.3:\/home\/user\/file.zip \/home\/user\r\nscp user@200.100.50.3:\/home\/user\/file.zip .\r\nscp 200.100.50.3:file.zip \/home\/user\r\nscp 200.100.50.3:file.zip .<\/pre>\n<p>The examples above show multiple ways to upload (1 to 5) and download (6 to 10) files.<\/p>\n<p>The first example in each block (in bold) shows the complete syntax, unless you need to apply additional options.<\/p>\n<p>By <strong>omitting the filename at the destination<\/strong>, it will be copied with the <strong>same name<\/strong>.<\/p>\n<p>By <strong>omitting the full path on the source<\/strong>, it refers to the <strong>current directory<\/strong>.<\/p>\n<p>By <strong>omitting the full path at the destination<\/strong>, it saves to the <strong>home directory<\/strong> of the user.<\/p>\n<p>By <strong>omitting the username<\/strong>, it will use the <strong>same username<\/strong> as the one you are logged in with locally.<\/p>\n<p>Use the option <strong>-r<\/strong> to copy directories recursively, <strong>-v<\/strong> (verbose) to print debugging messages, and <strong>-P<\/strong> (same as <strong>&#8211;progress<\/strong>) to see real-time progress.<\/p>\n<p><strong>RSYNC<\/strong><\/p>\n<p>The main feature of RSYNC is the ability to compare file dates and directory contents to determine what is new or missing on either side.<\/p>\n<pre>rsync <strong>-r<\/strong> \/directory user@200.100.50.3:\/directory\r\nrsync <strong>-r<\/strong> user@200.100.50.3:\/directory \/directory<\/pre>\n<p>The option <strong>-r<\/strong> (recursive) copies all content, while option <strong>-a<\/strong> (archive) compares both directories (local and remote) and only copies what is missing or has been updated.<\/p>\n<p>Option <strong>-b<\/strong> (backup) will not overwrite the old file with the new version but instead makes a backup copy of the old one first (<strong>file.zip~<\/strong>). The option <strong>&#8211;delete<\/strong> removes files at the destination that were deleted from the source.<\/p>\n<p>Using option <strong>-z<\/strong> (compress), traffic will be compressed, saving time on slow connections but increasing CPU usage on both ends. The option <strong>&#8211;dry-run<\/strong> simulates the operation and shows what would be done without actually performing any changes.<\/p>\n<p>The option <strong>&#8211;exclude &#8216;dir1&#8217;<\/strong> prevents RSYNC from comparing or copying a specific file or directory.<\/p>\n<p>You can also try the option <strong>&#8211;backup-dir=$(date +%F)<\/strong> to see what it does.<\/p>\n<hr \/>\n<p><strong>BONUS<\/strong><\/p>\n<p>LSYNCD is a tool that runs as a service and periodically compares and syncs files (using RSYNC) based on a provided configuration.<\/p>\n<pre>sudo apt install lsyncd -y\r\nsudo mkdir \/etc\/lsyncd\r\nsudo nano \/etc\/lsyncd\/lsyncd.conf.lua<\/pre>\n<p>Add the following content, customizing the parameters in bold:<\/p>\n<pre>settings {\r\nlogfile = \"\/var\/log\/lsyncd\/lsyncd.log\",\r\nstatusFile = \"\/var\/log\/lsyncd\/lsyncd.status\",\r\nstatusInterval = <strong>20<\/strong>,\r\nnodaemon   = false\r\n}\r\n\r\nsync {\r\ndefault.rsync,\r\nsource = \"\/<strong>path<\/strong>\/\",\r\ntarget = \"\/<strong>path<\/strong>\"\r\n}<\/pre>\n<p>Note: the interval (20) is in seconds.<\/p>\n<pre class=\"command\">systemctl start lsyncd\r\nsystemctl enable lsyncd\r\nsystemctl status lsyncd<\/pre>\n<p>For troubleshooting:<\/p>\n<pre>tail -f \/var\/log\/lsyncd\/lsyncd.log\r\ntail -f \/var\/log\/lsyncd\/lsyncd.status<\/pre>\n<hr \/>\n<p>Backing up additional or newer files from source to destination.<\/p>\n<pre>rsync -<strong>rlptgo<\/strong> \/source \/destination<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Both SCP and RSYNC use SSH to transfer files and directories. While SCP simply uploads [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,6],"tags":[],"class_list":["post-460","post","type-post","status-publish","format-standard","hentry","category-linux","category-raspberry-pi"],"_links":{"self":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/460","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=460"}],"version-history":[{"count":14,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/460\/revisions"}],"predecessor-version":[{"id":5816,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/460\/revisions\/5816"}],"wp:attachment":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=460"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=460"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=460"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}