WCM Goodie: bash script for validating web redirects

Posted by on March 01, 2012

Today a client and I needed to validate a large list of redirects for their site so I created this simple bash script. I thought it was worth sharing! Unix is such a great tool :)


INPUT=links.txt
OLDIFS=$IFS
IFS=,
[ ! -f $INPUT ] &while read link
do
resp=$(curl -L –write-out “%{http_code} | %{num_redirects} | %{url_effective}” –silent –output /dev/null $link)
echo “$link | $resp ”
done < $INPUT
IFS=$OLDIFS

To use this script just put each link you want to run on a separate line in the file: links.txt

With a links.txt file like:

http://rivetlogic.com/
http://blogs.rivetlogic.com/
http://www.google.com
http://www.sesamestreet.com/

You will get with the following pipe delimited output (URL, Status Code, # of redirects to destination, final destination)

http://rivetlogic.com/ | 200 | 0 | http://rivetlogic.com/
http://blogs.rivetlogic.com/ | 200 | 0 | http://blogs.rivetlogic.com/
http://www.google.com | 200 | 0 | http://www.google.com
http://www.sesamestreet.com/ | 200 | 1 | http://www.sesamestreet.org/

Trackbacks

Trackbacks are closed.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>