2

I'm writing a sh script to download a file from a Windows SFTP server. However, before doing so I want to check if the file exists.

There were many questions out there that suggests downloading winexe, or doing ssh host@host test -f file and other alternatives, but all of them would not work in my case. I'm not allowed to install anything and must use out-of-the-box utils.

  • 2
    Why check the existence of the file beforehand? Can't you request the down-load and process a failure in the same way as you would handle it if the file were missing on a pre-check? – AFH Dec 8 '14 at 22:16
  • 2
    Adding to what @AFH said, the file could disappear between when you check and when you download it. Or the file could exist, but you could lack permission to read it. Checking whether the file exists, separately from trying to download it, may not be as useful as you think. – Kenster Dec 8 '14 at 23:06
  • The reason why I didn't do it this way is because there is a lot of pre-existing processes already set up that requires the file to exist before reaching the download portion. Don't ask me why it is this way. It was already like this when I came in @__@ – drum Dec 9 '14 at 0:17
  • 1
    I think I'll try @AFH approach. I'll download the file but send it to /dev/null and grep for error messages. – drum Dec 9 '14 at 0:30
1

The standard (OpenSSH) SFTP client does not offer a function to test a file existence or anything close to it.


I'm assuming your only interface is the SFTP protocol to make the solution platform-independent (as you are connecting to Windows). So solutions like executing test over an SSH terminal are not viable (as you have confirmed yourself).


You can workaround that by using an SFTP command with a little overhead that fails when the file does not exist. And test for the sftp exit code (1 for error, 0 for ok).

You could abuse the df (disk free) command for that.

echo "df myfile.txt" | sftp -b - example.com
if [ $? -eq 0 ]
then
    echo "File exists"
else
    echo "File does not exist"
fi

Though df command uses a proprietary OpenSSH extension of the SFTP protocol internally (statvfs@openssh.com). So you have to test first, if your Windows SFTP server supports that. Obviously if you use OpenSSH (MS Win32 build or Cygwin), you are ok.


If the df won't work, the only other option is to try to download the file.

Ideally move your download code before all "pre-existing processes already set up that requires the file to exist".

If that's not possible, you need to download the file twice. First to test its existence. And later again to actually download it.

Fake download to test file existence:

echo "get myfile.txt /dev/null" | sftp -b - example.com
if [ $? -eq 0 ]
then
    echo "File exists"
else
    echo "File does not exist"
fi

Replace the /dev/null with an actual path, if you can merge the test and download steps into one.


The SFTP protocol as such can test file existence. So if you have other SFTP client than OpenSSH handy, you might be able to do this more easily.

For example the lftp has the find command:

lftp -c "open sftp://example.com ; find myfile.txt"
if [ $? -eq 0 ]
then
    echo "File exists"
else
    echo "File does not exist"
fi

For a similar question, see Test if any file was downloaded with SFTP command.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.