Copy files over SSH: Difference between revisions

From Jwiki
No edit summary
No edit summary
Line 8: Line 8:


'''Local to remote:'''
'''Local to remote:'''
<pre>
<syntaxhighlight lang="shell">
tar cjf - -C /path/to/local/ <source-dir> | ssh user@remote-host 'tar xjf - -C /path/to/remote/ <target-dir>'
tar cjf - -C /path/to/local/ <source-dir> | ssh user@remote-host 'tar xjf - -C /path/to/remote/ <target-dir>'
</pre>
</syntaxhighlight>


'''Remote to local:'''
'''Remote to local:'''
<pre>
<syntaxhighlight lang="shell">
ssh user@remote-host 'tar cjf - -C /path/to/remote/ <source-dir>' | tar xjf - -C /path/to/local/ <target-dir>
ssh user@remote-host 'tar cjf - -C /path/to/remote/ <source-dir>' | tar xjf - -C /path/to/local/ <target-dir>
</pre>
</syntaxhighlight>


== Notes ==
== Notes ==
* Add <code>--exclude='pattern'</code> to skip files or directories.
* Add <code>--exclude='pattern'</code> to skip files or directories.
* Replace <code>j</code> with <code>z</code> (i.e., <code>czf</code>, <code>xzf</code>) to use gzip for faster, but less compressed, transfers.
* Replace <code>j</code> with <code>z</code> (i.e., <code>czf</code>, <code>xzf</code>) to use gzip for faster, but less compressed, transfers.

Revision as of 19:48, 1 August 2025


Efficient File Transfer with Tar and SSH

Quickly transfer directories over SSH using tar. Great for migrations or backups.

Commands

Local to remote:

tar cjf - -C /path/to/local/ <source-dir> | ssh user@remote-host 'tar xjf - -C /path/to/remote/ <target-dir>'

Remote to local:

ssh user@remote-host 'tar cjf - -C /path/to/remote/ <source-dir>' | tar xjf - -C /path/to/local/ <target-dir>

Notes

  • Add --exclude='pattern' to skip files or directories.
  • Replace j with z (i.e., czf, xzf) to use gzip for faster, but less compressed, transfers.