Skip to content

Instantly share code, notes, and snippets.

@tegansnyder
Last active August 23, 2017 18:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tegansnyder/d33d9b3a8dc344ab5871 to your computer and use it in GitHub Desktop.
Save tegansnyder/d33d9b3a8dc344ab5871 to your computer and use it in GitHub Desktop.
Checking for 404s in Apache log

Get a list of 404s in the accesslogs:

sudo -s
grep "HTTP/1.1\" 404" /var/log/httpd/access_log | awk '{print $7 } ' | sort | uniq -c | sort -n > 404s.txt

another way

cut -d'"' -f2,3 /var/log/httpd/access_log | awk '$4=404{print $4" "$2}' | sort | uniq -c | sort -rg > 404s.txt

Checking status of links for 404s:

while read LINE; do
  curl -o /dev/null --silent --head --write-out '%{http_code}' "$LINE"
  echo " $LINE"
done < 404s.txt

A better way:

#!/bin/bash
filename='404s.txt'
filelines=`cat $filename`
echo Start
for line in $filelines ; do
    curl -o /dev/null --silent --head --write-out '%{http_code}' "$line"
    echo ",$line"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment