Search code examples
c#scpssh.net

Copying files to remote and creating directories and recursive copying


I am using SSH.NET to copy files using an ScpClient. This works fine. However, if I want to copy files to a destination that doesn't exist yet, there doesn't seem to be a way create the destination path. I am thinking this is so because it's a limitation of SCP itself.

Is there a way around this, or do I need to use an SshClient to first mkdir any necessary paths?

Also, there doesn't seem to be a way to copy files recursively (-r flag), but rather, I must copy files individually. Is that the case as well?

Edit: There is ScpClient.Upload() that takes in a source and destination path, so that can be done.

The documentation on the SSH.NET github page is pretty limited.


Solution

  • First, if you have SCP, you most likely have SFTP too. SFTP and SCP both typically come with an SSH server. So if you have SFTP too, use that, as SFTP is a complete protocol that supports not only file transfers (unlike SCP) but also all other file operations, like directory creation.


    Alternatively, as you have found out, if you have a shell access (what is also rather common, if you have SCP access), use shell commands for creating the directly (that is mkdir on Linux shells).