Finding Jekyll Posts to Update With Cover Images
2021 January 10

I recently updated my site to make use of cover images on the post listing. I wanted to make sure that I got all of the posts that need to be updated. I used grep to find posts which had an image in them, but no cover attribute in their front-matter.

Firstly, I needed to find all of the posts with images. I could look for the 2 characters that markdown uses for images. All images in markdown are of the form ![alt text](link to image). So I could simply look for matching posts like so:

grep '!\[' _posts/*

Note the backslash so that the regex is properly formatted.

Next to identify posts which already have a cover image in them. Grep is used again to look for “cover” in the front-matter of all posts, and since only filenames are needed the -l/--files-with-matches flag is used.

grep -l cover: _posts/*

Since there are multiple, grep supports using pattern file with the -f flag. But there is no file yet, so process substitution can take the results from the previous operation and sub it into the file. Then negative matches need to be be filtered out, the -v/--invert-match flag is used. Thus the overall command is like so:

grep '!\[' _posts/* | grep -v -f <(grep -l cover: _posts/*)

This prints out a series of lines which state which state which files have images, but don’t yet have the cover attribute set. I hope you found this useful.

Written by Henry J Schmale on 2021 January 10