Insert a Line for Every File in a Directory

Posted on Aug 15, 2018

I recently imported this entire blog from Wordpress to Hugo.

The move itself was smooth and there are a million blog posts out there explaining exactly how to do it.

However, I wanted to do something different, I wanted to add a “warning” for every imported post saying that it might be broken and that you should be aware of that.

I wanted to add imported: true to every file parameters.

In my template, I am checking for {{ .Params.imported }} and showing a warning if it exists. New posts (like this one) don’t have that parameter thus don’t have that warning.

First thing first, I needed to install gnu-sed on my Mac since the sed that comes with it does not have -i or in-place mode.

brew install gnu-sed

Now that it’s installed I have gsed command available.

If you are running linux, you can skip this step altogether, you already have the right one installed.

Now that I have the right command, I can actually go through and execute what I want.

cd content/posts
find . -maxdepth 1 -type f -exec gsed -i '2i imported: true' {} \;

This added imported: true to the second line of every file. Now I have a nice warning for every reader that comes through one of the imported posts.