I am looking for a simple shell (+curl) check that would evaluate as true or false if an URL exists (returns 200) or not.
Existence of an URL can be checked by checking the status code in the response header. The status code 200 is Standard response for successful HTTP requests and status code 404 means URL doesn't exist. Used Functions: get_headers() Function: It fetches all the headers sent by the server in response to the HTTP request.
wget or cURL will do the job.
curl -Is http://www.yourURL.com | head -1 You can try this command to check any URL. Status code 200 OK means that the request has succeeded and the URL is reachable.
cURL, which stands for client URL, is a command line tool that developers use to transfer data to and from a server. At the most fundamental, cURL lets you talk to a server by specifying the location (in the form of a URL) and the data you want to send.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With