Oleksandr_DJ
Oleksandr_DJ

Reputation: 1515

How can I duplicate files on a SFTP server using JSch?

I faced problem to copy(duplicate) a file from one SFTP folder to another one on the same server.

So, question: Is there a some effective method to do it remotely (i.e. without copying a data to client and vice-versa)?

I am using Java with the JSch library.

Upvotes: 1

Views: 1784

Answers (2)

Paŭlo Ebermann
Paŭlo Ebermann

Reputation: 74750

The SFTP protocol itself does only support data transfer between client and server, not on the server itself.

You can use other parts of the underlying SSH protocol (and JSch's implementation therof) to execute a command on the server, though. In this case, as mentioned by Joop, an Exec channel is the right thing to use. You can use the same Session you have used for your ChannelSftp also for any number of other channels, e.g. for your exec channel. Just make sure to close any channels after use. (Have a look at the Shell, Exec or Subsystem Channel page at the JSch wiki for some more info.

This will of course not work if the server doesn't support command execution, e.g. if it is configured to only support SFTP. Then your only solution is to download and re-upload this file.

Upvotes: 2

Joop Eggen
Joop Eggen

Reputation: 109547

In SFTP one possibly can execute ! ... which is execute local command.

! cp a.txt a-backup.txt

As JSch also can give an SSH connection, even the exclamation sign is moot. For code you might find some starting point in the examples. The Exec sample should do.

Upvotes: 1

Related Questions