Search code examples
bashftpwgetexit-code

check existence of ftp file and parse exit code


I need to check if a file exists via wget and test the exit code

Now, I'm running this:

wget -q --spider --ftp-user='ftpuser'--ftp-password='ftpassword' ftp://192.168.1.63/fileexists.txt
echo $? #0

and its return code is 0

But in the case the file does not exist

wget -q --spider --ftp-user='ftpuser'--ftp-password='ftpassword' ftp://192.168.1.63/filenotexist.txt
echo $? #0

its return code is equal 0, even though there isn't

So, I've tried without --spider option and I got 8 as exit code, meaning that the file does not exist

But, if there is one the wget actually downloads it. The problem is if I have a big file to 'check'..

Any ideas?

Thanks


Solution

  • How about using curl?

     curl -I --silent ftp://username:passwd@192.168.1.63/filenotexist.txt >/dev/null
    

    $? is 0 if file exists, $? is not 0 if file doesn't exists.