Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a way to ask mdfind to search for filenames with an exact filename ?

At some point in a pipe I have this:

(filenames are produced here) | while read f; do mdfind -name "$f" | grep -E "/$f";

I have two problems:

  • if the filename contains a +:

    grep: repetition-operator operand invalid

  • if the filename contains a parenthesis, the result is empty.

How can I fix this ?

share|improve this question
up vote 1 down vote accepted

I think that you don't need any regular expressions here. Just try to search for a fixed string with grep. You can enable fixed string matching with the -F switch.

Given that your command line looks like

(filenames are produced here) | \
while read f ; \
    do mdfind -name "$f" | grep -F "/$f" ; \
share|improve this answer

Use the power of the man page. ;)

The man page of mdfind says:

 -literal    Force the provided query string to be taken as a literal query string, without interpretation.

Without that option mdfind interprets characters like "*", "+" and "()" as regular expression.

share|improve this answer
But is the filename a metadata attribute ? – alecail Jan 22 '13 at 11:59
This works:mdfind -literal 'kMDItemFSName == .. but it is so slow that it is not usable. mdfind -name returns almost immediately, -literal takes easily 30 seconds on the request I tried. – alecail Jan 22 '13 at 12:11
@Antoine, that is weird. It should be faster not to have to consider alternatives. – vonbrand Jan 22 '13 at 14:48
@vonbrand I'll have a look later - I'm currently moving a few TB of files - it may be related – alecail Jan 22 '13 at 14:52
@vonbrand I just tested on a non busy system and it's incredibly slow compared to mdfind -name. – alecail Jan 25 '13 at 11:26

Your Answer


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.