mdfind search string with underscores not working

I am migrating my method to find strings in files to mdfind.

But when I search for a string with underscores it does not find the file I should be finding.


Setup:

A file in a lone directory with the following contents:

this_is_a_test
thisIsATest


When I search for thisisatest it works

% mdfind -onlyin . thisisatest
..../test/afile


but if I search for this_is_a_test it does not work, I get no output returned

% mdfind -onlyin . this_is_a_test


Is there some secret sauce to search for strings with underscores?

Any help is appreciated.

MacBook Pro with Touch Bar

Posted on Jan 27, 2021 11:12 AM

Reply
Question marked as Top-ranking reply

Posted on Jan 27, 2021 12:40 PM

The mdfind command is for locating files based on substrings of their filename or certain specified document attributes, associated with the file. It has nothing to do with actual document content. For that, you want one of the grep family.


You run the mdls command on a given filename and the keywords for those attributes are shown for the particular document.


Let's say you want to search for shell script category documents:


mdls -onlyin . 'kMDItemContentTypeTree == "public.shell-script"'


This will list the full POSIX path of every script file (e.g. zsh, sh, py, pl, etc.) that is present. Doesn't care about the internal contents.

5 replies
Question marked as Top-ranking reply

Jan 27, 2021 12:40 PM in response to cancer22

The mdfind command is for locating files based on substrings of their filename or certain specified document attributes, associated with the file. It has nothing to do with actual document content. For that, you want one of the grep family.


You run the mdls command on a given filename and the keywords for those attributes are shown for the particular document.


Let's say you want to search for shell script category documents:


mdls -onlyin . 'kMDItemContentTypeTree == "public.shell-script"'


This will list the full POSIX path of every script file (e.g. zsh, sh, py, pl, etc.) that is present. Doesn't care about the internal contents.

Jan 27, 2021 1:15 PM in response to cancer22

The mdfind command uses the long-form attribute names, and the Spotlight and Finder search interfaces allow you to use shorter more language-focused keywords. I can usually find what I want from the Spotlight or Finder search boxes without resorting to mdls on the command-line.


From the Spotlight or Finder window search panels, you can locate documents containing particular single-word, or quoted strings. This is far faster when appropriately used than playing with e/grep.

Jan 27, 2021 12:17 PM in response to VikingOSX

The filename works, but rather I was concerned about looking at the contents of the file.

I'm still seeing inconsistent behavior. See below:


% echo "this_is_a_test\nthisisatest" > afile                 
% mdfind -onlyin . this_is_a_test
% cp afile bfile.txt
% mdfind -onlyin . this_is_a_test
% touch cfile
% cat afile >> cfile
% mdfind -onlyin . this_is_a_test
% touch find_this_lethargic_underlined.txt
% mdfind -onlyin . find_this_lethargic_underlined
.../find_this_lethargic_underlined.txt
% cat afile >> find_this_lethargic_underlined.txt
% mdfind -onlyin . this_is_a_test                
% cat find_this_lethargic_underlined.txt
this_is_a_test
thisisatest
% echo "this_is_not_a_test" >> find_this_lethargic_underlined.txt
% mdfind -onlyin . this_is_not_a_test                            
.../find_this_lethargic_underlined.txt



This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

mdfind search string with underscores not working

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.