Brain Phrye
code cooking diy fiction reviews personal politics tools

EOL at EOF

Doesn’t happen often but every now and then I’m confronted with a bunch of files that should have a newline as the last char of the file, but some or all of them don’t. A few months back it was postfix config files (postconf assumes there’s a newline at the end when adding a new config option), and today it was the source files for this blog. In the latter case not exactly required, but “nice to have.”

The solution, surprisingly, is ed. It will always append a newline to a file if it’s missing.

1
for f in **/*.md; do ed -s $f <<< w; done

That does have some zsh-isms in it, so you can also do this on more limited shells:

1
for f in $(find . -name '*.md'); do echo w | ed -s $f; done

The downside is that this will update the timestamps on every file. I’ve never had a situation where I cared, but some touch magic would probably fix it.