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

后端 未结 2 1556
闹比i
闹比i 2020-11-28 20:36

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

相关标签:
2条回答
  • 2020-11-28 20:45

    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
    
    0 讨论(0)
  • 2020-11-28 20:45

    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.

    0 讨论(0)
提交回复
热议问题