Reindex Spotlight from the Command Line in Mac OS X
Spotlight is generally very good at keeping a valid index of a drives contents up to date, but if you’ve recently restored a drive or had to delete the Spotlight index for one reason or another, you may need to reindex the drive manually. This is easy from the Spotlight control panel, and can also be achieved through the command line as we’ll demonstrate.
Reindexing Spotlight from the Command Line of Mac OS X
Reindexing Spotlight from the command line is done with the mdutil tool, first launch Terminal and then type:
sudo mdutil -E /
This will reindex every mounted volume on the Mac, including hard drives, disk images, external drives, etc. Specific drives can be chosen by pointing to them in /Volumes/, to only rebuild the primary Macintosh HD:
sudo mdutil -E /Volumes/Macintosh\ HD/
To reindex an external drive named “External” the command would be:
sudo mdutil -E /Volumes/External/
Individually Reindexing Selected Files in Mac OS X
In rare cases, Spotlight can miss a file during index, so rather than reindex an entire drive you can also manually add an individual file to the search index with the mdimport command:
The mdimport command can be used on directories as well, if a specific directory needs to be reindexed or was somehow left out of the spotlight search index.