Bash: Make a Web Site Index

An old Bash script that uses awk to construct an index with hyperlinks from the .html files in a web tree. Attempts to use the <title> tag to name each link.

To use, change your directories in the ‘for dir’ line to the directories you want to scan, then run the script: ./myscript.sh > myfile.html.

#!/bin/bash

for dir in html img inc home; do
    echo
    echo "<div class=\"page-section\">"

    newstring=$dir
    firstchar=${newstring:0:1}
    string1=${newstring:1}
    dirname=`echo $firstchar | tr '[a-z]' '[A-Z]'`

    echo "<a id=\"ul${dir}\"></a>"
    echo "<h3>$dirname$string1</h3>"
    echo "<ul class=\"default\">"
    for file in `find $dir -name \*.html -print`; do
        #echo $file
        title=`awk 'BEGIN{IGNORECASE=1;FS="<title>|</title>";RS=EOF} {print $2}' $file`
        if [ -z "$title" ]; then
            title="NO TITLE! - $file"
        fi
        echo "<li><a href=\"/$file\">$title</a></li>"
    done
    echo "</ul>"
    echo "</div>"
done

Leave a Reply

Your email address will not be published.