One Liner For Directory Listing on GitHub Pages

Posted by Jen Tong on April 28, 2015

I recently moved my blog from the VPS that I used for over a decade to GitHub Pages. They’re really cool, especially the automagic deploys of the magic gh-pages branch. But, they were missing one feature I sometimes use: directory listings (because sometimes I’m lazy). I would write a Jekyll plugin, but GitHub Pages does not run arbitrary plugins.

I came up with an embarrassingly hacky solution that I thought I’d share.

Create a file named _makeindex.sh with this content. Start it with an underscore to prevent Jekyll from serving it.

#!/bin/sh

ls | egrep '(jpg|png)' | \\ #filter the content you want in the index
perl -e 'print "<html><body><ul>"; while(<>) { chop $_;  print "<li><a href=\"./$_\">$_</a></li>";} print "</ul></body></html>"' > index.html
  1. Tweak the grep statement in the first line to display what you want to serve.
  2. Run the script before you deploy.
  3. Feel dirty for doing something so hacky. Wash with extra soap.