URL이 존재하는지 확인하세요.

URL이 존재하는지 확인하세요.

URL을 다운로드하지 않고 존재하는지 확인하고 싶습니다. 나는 아래를 사용하고 있습니다 curl:

if [[ $(curl ftp://ftp.somewhere.com/bigfile.gz) ]] 2>/dev/null;
 then
  echo "This page exists."
 else
  echo "This page does not exist."
fi

또는 다음을 사용하십시오 wget.

if [[ $(wget ftp://ftp.somewhere.com/bigfile.gz) -O-]] 2>/dev/null;
 then
  echo "This page exists."
 else
  echo "This page does not exist."
fi

URL이 존재하지 않으면 완벽하게 작동합니다. 존재하는 경우 파일이 다운로드됩니다. 제 경우에는 파일이 매우 커서 다운로드하고 싶지 않습니다. URL이 존재하는지 알고 싶습니다.

답변1

당신은 가깝습니다. 이 문제를 해결하는 올바른 방법은 다음을 사용하는 것입니다.머리방법.

컬 사용:

if curl --head --silent --fail ftp://ftp.somewhere.com/bigfile.gz 2> /dev/null;
 then
  echo "This page exists."
 else
  echo "This page does not exist."
fi

아니면 wget을 사용하세요:

if wget -q --method=HEAD ftp://ftp.somewhere.com/bigfile.gz;
 then
  echo "This page exists."
 else
  echo "This page does not exist."
fi

답변2

--spider도구 와 함께 인수를 사용하는 것이 가장 좋다고 생각합니다 wget. 또한 wget경량 버전의 도구(BusyBox 내) 에서도 작동합니다 .

예:

URL="http://www.google.com"

if wget --spider "${URL}" 2>/dev/null; then
    echo "OK !"
else
    echo "ERROR ! Online URL ${URL} not found !"
fi

답변3

curl --head 헤더만 얻으려면 (HTTP FTP FILE)을 시도하십시오 !

status=$(curl --head --silent ftp://ftp.somewhere.com/bigfile.gz | head -n 1)

if echo "$status" | grep -q 404
  echo "file does not exist"
else
  echo "file exists"
fi

답변4

echo $(curl --head --silent <url> | head -n 1)

관련 정보