Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

Is there a way to search for an exact phrase using the mdfind utility? For example, I created two text documents titled "test1" and "test2". The contents of "test1" are:

I love Apple

And the contents of "test2" are:

Apple love I

When I type this in terminal (I placed both files in ~/Documents):

mdfind "I love Apple" -onlyin ~/Documents

I get:

~/Documents/test1.txt
~/Documents/test2.txt

How would I search for the exact phrase "I love Apple" so mdfind only returns results containing those words in that order (in this case only "test1.txt")?

share|improve this question
add comment

2 Answers 2

up vote 5 down vote accepted

You need to escape your quotes like so:

mdfind \"I love Apple\" -onlyin ~/Documents

This results in just the one document being found:

~/Documents/test1.txt

Without escaping them, I don't think the quotes actually get passed to the mdfind command, they're just interpreted by your shell to say that I love Apple is a single argument. With the backslash-escaping, the argument then includes the quote characters.

share|improve this answer
    
Thanks so much! –  qegal Oct 15 '12 at 2:26
add comment

The double quotes can also be placed inside single quotes:

mdfind '"exact phrase"'

This would search only in the contents:

mdfind 'kMDItemTextContent=="*exact phrase*"'
share|improve this answer
    
Thanks. This worked too, but drfrogsplat answered first. –  qegal Oct 15 '12 at 6:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.