Hugo new post Bash function

Hugo has a hugo new helper command that can generate new content files for you. It doesn’t quite do what I want, though, so I’ve added this quick Bash function to my .profile to make it easier to use:

hnp() {
  hugo new "${2:-posts}/$(date +%Y)/$(date +%m)/${1}/index.md"
}

All it does is create a new post file in its own content directory at a date-based path, which is the structure I use for my Hugo sites.

Use it like this:

hnp my-post-slug

And you’ll get a file at content/posts/2022/01/my-post-slug/index.md with initial content like:

---
title: "My Post Slug"
date: 2022-01-15T12:52:06Z
draft: true
---

You can also specify an optional second argument if you don’t want the item at posts:

hnp my-post-slug blog

Will create a new file at content/blog/2022/01/my-post-slug/index.md.


Tech mentioned