You may use following command line functions to quickly find and open relevant file. I find this easier than typing long string of query in spotlight window.
Add following functions in ~/.bash_aliases
.
# find any item matching search query in file name
spot(){
mdfind "kMDItemDisplayName=='*$1*'cd";
}
# restrict to files under (recursive) a specific path
findpaper(){
mdfind -onlyin "/Users/foo/articles" "kMDItemDisplayName=='*$1*'cd";
}
# default to open the first entry unless 2nd positional argument is given
openpaper(){
FILEID=$(printf "%sp" ${2:-1})
open "$(mdfind -name -onlyin "/Users/foo/articles" "kMDItemDisplayName=='*$1*'cd" | sed -n "${FILEID}")"
}
Now, either source ~/.bash_aliases
or open a new terminal load functions. To search for files with words, pie and 2016 anywhere in the file name, do
spot pie*2016 #or
spot 2016*pie
There is no need to prepend or append *
to your query as the search pattern, '*$1*'
already tags wild card entry at beginning and end of your query. Additional cd
is for case insensitive and ignoring diacritical marks, e.g., fred
will return both, Frédéric and FrEDeric.
findpaper
will restrict search to results under a specific path (recursive) while openpaper pie*201
will open a (or first of multiple results) search result or openpaper pie*201 3
will open third result entry. To avoid opening bash scripts or other non-document files, you may restrict file contents by additional search attributes. See File Metadata Query Expression Syntax and https://ss64.com/osx/mdfind.html for using other search operators.