Skip to content

Instantly share code, notes, and snippets.

@haxwithaxe
Last active February 10, 2016 00:46
Show Gist options
  • Save haxwithaxe/66d18082123f765d659a to your computer and use it in GitHub Desktop.
Save haxwithaxe/66d18082123f765d659a to your computer and use it in GitHub Desktop.
Create static html directory indexes
#!/bin/bash
# generate an html index of a directory
#
# Arguments:
# $1 (optional): Path to create the index.html relative to.
#
# Outputs:
# STDOUT: HTML index of files in $path
#
path=${1:-$PWD}
cat - <<EOHTML
<html>
<head>
<style>
table {
text-align: left;
}
.middle-col {
padding-left: 10px;
padding-right: 10px;
}
td.right-col {
text-align: right;
}
</style>
</head>
<body>
<div class="panel">
<h1 class="panel-head">Index of files</h1>
<table class="table table-striped table-condensed">
<tr><th class="left-col">File</th><th class="middle-col">Modified</th><th class="right-col">Size in kB</th></tr>
EOHTML
find $path -maxdepth 1 -regex '.*/[^.][^/]*' -not -path $PWD -not -name 'index.html' -printf '<tr><td class="left-col"><a href="%f">%f</a></td><td class="middle-col">%TY-%Tm-%Td %TH:%TM</td><td class="right-col">%s</td></tr>\n'
cat - <<EOHTML
</table>
</div>
<br/>
<br/>
<div><small>Last Modified: $(date +%c)</small></div>
</body>
</html>
EOHTML
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment