There are enough valid reasons to explicitly disable automatic directory indexes in apache or other web servers. Or, for example, you might want to only include certain file types in the index. In such cases you might still want to have a statically generated index.html file for specific folders.
tree
tree is a minimalistic utility that is available on most unix-like systems (ubuntu/debian: sudo apt install tree, mac: brew install tree, windows: zip). tree can generate plain text, XML, JSON or HTML output.
Generate an HTML directory index one level deep:
tree -H '.' -L 1 --noreport --dirsfirst -T 'Downloads' -s -D --charset utf-8 -o index.html
Only include specific file types that match a glob pattern, e.g. *.zip files:
tree -H '.' -L 1 --noreport --charset utf-8 -P "*.zip" -o index.html
The argument to
-His what will be used as a base href, so you can pass either a relative path such as.or an absolute path from the web root, such as/files.-L 1limits the listing to the current directory only.--dirsfirstputs the directories first,-Tsets a custom title,-sincludes the file sizes and-Dincludes the modified dates. You can additionally specify the format of the date with e.g.--timefmt '%d-%b-%Y %H:%M'(seeman strftimein your terminal for details).
tree does not expose a flag to disable the credits in the HTML footer, but you can cut it out by piping through sed:
# delete 7 lines starting with the line matching <hr>
tree -H '.' -L 1 --noreport --charset utf-8 | sed -e '/<hr>/,+7d' > index.html
For all supported options see tree --help or man tree in a shell.
Generator script with recursive traversal
I needed an index generator which I could style the way I want, so ended up writing this script (python 3) which in addition to having customisable styling can also recursively generate an index.html file in all the nested subdirectories (with the --recursive or -r flag). The styling borrows heavily from caddyserver’s file-server module. It includes last modified time and is responsive in mobile viewports.