Skip to main content

The os.Stat function in Hugo

Published: 2018-10-29 | Lastmod: 2019-09-12

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
884 bytes

Mode
-rw-r--r--

ModTime
2020-02-22 19:29:49.656560139 +0000 UTC

IsDir
false


Next: Resources for learning to code
Previous: Use Snap to install the Hugo edge version on Fedora and Ubuntu