Mdfind tool : consider in the same time, to perform researching of files, a substring of the filename and the insensitive case for these substrings

catalinafindersearchspotlightterminal

Is it possible to specify, in the context of files searching on my MacOS Catalina 10.15.3, a way to combine in the same time a potential substring of filename and the case insensitive of this substring ?

For the moment, I know the command :

1) mdfind -name 'substring'for the substring

2) mdfind 'kMDItemFSName == substring for insensitive case

But I wonder if I can combine them (like the classical locate -i substring command).

UPDATE 1 : I can't explain the bad working of my mdfind command compared to classical command locate. Indeed, for example, with the classical locate command, I get a lot of files with substring *include.h*

by doing :

    $ locate include.h

/Applications/MATLAB_R2016b.app/help/bugfinder/ref/includeinclude.html
/Applications/MATLAB_R2016b.app/help/codeprover/ref/includeinclude.html
/Applications/MATLAB_R2016b.app/help/coder/ref/coder.cinclude.html
/Applications/MATLAB_R2016b.app/help/simulink/slref/coder.cinclude.html
/Applications/MATLAB_R2016b.app/polyspace/examples/cxx/Demo_C/sources/include.h
/Applications/MATLAB_R2016b.app/polyspace/examples/cxx/Demo_C_Single-File/sources/include.h
/Applications/MATLAB_R2016b.app/polyspace/examples/cxx/Demo_C_Workflow/sources/includes/include.h
/Applications/MATLAB_R2016b.app/polyspace/examples/cxx/Demo_Cpp/sources/include.h
/Applications/Xcode.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS.sdk/usr/include/libxml2/libxml/xinclude.h
/Applications/Xcode.app/Contents/Developer/Platforms/AppleTVSimulator.platform/Developer/SDKs/AppleTVSimulator.sdk/usr/include/libxml2/libxml/xinclude.h
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/cffi/_cffi_include.h
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/gems/2.6.0/gems/libxml-ruby-3.1.0/ext/libxml/ruby_xml_xinclude.h
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/apache2/mod_include.h
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/libxml2/libxml/xinclude.h
/Applications/Xcode.app/Contents/Developer/Platforms/WatchOS.platform/Developer/SDKs/WatchOS.sdk/usr/include/libxml2/libxml/xinclude.h
/Applications/Xcode.app/Contents/Developer/Platforms/WatchSimulator.platform/Developer/SDKs/WatchSimulator.sdk/usr/include/libxml2/libxml/xinclude.h
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/include/libxml2/libxml/xinclude.h
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/usr/include/libxml2/libxml/xinclude.h
/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/gems/2.3.0/gems/libxml-ruby-2.9.0/ext/libxml/ruby_xml_xinclude.h
/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/apache2/mod_include.h
/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/libxml2/libxml/xinclude.h
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/cffi/_cffi_include.h
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/gems/2.6.0/gems/libxml-ruby-3.1.0/ext/libxml/ruby_xml_xinclude.h
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/apache2/mod_include.h
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/libxml2/libxml/xinclude.h
/Library/WebServer/share/httpd/manual/mod/mod_include.html
/Library/WebServer/share/httpd/manual/mod/mod_include.html.en
/Library/WebServer/share/httpd/manual/mod/mod_include.html.fr.utf8
/Library/WebServer/share/httpd/manual/mod/mod_include.html.ja.utf8
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/cffi/_cffi_include.h
/System/Library/Templates/Data/Library/WebServer/share/httpd/manual/mod/mod_include.html
/System/Library/Templates/Data/Library/WebServer/share/httpd/manual/mod/mod_include.html.en
 ...

whereas with the defined personal command locatedefined by :

#!/bin/bash

if [ "$2" != "" ]; then
  mdfind -name "$1" -onlyin "$2"
else
  mdfind -name "$1"
fi

Then, if I do a : $ locate include.h

I get only one result :

/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/cffi/_cffi_include.h

Same thing with : $ locate 'include.h'

Why many results are missing using mdfind command instead of standard locate command ? It seems that I have to specify the substring with an option for mdfind but I don't know for the moment this option.

Any feedback or help would be nice.

Best Answer

Would mdfind -interpret somestring work?

How 'bout a script?

#!/bin/bash

if [ $# -lt 1 ]; then
    echo "$(basename $0) portion-of-filename"
    exit
fi

find / -type f -print 2>/dev/null | gawk -F'/' -v searchFor="$1" '
BEGIN{
    IGNORECASE=1
}
searchFor {
    base=$NF
    if (index(tolower(base), tolower(searchFor))) {
        printf("%s\n",$0)
    }
}
'