Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
add comment

2 Answers

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" ; \
done
share|improve this answer
add comment

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 ? –  Antoine Lecaille 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. –  Antoine Lecaille 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 –  Antoine Lecaille Jan 22 '13 at 14:52
    
@vonbrand I just tested on a non busy system and it's incredibly slow compared to mdfind -name. –  Antoine Lecaille Jan 25 '13 at 11:26
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.