I want to search a folder of PDFs to find a particular string and get back a list of matches with a little of the surrounding context.


mdfind -onlyin ~/Dropbox/PDFs 'term' >> ~/Dropbox/searches/term.txt

I can get a list of files that contain a match, but I would like to see a little of the surrounding context so that I can tell a little bit more about the match. Ideally, with the filename prepended.

Something like:

~/Dropbox/PDFs/file1.pdf: here is the term you were searching
~/Dropbox/PDFs/file1.pdf: with term limits
~/Dropbox/PDFs/file2.pdf: the German term for a 
~/Dropbox/PDFs/file3.pdf: come up with a term for

1 Answer 1


mdfind will only ever return filenames, it doesn't display any content, nor does it possess the ability to do so in looking at its man page.

So instead of using mdutil, I'd suggest using a pdfgrep instead. This tool is available via brew:

$ brew install pdfgrep

Once installed it works like this:

$ pwd

$ pdfgrep website -r
./docs/misc/Web-Shells-rev2.pdf:Say I run into the following website:
./docs/misc/Web-Shells-rev2.pdf:home directory of a website for shared host web servers:
./docs/misc/Web-Shells-rev2.pdf:Many websites with upload capabilities still rely on client side validation (javascript). These are
./docs/misc/Web-Shells-rev2.pdf:reverse shell I've hosted the file on one of my websites. Just remember to rename the extension
./docs/misc/Web-Shells-rev2.pdf:the output file, we could overwrite IIS log files or even deface websites. Elements the user can

pdfgrep has the normal compliment of switches to do recursive -r as well as ignore case -i, or even do regex searches.

  • This is wrong. It should say, mdfind uses two query languages that are so poorly documented, almost everyone that tries searching files by content fails to get the query correct and concludes mdfind can't display content. This is correct in that you need a grep like tool so show content around the matched term. Spotlight does not expose that level of detail - only if the term was indexed in a specific file.
    – bmike
    May 23, 2020 at 12:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .