The os.Stat Function in Hugo

Published: Oct 29, 2018
Updated: May 3, 2021

The os.Stat function in Hugo (see the docs) is useful for getting info on a file.

For example, say I wanted to get the info on this page. The following shortcode:

{{ $file := .Get 0 }}
{{ $stat := os.Stat $file }}

<p>
  <strong>Name</strong><br>
  <code>{{ $stat.Name }}</code>
</p>
<p>
  <strong>Size</strong><br>
  <code>{{ $stat.Size }}</code> bytes
</p>
<p>
  <strong>Mode</strong><br>
  <code>{{ $stat.Mode }}</code>
</p>
<p>
  <strong>ModTime</strong><br>
  <code>{{ $stat.ModTime }}</code>
</p>
<p>
  <strong>IsDir</strong><br>
  <code>{{ $stat.IsDir }}</code>
</p>

Would give this output:

Name
index.md

Size
862 bytes

Mode
-rw-r--r--

ModTime
2024-10-21 13:50:46.952684061 +0000 UTC

IsDir
false