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!)

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

Searching with grep & Spotlight's kMDItemDisplayName


# search for a file or folder (path) name that contains the specified search string
mdfind 'kMDItemDisplayName == "*apple*"wc'
mdfind -0 'kMDItemDisplayName == "*apple*"wc' | xargs -0 basename
mdfind -0 'kMDItemDisplayName == "*apple*"wc' | xargs -0 basename | grep Apple
mdfind -0 -onlyin ~/Desktop 'kMDItemDisplayName == "*apple*"wc' | xargs -0 basename
mdfind '*==*' | grep -i apple
mdfind '*==*' | grep -i -E '/[^/]*apple[^/]*$'
mdfind '*==*' | grep -i -E '/[^/]*apple[^/]*$' | while read filename; do basename "$filename"; done



# the following is an example of bash process substitution
# http://wooledge.org:8000/BashFAQ (FAQ 20 & 24)
# http://wooledge.org:8000/ProcessSubstitution

# read matched file names into an array

unset -v ar i
dir="$HOME/Desktop"
mdfind_search="'*search_string*'wc"
mdfind_search="\"*search_string*\"wc"   # alternative

while read -d $'\0' filename; do 
   ar[i++]="$filename"
done < <(mdfind -0 -onlyin ${dir} "kMDItemDisplayName == ${mdfind_search}")

echo ${ar[*]}; echo; echo ${ar[0]}; echo ${ar[1]}; echo



# let mdfind & kMDItemDisplayName search for the fixed part of the search string, and grep for the variable one
# read the matched file names into an array

unset -v ar i
dir="$HOME/Desktop"
mdfind_search="'*search_string*'wc"
grep_search="[0-5]"

while read -d $'\0' filename; do
   #if [[ -n $(echo "$filename" | grep -Eos -m1 "$grep_search") ]]; then     # match entire path name with grep
   if [[ -n $(basename "$filename" | grep -Eos -m1 "$grep_search") ]]; then     # match last part of path name with grep
      #ar[i++]="$filename"
      ar[i++]="$(basename "$filename")"
   fi 
done < <(mdfind -0 -onlyin ${dir} "kMDItemDisplayName == ${mdfind_search}") 

echo ${ar[*]}; echo; echo ${ar[0]}; echo ${ar[1]}; echo



# To search only for files add: 'kMDItemKind != "Folder" && ...'

unset -v ar i
dir="$HOME/Desktop"
mdfind_search="'*search_string*'wc"
grep_search="[0-5]"

item1="'*text*'wc"
item2="'*document*'wc"
item3="'*source*'wc"

while read -d $'\0' filename; do
   #if [[ -n $(echo "$filename" | grep -Eos -m1 "$grep_search") ]]; then 
   if [[ -n $(basename "$filename" | grep -Eos -m1 "$grep_search") ]]; then 
      #ar[i++]="$filename"
      ar[i++]="$(basename "$filename")"
   fi 
done < <(mdfind -0 -onlyin ${dir} "kMDItemKind != 'Folder' && kMDItemDisplayName == ${mdfind_search}"); 
#done < <(mdfind -0 -onlyin ${dir} "( kMDItemKind != 'Folder' && ( kMDItemKind == ${item1} || kMDItemKind == ${item2} || kMDItemKind == ${item3}) ) && kMDItemDisplayName == ${mdfind_search}"); 

echo ${ar[*]}; echo; echo ${ar[0]}; echo ${ar[1]}; echo



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

unset -v ar i
dir="$HOME/Desktop"
mdfind_search="'*search_string*'wc"
grep_search="[0-5]"

while read -d $'\0' filename; do
   #if [[ -n $(echo "$filename" | grep -Eos -m1 "$grep_search") ]]; then 
   if [[ -n $(basename "$filename" | grep -Eos -m1 "$grep_search") ]]; then 
      #ar[i++]="$filename"
      ar[i++]="$(basename "$filename")"
   fi 
done < <(mdfind -0 -onlyin ${dir} "kMDItemKind == 'Folder' && kMDItemDisplayName == ${mdfind_search}"); 

echo ${ar[*]}; echo; echo ${ar[0]}; echo ${ar[1]}; echo

Regex-based file/folder search with Spotlight

To get more Spotlight search options use the commands mdimport -A and mdls file.txt in Terminal.app.


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

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

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


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