Bash: counting number of times substring is found in directory

A simple grep can help you recursively search for a substring in all the files of a directory.  Here is an example of looking for multi-doc yaml files:

# recursively search directory, looking for line '---'
# regex: caret means line starts with, dollar sign mean line ends with
grep -sr '^---$'

If you have the additional requirement of needing to count how many times the substring is found within each file, you can pair this with a uniq and sort like below.

grep -sr '^--$' | uniq -c | sort -n

If you wanted a substring instance count for all files (including those with 0 count), you can use grep and wc like below.

for file in $(find . -type f); do cat $file | grep -o '^---$' | wc -l | xargs -I {} printf "%d %s\n" {} $file; done | sort -n

REFERENCES

man page for grep