Auto index html bash script

Alec Jacobson

August 13, 2009

weblog/

Though apache does it on her own, I've run in to a couple situations where it has been nice to generate an html index of a directory on the fly. Save this in a file called auto-index.sh:
#!/bin/bash
# usage: auto-index [dir]
INDEX=`ls -1 $1 | sed "s/^.*/      <li\>\<a\ href=\"&\"\>&\<\\/a\>\<\\/li\>/"`
echo "<html>
  <head><title>Index of $1</title></head>
  <body>
    <h2>Index of $1</h2>
    <hr>
    <ui>
$INDEX
    <ui>
  </body>
</html>"

Then you can run ./auto-index.sh [path to dir] to dump an html index page of that directory to standard out. I didn't bother with specifying an output file, so to dump this to a file called index.html run:
./auto-index.sh [path to dir] > index.html
Note: This could also be considered a much stripped down version of a previous post.