0

I need to check to see if a file exists in a web directory. if the file does exist, I would like to execute a bash script. If it doesn't, exit and don't execute the bash script.

Here's what I have so far (in a bash script):

url="http://website.url/directory/file.txt"

if wget --spider ${url} 2>/dev/null; then
  bash some_bash_script.sh
else
  exit 0
fi
2

You can verify using curl:

url="http://website.url/directory/file.txt"

if curl -f ${url} >/dev/null 2>&1; then
  bash some_bash_script.sh
else
  exit 0
fi

This will work just if the server return a 404 in case the file doesn't exist. If the site don't return a 404 you will need to adapt your if check command, possibly verifying the curl output.

  • This is great! Thank you so much for the reply. I knew that I was close but I wasn't entirely sure. Is there a way that I can see the output/results of the script after it is triggered by the "exists" condition? Right now, it appears that the script is running because the script is still running in the terminal but, I can't see it running. – arnpry Oct 21 '16 at 13:29
  • I'll post this as a new question actually. – arnpry Oct 21 '16 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.