Mac OS X,  Mac Security,  Mass Deployment

Reindex Spotlight from the Command Line

Spotlight is really a simple tool. Spotlight consists of mds, a command that is the metadata server, mdworker, the pawn that mds sends to scan objects and index them and then the three command lines of mdutil (manage the indexes), mdls (list metadata of an object) and mdfind, which as the name implies, finds things. All of this is used to keep a database called .store.db nested under .Spotlight-V100 at the root of each volume that’s been indexed.

Screen Shot 2013-11-14 at 9.39.09 PM

To reindex Spotlight from the command line, we’ll use mdutil. From a command prompt, enter the following to index your boot volume.

sudo mdutil -E /

Or an external drive named krypted:

sudo mdutil -E /Volumes/krypted

When indexing, you will see the mds and mdworker processes running. If the process does not seem to be completing timely, you can use lsof to see where it is in the process. If the indexing errors then try and manually reindex based on the file that you see from where it crapped out:

mdimport /Volumes/krypted/designingwomencollection/episode0101.mov

Or to mdimport the directory that might be problematic:

mdimport /Volumes/krypted/designingwomencollection

Finally, the attributes that are tracked by Spotlight are many. The metadata attributes themselves are typically in xattr. For example, Command Line Finder Tags (https://krypted.com//mac-os-x/command-line-finder-tags/).  To see the raw metadata for a file, use xattr:

xattr -l /Volumes/krypted/designingwomencollection/episodelist.pdf

The output of which would include the following:

com.apple.FinderInfo:
00000000  00 00 00 00 00 00 00 00 00 1C 00 00 00 00 00 00  |................|
00000010  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  |................|
00000020
com.apple.metadata:_kMDItemUserTags:
00000000  62 70 6C 69 73 74 30 30 A1 01 55 52 65 64 0A 36  |bplist00..URed.6|
00000010  08 0A 00 00 00 00 00 00 01 01 00 00 00 00 00 00  |................|
00000020  00 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00  |................|
00000030  00 10                                            |..|
00000032
com.apple.quarantine: 0041;520d0314;Safari;

The more metadata (QuickTime and some other apps can put a lot of metadata in files btw) the more you’d see. You can assign metadata manually or even exclude directories from indexing if you see the system is stuck on indexing a given directory.