# How to check if the URL exists?

**URL:** <https://community.unix.com/t/how-to-check-if-the-url-exists/340021>\
**Category:** Shell Programming and Scripting\
**Created:** [February 23, 2014, 2:19am UTC](https://community.unix.com/t/how-to-check-if-the-url-exists/340021 "2014-02-23T02:19:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mohtashims](https://community.unix.com/letter_avatar/mohtashims/32/5_5575768a8748004e209b776fc1b2916d.png) [@mohtashims](https://community.unix.com/u/mohtashims)\
**Post date:** [February 23, 2014, 2:19am UTC](https://community.unix.com/t/how-to-check-if-the-url-exists/340021/1 "2014-02-23T02:19:09Z")

</div>

Hi,

I need to check if the URL exists.

Below is my OS:

```nohighlight
SunOS mymac1 Generic_148888-04 sun4v sparc SUNW,SPARC-Enterprise-T5220

```

I do not have the `curl ` set in the profile nor am i aware about its path.

But i have `wget` . Please help me with params for the same.

Can you help me check if the URL exists from unix shell script?

---

<div class="post-metadata">

**Author:** ![Akshay\_Hegde](https://community.unix.com/user_avatar/community.unix.com/akshay_hegde/32/2771_2.png) [@Akshay\_Hegde](https://community.unix.com/u/Akshay_Hegde)\
**Post date:** [February 23, 2014, 3:17am UTC](https://community.unix.com/t/how-to-check-if-the-url-exists/340021/2 "2014-02-23T03:17:01Z")

</div>

Just an example Try :

```nohighlight
#!/bin/bash

url="http://www.unix.com"

if wget $url >/dev/null 2>&1 ; then
	echo "Url : $url exists..."
else
	echo "Url : $url doesn't exists.."
fi

url="http://www.unixaaaa.com"

if wget $url >/dev/null 2>&1 ; then
	echo "Url : $url exists..."
else
	echo "Url : $url doesn't exists.."
fi

```

---------- Post updated at 02:47 PM ---------- Previous update was at 02:40 PM ----------

You can create function something like this and call

```nohighlight
#!/bin/bash

url(){
	if [! -z "$1"]; then
		wget "$1" >/dev/null 2>&1 
		["$?" -eq 0] && echo "Url : $1 exists..." || echo "Url : $1 doesn't exists.."
	else
		echo "No Arguments..exiting" & exit
	fi
}

url "http://www.unix.com"
url "http://www.unixaaaa.com"

```

---

<div class="post-metadata">

**Author:** ![mohtashims](https://community.unix.com/letter_avatar/mohtashims/32/5_5575768a8748004e209b776fc1b2916d.png) [@mohtashims](https://community.unix.com/u/mohtashims)\
**Post date:** [February 23, 2014, 3:40am UTC](https://community.unix.com/t/how-to-check-if-the-url-exists/340021/3 "2014-02-23T03:40:10Z")

</div>

> [@akshay hegde](#):
>
> Just an example Try :
> 
> ```plaintext
> #!/bin/bash
> 
> url="http://www.unix.com"
> 
> if wget $url >/dev/null 2>&1 ; then
> echo "Url : $url exists..."
> else
> echo "Url : $url doesn't exists.."
> fi
> 
> url="http://www.unixaaaa.com"
> 
> if wget $url >/dev/null 2>&1 ; then
> echo "Url : $url exists..."
> else
> echo "Url : $url doesn't exists.."
> fi
> 
> ```
> 
> ---------- Post updated at 02:47 PM ---------- Previous update was at 02:40 PM ----------
> 
> You can create function something like this and call
> 
> ```plaintext
> #!/bin/bash
> 
> url(){
> if [! -z "$1"]; then
> wget "$1" >/dev/null 2>&1 
> ["$?" -eq 0] && echo "Url : $1 exists..." || echo "Url : $1 doesn't exists.."
> else
> echo "No Arguments..exiting" & exit
> fi
> }
> 
> url "http://www.unix.com"
> url "http://www.unixaaaa.com"
> 
> ```

URL=[https://mybank.net:8443/Services/com/Notification.jws](https://mybank.net:8443/Services/com/Notification.jws)

This only checks for the hostname / port number [[https://mybank.net:8443](https://mybank.net:8443)]. What about the rest of the URL / page [/Services/com/Notification.jws] if it exists or not?

How can we check that?

---

<div class="post-metadata">

**Author:** ![Akshay\_Hegde](https://community.unix.com/user_avatar/community.unix.com/akshay_hegde/32/2771_2.png) [@Akshay\_Hegde](https://community.unix.com/u/Akshay_Hegde)\
**Post date:** [February 23, 2014, 3:51am UTC](https://community.unix.com/t/how-to-check-if-the-url-exists/340021/4 "2014-02-23T03:51:40Z")

</div>

Did you try what I posted in message `#2` ? it does your job, I guess

---

<div class="post-metadata">

**Author:** ![mohtashims](https://community.unix.com/letter_avatar/mohtashims/32/5_5575768a8748004e209b776fc1b2916d.png) [@mohtashims](https://community.unix.com/u/mohtashims)\
**Post date:** [February 23, 2014, 4:12am UTC](https://community.unix.com/t/how-to-check-if-the-url-exists/340021/5 "2014-02-23T04:12:18Z")

</div>

I have a https url which does exist.

`$?` returns 5 with `wget`

Using your code it says "URL DOES NOT EXIST"

The URL is internal to our network so u cant access it but the server running the script can

Why is this the problem?

---

<div class="post-metadata">

**Author:** ![Akshay\_Hegde](https://community.unix.com/user_avatar/community.unix.com/akshay_hegde/32/2771_2.png) [@Akshay\_Hegde](https://community.unix.com/u/Akshay_Hegde)\
**Post date:** [February 23, 2014, 4:20am UTC](https://community.unix.com/t/how-to-check-if-the-url-exists/340021/6 "2014-02-23T04:20:11Z")

</div>

repalce `wget "$1" >/dev/null 2>&1` with `wget -q --no-check-certificate "$1" ` and try  
Wget Error Codes:

```nohighlight
    0 No problems occurred.
    1 Generic error code.
    2 Parse error�for instance, when parsing command-line options, the .wgetrc or .netrc�
    3 File I/O error.
    4 Network failure.
    5 SSL verification failure.
    6 Username/password authentication failure.
    7 Protocol errors.
    8 Server issued an error response.

```

---

<div class="post-metadata">

**Author:** ![mohtashims](https://community.unix.com/letter_avatar/mohtashims/32/5_5575768a8748004e209b776fc1b2916d.png) [@mohtashims](https://community.unix.com/u/mohtashims)\
**Post date:** [February 23, 2014, 8:34am UTC](https://community.unix.com/t/how-to-check-if-the-url-exists/340021/7 "2014-02-23T08:34:59Z")

</div>

Thank you.
