How to check if file exists in remote SFTP server from local Linux script?

24,956

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 ([email protected]). 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.

Share:
24,956

Related videos on Youtube

drum
Author by

drum

Updated on September 18, 2022

Comments

  • drum
    drum over 1 year

    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.

    • AFH
      AFH over 9 years
      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?
    • Kenster
      Kenster over 9 years
      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.