Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

shell

curl

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

like image 782
sorin Avatar asked Aug 30 '12 14:08

sorin


People also ask

How do I check if a URL exists?

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.

What is the ideal way to search if a Web browser exists from the shell prompt?

wget or cURL will do the job.

How can I tell if a URL is accessible in Linux?

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.

What happens when you curl a URL from the command line?

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.


1 Answers

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 
like image 164
Charles Duffy Avatar answered Oct 18 '22 02:10

Charles Duffy