How to check if a url exists with a wrapper and possibly curl?

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

+34
shell curl
Aug 30 2018-12-12T00:
source share
2 answers

Using --fail will make the exit status nonzero if the request fails. Using --head will avoid loading the contents of the file because it is not needed for this check. Using --silent avoids the state or errors when emitting 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 
+65
Aug 30 2018-12-12T00:
source share

I find wget a better tool for this than CURL; there are fewer options to remember, and you can really check its truth value in bash to make sure that 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 causes wget to simply check the file, not load it, and 2> /dev/null disables the output of wget stderr.

+13
Apr 24 '15 at 22:52
source share



All Articles