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

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

Leave a Comment