2

How to get the return code of SFTP command?

I do this to download all files from a directory. But if the directory is empty the command returns 1

How to fetch the actual code for File not found?

echo 'get * /var/download' | sftp -b - user@host

Or a solution would be to ignore/suppress the error File not found and return 0 (not suppress all errors, only this one)

1 Answer 1

4

OpenSSH sftp returns 0 on success and 1 on error.

No further distinction is provided.


To test, if any file exists in a directory before trying to download them, you can use:

echo "ls /remote/path/*" | sftp -b - [email protected]
if [ $? -eq 0 ]
then
    echo "Files exist, can download now"

    echo 'get /remote/path/* /local/path/' | sftp -b - [email protected]
    if [ $? -eq 0 ]
    then
        echo "Files successfully downloaded"
    else
        echo "Files exist, but failed to download"
    fi
else
    echo "Files do not exist"
fi

For a similar question, see How to check if file exists in remote SFTP server from local Linux script?

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .