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

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


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.