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.
2Why 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 – 2014-12-08T22:16:17.670
2Adding 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 – 2014-12-08T23:06:33.373
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 – 2014-12-09T00:17:33.007
1I think I'll try @AFH approach. I'll download the file but send it to /dev/null and grep for error messages. – drum – 2014-12-09T00:30:15.623