lastmod shell scripts and sed

Mar 13, 2025
Tags: blog hugo shellscript

Wrapping Things Up

I never finished my write-up on making a new gallery / image plug-in and frankly I’ve forgotten what I wanted to say about it. I will say that I like the new plug-ins quite a bit. They’ve managed to simplify my workflow when making posts, especially in the games and bleets section. I made some updates to the way the CSS works which you can read about in the changelog.

I also made a pretty cool shell script that solves a very specific problem that isn’t even necessarily a problem, it was something that was working by design, but really didn’t work the way I had hoped.

Lastmod and YOU!

I’m treating a lot of posts in the game section like they are a thread on a social media site. It’s where I first fell in love with journaling about playing games and I wanted to try to recreate that experience here for myself. It’s one of the main reasons I decided to build this website from the ground up, because I really wanted to control the way it worked, tops to tails. When I make a post in games it generates a page bundle with an index.md inside of it. If I were to make another markdown file in that page bundle, it would be added to the completed HTML page and look like a follow-up post, a la classic twitter. Some of these games will be played over a week or two and never booted up again, but others I’m going to play for a while, such as monster hunter: wilds. And when I update that thread, how will anyone know? I added a “recently updated” section to the main page but it didn’t really work, it was just showing recently created, and not in the right order at that.

Enter lastmod. This is a command that you can preconfigure if you’d like to control the specific usage by adding some code to your config file (in my case, hugo.toml). I’ll get into the need for a shell script shortly here, but first to talk about lastmod a little more. What it does is returns the last modified date of any given page. In my hugo.toml file I placed the following parameters:

[frontmatter]
	lastmod =['lastmod', ':fileModTime']

This tells hugo that first we want to check for a lastmod date entry in the pages frontmatter, if that doesn’t exist just use the OS’s last modified file time/date. There is more here that will work with GIT but I’m not bothering with any of that noise. This seems pretty straightforward, using this I should be able to sort the pages by this last modified date and it should show which of these pages was updated last. Well, it doesn’t work on the resulting HTML files, its actually on the markdown files. Furthermore it doesn’t take into account page resources and when they were created. It all makes sense, but since I’m not editing the original file, the index.md of each page bundle, nothing worked like I wanted it. What I could do is go into each index file and edit something or make a change to the lastmod parameter by hand but where is the fun in that?

Enter The Shell Script

This is where my shells script comes in handy. Since I was going to write something to make my life easier, I really wanted it to do as much as possible. My criteria for this thing:

  • Create a new post in the current folder. If no posts exits, create post01.md. If there is a post#.md, create post#+1.md. If I’m in the god-of-war-3 folder and the last post in there is post04.md, then this script will start us off by making post05.md.
  • Add in the front matter to the new post file, since we aren’t using hugo’s built-in archetypes.
  • Needs to update the lastmod parameter in the parents index page.
  • Work in any folder on the terminal

I’m not great at shell scripting so I literally did what anyone would do, I started searching the internet for people asking for similar problems. The first was to figure out how to figure out if there is a post file in the directory and then to create the next one up the number chain. This post on ask ubuntu was practically perfect. It was well written, easy to understand and quickly adaptable to the task at hand. Here is what I ended up with:

#count your existing files
NUMBERATM=`find ./ -type f -name "post*" | wc -l`
#add +1 to create the next higher file
NUMBERATM=$((NUMBERATM+1))

#check if it's below 10 since you need the 0 infront of it
if [ $NUMBERATM -lt "10" ]; then
   var=0$NUMBERATM
else
   var=$NUMBERATM
fi

The next thing to do is to create the file using our variable as a filename. I had tried to use touch to make the file but I needed something that let you add multiple lines. It turns out cat does just that!

#get the date for frontmatter
newdate=$(date +"%Y-%m-%dT%H:%M:%S")

cat > "post${var}.md" << END
+++
title = 'Post${var}'
date = $newdate
draft = true
+++
END

Also a little note about the newdate variable, this is formatting the date in a variable in a way that Hugo likes so I can use it both in the post front matter and the lastmod parameter in index. To do that last part we’re going to use SED! SED SED SED! I’ve never used Sed before but it’s a pretty powerful, old school method of text formatting and replacing. LUCKILY I found someone with a write-up about using it to edit front matter, but for different reasons. This was useful because it got me familiar with the syntax to do a search and replace. However it turns out that sed for macOS is the BSD version so I had to work backwards towards to the correct syntax and ended up with this:

sed -i '' "s/lastmod = .*/lastmod = ${newdate}-08:00/g" index.md

The important thing to remember is that this uses a swap file to archive changes and after -i you have to include what you want for the file extension which are the empty single quotes.

And it works! It works a treat! It makes the posts, updates the lastmod date and gives me a happy little message. Full code below!

#!/bin/zsh

#count your existing files
NUMBERATM=`find ./ -type f -name "post*" | wc -l`
#add +1 to create the next higher file
NUMBERATM=$((NUMBERATM+1))

#check if it's below 10 since you need the 0 infront of it
if [ $NUMBERATM -lt "10" ]; then
   var=0$NUMBERATM
else
   var=$NUMBERATM
fi

#create your file
#touch "post${var}.md"

#get the date for frontmatter
newdate=$(date +"%Y-%m-%dT%H:%M:%S")

cat > "post${var}.md" << END
+++
title = 'Post${var}'
date = $newdate
draft = true
+++
END

echo ""post${var}.md" has been created!"

#sed voodoo to update the lastmod of index
sed -i '' "s/lastmod = .*/lastmod = ${newdate}-08:00/g" index.md

echo "lastmod in index.md has been updated!"

Oh lastly, in my home folder I made a new folder called /Scripts and moved this and quickbleet in and added them to zsh so they work anywhere!