Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

About this user

« Newer Snippets
Older Snippets »
3 total  XML / RSS feed 

Searching with grep & Spotlight's kMDItemDisplayName


# first a grep regex:
# only match the last part of a complete file path

mdfind '*==*' | grep -i -E '/[^/]*?somestring[^/]*?[0-5][^/]*?$' | sed 's/ /\\ /g' | xargs basename


# let Spotlight search for the fixed part of the search string, and grep for the variable one

mdfind 'kMDItemDisplayName == "*somestring*"wc' | grep -i -E '/[^/]*?[0-5][^/]*?$'



# To search for folders add:
mdfind 'kMDItemKind == "Folder" && kMDItemDisplayName == ...' ...


# To search for files you may add, for example:
mdfind '( kMDItemKind == "*text*"wc || kMDItemKind == "*document*"wc || kMDItemKind == "*image*"wc ) && kMDItemDisplayName == ...' ...


# ... or just ...
mdfind 'kMDItemKind != "Folder" && kMDItemDisplayName == ...' ...



grep-based phrase search with Spotlight

Some examples:


# for mdfind "phrase* " and "phrase! " also work, but not "phrase ! "
mdfind -0 -onlyin ~ "phrase This is a" | xargs -0 grep -ails -E 'This is a phrase!'


mdfind -0 "It's just $8.99!" | xargs -0 grep -ails -E "It's just \\\$8\\.99\\!"


mdfind -0 'Fr\303\251d\303\251ric Chopin' | xargs -0 grep -ails -Z -E 'Fr.d.ric Chopin' | \
xargs -0 basename


Regex-based file/folder search with Spotlight

To get more Spotlight search options use mdimport -A and
mdls /somefile.txt in Terminal.app.



mdfind -onlyin ~ "*==*" | grep -i -E 'part.*?of.*?name'


mdfind -onlyin ~ '*==* && kMDItemContentType == "text"wc' | \
grep -i -E '.*?txt$'


mdfind -onlyin ~ '*==* && kMDItemKind == "Folder"' | \
grep -i -E '.*?photos.*?[0-5]'




« Newer Snippets
Older Snippets »
3 total  XML / RSS feed