How to check if an URL exists with the shell and probably curl?

ShellCurl

Shell Problem Overview


I am looking for a simple shell (+curl) check that would evaluate as true or false if an URL exists (returns 200) or not.

Shell Solutions


Solution 1 - Shell

Using --fail will make the exit status nonzero on a failed request. Using --head will avoid downloading the file contents, since we don't need it for this check. Using --silent will avoid status or errors from being emitted by the check itself.

if curl --output /dev/null --silent --head --fail "$url"; then
  echo "URL exists: $url"
else
  echo "URL does not exist: $url"
fi

If your server refuses HEAD requests, an alternative is to request only the first byte of the file:

if curl --output /dev/null --silent --fail -r 0-0 "$url"; then

Solution 2 - Shell

I find wget to be a better tool for this than CURL; there's fewer options to remember and you can actually check for its truth value in bash to see if it succeeded or not by default.

if wget --spider http://google.com 2>/dev/null; then
  echo "File exists"
else
  echo "File does not exist"
fi

The --spider option makes wget just check for the file instead of downloading it, and 2> /dev/null silences wget's stderr output.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsorinView Question on Stackoverflow
Solution 1 - ShellCharles DuffyView Answer on Stackoverflow
Solution 2 - ShellailnlvView Answer on Stackoverflow