<p>What is the best way to copy a directory (with sub-dirs and files) from one remote Linux server to another remote Linux server? I have connected to both using SSH client (like Putty). I have root access to both. </p>

[{'answer_id': 69418, 'author': 'Ana Betts', 'author_id': 5728, 'author_profile': 'https://Stackoverflow.com/users/5728', 'pm_score': 3, 'selected': False, 'text': '<p>rsync -avlzp /path/to/folder name@remote.server:/path/to/remote/folder</p>\n'}, {'answer_id': 69419, 'author': 'John Douthat', 'author_id': 2774, 'author_profile': 'https://Stackoverflow.com/users/2774', 'pm_score': 2, 'selected': False, 'text': '<p>Check out <a href="https://en.wikipedia.org/wiki/Secure_copy" rel="nofollow noreferrer">scp</a> or <a href="https://en.wikipedia.org/wiki/Rsync" rel="nofollow noreferrer">rsync</a>, \n<code>man scp</code>\n<code>man rsync</code></p>\n\n<pre><code>scp file1 file2 dir3 user@remotehost:path\n</code></pre>\n'}, {'answer_id': 69420, 'author': 'Ady Romantika', 'author_id': 67775, 'author_profile': 'https://Stackoverflow.com/users/67775', 'pm_score': 2, 'selected': False, 'text': '<p>Use rsync so that you can continue if the connection gets broken. And if something changes you can copy them much faster too!</p>\n\n<p>Rsync works with SSH so your copy operation is secure.</p>\n'}, {'answer_id': 69421, 'author': 'Serafina Brocious', 'author_id': 4977, 'author_profile': 'https://Stackoverflow.com/users/4977', 'pm_score': 3, 'selected': False, 'text': '<pre><code>scp -r &lt;directory&gt; &lt;username&gt;@&lt;targethost&gt;:&lt;targetdir&gt;\n</code></pre>\n'}, {'answer_id': 69422, 'author': 'zigdon', 'author_id': 4913, 'author_profile': 'https://Stackoverflow.com/users/4913', 'pm_score': 1, 'selected': False, 'text': "<p>Well, quick answer would to take a look at the 'scp' manpage, or perhaps rsync - depending exactly on what you need to copy.  If you had to, you could even do tar-over-ssh:</p>\n\n<pre><code>tar cvf - | ssh server tar xf -\n</code></pre>\n"}, {'answer_id': 69423, 'author': 'Flame', 'author_id': 5387, 'author_profile': 'https://Stackoverflow.com/users/5387', 'pm_score': 3, 'selected': False, 'text': '<p>Log in to one machine</p>\n\n<blockquote>\n  <p>$ scp -r /path/to/top/directory user@server:/path/to/copy</p>\n</blockquote>\n'}, {'answer_id': 69424, 'author': 'Allan Wind', 'author_id': 9706, 'author_profile': 'https://Stackoverflow.com/users/9706', 'pm_score': 0, 'selected': False, 'text': '<p>As non-root user ideally:</p>\n\n<p>scp -r src $host:$path</p>\n\n<p>If you already some of the content on $host consider using rsync with ssh as a tunnel.</p>\n\n<p>/Allan</p>\n'}, {'answer_id': 69433, 'author': 'Ycros', 'author_id': 10495, 'author_profile': 'https://Stackoverflow.com/users/10495', 'pm_score': 6, 'selected': False, 'text': '<p>There are two ways I usually do this, both use ssh:</p>\n\n<pre><code>scp -r sourcedir/ user@dest.com:/dest/dir/\n</code></pre>\n\n<p>or, the more robust and faster (in terms of transfer speed) method:</p>\n\n<pre><code>rsync -auv -e ssh --progress sourcedir/ user@dest.com:/dest/dir/\n</code></pre>\n\n<p>Read the man pages for each command if you want more details about how they work.</p>\n'}, {'answer_id': 69566, 'author': 'user11104', 'author_id': 11104, 'author_profile': 'https://Stackoverflow.com/users/11104', 'pm_score': 2, 'selected': False, 'text': '<p>Try unison if the task is recurring.\n<a href="http://www.cis.upenn.edu/~bcpierce/unison/" rel="nofollow noreferrer">http://www.cis.upenn.edu/~bcpierce/unison/</a></p>\n'}, {'answer_id': 74328, 'author': 'Ram Prasad', 'author_id': 6361, 'author_profile': 'https://Stackoverflow.com/users/6361', 'pm_score': 1, 'selected': False, 'text': '<p>I think you can try with:</p>\n\n<pre><code>rsync -azvu -e ssh user@host1:/directory/ user@host2:/directory2/\n</code></pre>\n\n<p>(and I assume you are on host0 and you want to copy from host1 to host2 directly)</p>\n\n<p>If the above does not work, you could try:</p>\n\n<pre><code>ssh user@host1 "/usr/bin/rsync -azvu -e ssh /directory/ user@host2:/directory2/"\n</code></pre>\n\n<p>in the this, it would work, if you already have setup passwordless SSH login from host1 to host2</p>\n'}, {'answer_id': 82302, 'author': 'Gunstick', 'author_id': 15653, 'author_profile': 'https://Stackoverflow.com/users/15653', 'pm_score': 2, 'selected': False, 'text': '<p>I used rdiffbackup <a href="http://www.nongnu.org/rdiff-backup/index.html" rel="nofollow noreferrer">http://www.nongnu.org/rdiff-backup/index.html</a> because it does all you need without any fancy options. It\'s based on the rsync algorithm.\nIf you only need to copy one time, you can later remove the rdiff-backup-data directory on the destination host.</p>\n\n<pre><code>rdiff-backup user1@host1::/source-dir user2@host2::/dest-dir\n</code></pre>\n\n<p>from the doc:</p>\n\n<blockquote>\n  <p>rdiff-backup also preserves \n  subdirectories, hard links, dev files,\n  permissions, uid/gid ownership, \n  modification times, extended\n  attributes, acls, and resource forks.</p>\n</blockquote>\n\n<p>which is an bonus to the scp -p proposals as the -p option does not preserve all (e.g. rights on directories are set badly)</p>\n\n<p>install on ubuntu:</p>\n\n<pre><code>sudo apt-get install rdiff-backup\n</code></pre>\n'}, {'answer_id': 82493, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>If you are serious about wanting an exact copy, you probably also want to use the -p switch to scp, if you're using that.  I've found that scp <strong>reads from devices</strong>, and I've had problems with cpio, so I personally always use <em>tar</em>, like this:</p>\n\n<pre><code>cd /origin; find . -xdev -depth -not -path ./lost+found -print0 \\\n| tar --create --atime-preserve=system --null --files-from=- --format=posix \\\n--no-recursion --sparse | ssh targethost 'cd /target; tar --extract \\\n--overwrite --preserve-permissions --sparse'\n</code></pre>\n\n<p>I keep this incantation around in a file with various other means of copying files around.  This one is for copying over SSH; the other ones are for copying to a compressed archive, for copying within the same computer, and for copying over an unencrypted TCP socket when SSH is too slow.</p>\n"}, {'answer_id': 83539, 'author': 'Sergey Stolyarov', 'author_id': 15958, 'author_profile': 'https://Stackoverflow.com/users/15958', 'pm_score': 0, 'selected': False, 'text': "<p>scp as mentioned above is usually a best way, but don't forget colon in the remote directory spec otherwise you'll get copy of source directory on local machine.</p>\n"}, {'answer_id': 102494, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>I like to pipe tar through ssh.  </p>\n\n<p>tar cf - [directory] | ssh [username]@[hostname] tar xf - -C [destination on remote box]</p>\n\n<p>This method gives you lots of options.  Since you should have root ssh disabled copying files for multiple user accounts is hard since you are logging into the remote server as a normal user.  To get around this you can create a tar file on the remote box that still hold that preserves ownership.</p>\n\n<p>tar cf - [directory] | ssh [username]@[hostname] "cat > output.tar"</p>\n\n<p>For slow connections you can add compression, z for gzip or j for bzip2.</p>\n\n<p>tar cjf - [directory] | ssh [username]@[hostname] "cat > output.tar.bz2"</p>\n\n<p>tar czf - [directory] | ssh [username]@[hostname] "cat > output.tar.gz"</p>\n\n<p>tar czf - [directory] | ssh [username]@[hostname] tar xzf - -C [destination on remote box]</p>\n'}, {'answer_id': 104727, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>scp will do the job, but there is one wrinkle: the connection to the second remote destination will use the configuration on the first remote destination, so if you use .ssh/config on the local environment, and you expect rsa and dsa keys to work, you have to forward your agent to the first remote host.</p>\n'}, {'answer_id': 104897, 'author': 'user19218', 'author_id': 19218, 'author_profile': 'https://Stackoverflow.com/users/19218', 'pm_score': 5, 'selected': False, 'text': '<p>I would modify a previously suggested reply:</p>\n\n<pre><code>rsync -avlzp /path/to/sfolder name@remote.server:/path/to/remote/dfolder\n</code></pre>\n\n<p>as follows:</p>\n\n<p>-a (for archive) implies -rlptgoD so the l and p above are superfluous. I also like to include -H, which copies hard links.  It is not part of -a by default because it\'s expensive.  So now we have this:</p>\n\n<pre><code>rsync -aHvz /path/to/sfolder name@remote.server:/path/to/remote/dfolder\n</code></pre>\n\n<p>You also have to be careful about trailing slashes.  You probably want</p>\n\n<pre><code>rsync -aHvz /path/to/sfolder/ name@remote.server:/path/to/remote/dfolder\n</code></pre>\n\n<p>if the desire is for the contents of the source "sfolder" to appear in the destination "dfolder".  Without the trailing slash, an "sfolder" subdirectory would be created in the destination "dfolder".</p>\n'}]
['https://Stackoverflow.com/questions/69411', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']