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

后端 未结 2 1565
闹比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

    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.

提交回复
热议问题