Skip to content

How to execute shell command using SFTP channel in JSch?

I am trying to list all *.xml files in the directory. I do a cd first and then was trying to execute:

find . -type f -name *.xml

But not sure how exactly to do it. There were some example around the Exec channel but is there a way to do the find with SFTP itself?

String username = "abcd";
String password = "pqrst";
String host = "";
int port = 22;
String SFTPWORKINGDIR  = "/xxx/xxx/xxx/xxxx";

Session     session     = null;
ChannelSftp channelSftp = null;
try {
    JSch jSch = new JSch();
    session = jSch.getSession(username, host, port);
    session.setConfig("StrictHostKeyChecking", "no");
    channelSftp = (ChannelSftp) session.openChannel("sftp");
    // List all the *xml file.
    // --------- Want to execute 'find . -type f -name "*.xml" ' here ---------
    /*  Vector fileList = 
    for(int i=0; i<fileList.size();i++){
        LsEntry entry = (LsEntry) fileList.get(i);
} catch (JSchException | SftpException e) {
finally {
    if(session != null) session.disconnect();
    if(channelSftp != null) channelSftp.disconnect();


Your requirements are conflicting.

You cannot execute shell commands with SFTP.
(That’s true in any case, no matter what language or library are you using).

So you have to choose.

  • Either run your find shell command with “Exec channel”.
  • Or use a pure SFTP interface via (and programatically recurse into subdirectories). This approach is a way more laborious and will be magnitudes slower. On the other hand it will be a way more robust. And it is actually the only solution, if you do not have a shell access in the first place. Not to mention that it is platform-independent (what find command is not).