Return only one line per file with grep in Terminal
It's not working as expected because Macs use a BSD version of grep while the answers you're seeing are for GNU grep as found on Linux. They're very similar but not identical, and they handle -m differently. BSD grep treats -m as covering the full output, while GNU's version is per-file.

One way to get the result you describe is like this:

find . -type f -exec grep -H -m1 library '{}' \;


This uses find to get the path to every file in . (recursively, so it gets all sub-directories) an then runs grep -m1 on each of them. The -type f tells find to only get regular files, not directories and other things. The -H tells grep to print the names of matching files, not just the matching text.

Another way would be to install GNU grep, using Homebrew.