Skip to content

Spotlight

Spotlight is the Apple metadata database.

Indexing

You can edit the file /.Spotlight-V100/_rules.plist to add or deny indexing of specific folders. Use mdutil to edit per-disk indexing behavior. More info at MacOSXHints.com: configure spotlight to index excluded directories

Spotlight Search Bar Examples

  • name:nikon kind:pdf
  • kind:image modified:>3/25/2011 # does not support iso-8601 format 😞

mdls

"mdls -- lists the metadata attributes for the specified file" - man mdls

Show GPS date for all JPG files

mdls -name kMDItemGPSDateStamp *.jpg

Show name and version of an app

mdls -name kMDItemVersion -name kMDItemDisplayName /Applications/Alacritty.app

mdutil

"mdutil -- manage the metadata stores used by Spotlight" - man mdutil

Disable indexing on a volume

sudo mdutil -i off /Volumes/volume_name

Delete local indexes

This flag will cause each local store for the volumes indicated to be erased. The stores will be rebuilt if appropriate.

sudo mdutil -E /

mdimport

A tool to manage the way things are imported.

Show the schema

mdimport -X

mdfind

The terminal search tool for spotlight.

"mdfind -- finds files matching a given query" - man mdfind

Find all landscape oriented photos

mdfind -onlyin "$PWD" "kMDItemOrientation = 0"

Find all portrait oriented photos

mdfind -onlyin "$PWD" "kMDItemOrientation = 1"

Spotlight search by filename function for bash

function sl { mdfind "kMDItemFSName == '$*'wc" ; }

Find music files modified since yesterday

You must use single quotes for the spotlitght $time variable so bash does not attempt to interpolate it as a bash variable. You could also use double quotes and escape it.

mdfind -onlyin "/Volumes/Peter/Music/" 'kMDItemContentModificationDate >= $time.yesterday'

mdgrep

#!/bin/bash
#
# Spotlight metadata find and grep by Daniel.Hoherd at gmail dot com

## Check for at least two arguments, print usage if else
if [ $# -lt 2 ] ; then
    echo "usage: $0 searchstring [dir or file] [dir2 or file2]"
    exit ;
fi

ss=$1;
shift;
files=$@;

until [ -z "$1" ] ; do
    thisitem=$1
    onlyin="-onlyin '$thisitem' $onlyin"
    shift
done;
eval mdfind -0 $onlyin "$ss" | xargs -0 grep -Hi "$ss"

xattr

xattr can be used to set arbitrary spotlight metadata:

dho@tro:~/temp$ touch foo
dho@tro:~/temp$ xattr -w com.apple.metadata:kMDItemStarRating 10 foo
dho@tro:~/temp$ mdls -name kMDItemStarRating foo
kMDItemStarRating = "10"

See Also