Find files that have YAML frontmatter
July 7, 2015 in SoftwareHere’s a bash one liner that will find all files in the current directory that have YAML front matter:
find . -exec sh -c 'if (head -n 1 "{}" | grep -- --- >/dev/null); then echo "{}"; fi' \;
If you need files that don’t have front matter, grep -v
will do the trick:
find . -exec sh -c 'if (head -n 1 "{}" | grep -v -- --- >/dev/null); then echo "{}"; fi' \;
If you need a different directory, or files of a specific type, find
has lots of flexibility:
find ~/Dropbox/blog -iname '*.md' -exec sh -c 'if (head -n 1 "{}" | grep -v -- --- >/dev/null); then echo "{}"; fi' \;
find -exec
is one command less thanfind | xargs
- I had to resort to
head | grep
to find matches in just the first line.
You can also use for loops, for example in syntax-light fish shell:
for file in source/articles/*.txt
if head -n 1 $file | grep -- --- >/dev/null
echo $file
end
end
Liked the post? Treat me to a coffee