Reputation: 1141
I need to check and see if a certain file is available for download on an FTP server. I've been using wget
's --spider option to achieve this with HTTP but, this only works with 404 errors, which FTP does not use.
Current Code (Not Working):
file1="12345.tar"
abc1="http://some.website/data/${file1}"
if wget --no-cache --spider --user=username --password='password' ${abc1} >/dev/null 2>&1; then
echo "File ${abc1} exists. Let's get it!"
bash run.sh
else
echo "File ${abc1} doesn't exist. Exiting script..."
exit 0
fi
How can I check ("spider") to see if a file is available on an FTP server? I know that the "404" version of FTP is 550 (No such file or directory).
Upvotes: 0
Views: 2479
Reputation: 76
For FTP:
Response code: Service ready for new user (220)
Response code: 150 Here comes the directory listing / opening binary data connection
For HTTP:
replace 220 or 150 with HTTP/1.1 200 OK
#!/bin/bash
url="ftp://path/to/some/file.something"
function validate_url(){
if [[ `wget -S --spider $url 2>&1 | grep '150'` ]]; then exit_status=$?; fi
if [[ $exit_status == 0 ]]; then
echo "FTP location exists"
elif [[ $exist_status == 1 ]]; then
echo "FTP location not available"
fi
}
validate_url
Upvotes: 2
Reputation: 1141
Here is what I was able to get working:
file1="12345.tar"
abc1="http://some.website/data/${file1}"
check=`wget --no-cache --spider --user=username --password='password' ${abc1} 2>&1 | awk '{print $1}' | head -n 24 | tail -1`
if [ ${check} -eq "No" ]; then
echo "File ${abc1} doesn't exist. Exiting script..."
exit 0
else
echo "File exists!"
fi
Upvotes: 0