1

I was wondering if there is a way to copy files on an SFTP server to another directory on the same SFTP server. I want to do this without getting the file in a client and then setting it in the other folder. Of course this would work fine but I guess that this would produce more overhead, so I would like to avoid this if at all possible. I'm currently working with Spring integration which is based on JCraft JSch.

So far I haven't been able to find any way to do this without an intermediary.

Another approach would be to open an SSH channel and just use the cp command but well that's not too pretty either in my opinion.

Thanks in advance!

Martin Prikryl
  • 167,268
  • 50
  • 405
  • 846
user3157264
  • 99
  • 3
  • 11
  • You might be able to get this behavior by using a site-to-site transfer (_a.k.a._ "FXP") _with that same server itself_. _I.e._ your FTP client opens two FTP sessions to the server; one session tells the server to use a passive data transfer (_e.g._ for receiving the file), the other session tells the server to do an active data transfer (_e.g._ for sending the file), and you use the address/port from the passive data response in the active data command. – Castaglia Jun 06 '16 at 19:29
  • Possible duplicate of [How do I copy files stored in a remote SFTP server to another folder in the same remote server using Java?](https://stackoverflow.com/questions/45004008/how-do-i-copy-files-stored-in-a-remote-sftp-server-to-another-folder-in-the-same) – Roddy of the Frozen Peas Nov 09 '18 at 23:16

1 Answers1

1

A core SFTP protocol does not support duplicating a remote file.

There's a draft of copy-file extension to the protocol, but that's supported by only few SFTP servers (ProFTPD mod_sftp and Bitvise SFTP server for example).

In the most widespread OpenSSH SFTP server it is supported only by very recent version 9.0.

And it's also not supported by the JSch library.


See also my answer to How can I copy/duplicate a file to another directory using SFTP?


So actually using the cp shell command over an "exec" channel (ChannelExec) is unfortunately the best available approach (assuming you connect to a *nix server and you have a shell access).


If you do not have a shell access, then your only option is indeed to download the file to a local temporary folder and upload it back to the new location (or use streams, to avoid a temporary file). See also:

Martin Prikryl
  • 167,268
  • 50
  • 405
  • 846