Terminal commands for improving Spotlight
Spotlight works great most of the time, but occasionally you may need to do a bit of tinkering to get it to work properly. Most of us have probably had a problem where Spotlight won’t find a file you know is there. Here are a few Terminal commands for changing hidden Spotlight settings, performing more complicated searches and updating the index.
Add a file to the Spotlight index
In theory, all files are added to the Spotlight index when they are created. However, every now and again something goes wrong and a stubborn file might refuse to show up. To manually add it to the index, you can use the following command. Start by opening up Terminal (located in Applications/Utilities). Type in mdimport
and then hit the space bar. Next, find the file you want to add in the Finder, and drag it onto the Terminal window. Terminal should automatically type in the path to the file for you. Of course, if you know the path you can type it in manually yourself. Finally, hit return and the file should now show up in you Spotlight searches.
Add a folder to the Spotlight index
Adding a folder works in exactly the same way as with a file. However, in Mac OS X 10.4 Tiger and earlier, the command is slightly different if you want the contents of the folder to be indexed too. In that case use mdimport -f
instead. You might want to use this to add the System folder with mdimport -f /System
as it isn't searchable by default.
Search Spotlight from the Terminal
Considering Spotlight is so quick and easy to access, you will probably only use this if you are already working in the Terminal and want to find a file, or if you want some extra options. To do a search, just type mdfind query
where query
is what you want to search for. To limit this to a specific folder, instead use mdfind -onlyin ~/Desktop query
replacing ~/Desktop
with the path to your folder. To run a “live” search which will update the results as users add files, use mdfind -live query
. Control-C will stop it updating. To see all the other options, look at the man page by typing man mdfind
into Terminal.
Re-index an entire drive
If you are having problems with your Spotlight index, you might want to start over and re-index your hard drive. In the Terminal, type sudo mdutil -E /
and hit return. You may have to enter your password. The existing Spotlight index will be deleted, and Spotlight will start creating a new index in the background. You can re-index other hard drives by changing the command to include their name, like sudo mdutil -E /Volumes/OtherHardDrive
.
Re-index a specific file format
When Spotlight adds a file to the index, it checks to see if it recognises the file type and then uses an mdimporter plugin to index the contents if it does. Each file format has its own plugin, and most are located in Macintosh HD/System/Library/Spotlight or Macintosh HD/Library/Spotlight. Double clicking on a plugin will give you the option to re-index all the files associated with it. Alternatively, you can do this from the Terminal with the command mdimport -r
. For example, mdimport -r /System/Library/Spotlight/Chat.mdimporter
would cause all of your chat files to be re-indexed.
Add a new file format to the Spotlight index
One of the great things about Spotlight is that it will index the contents of many types of files. For example you can search for a word written in a PDF document or the telephone number of one of your contacts. Many third party applications require plugins to make Spotlight index their file's contents, which are available on the Apple Spotlight plugins page.
Make Spotlight index source code
Apple's developer tools come with a source code mdimporter plugin that allows Spotlight to search the contents of your source code. However, if don't want to install the developer tools (it's a big download) then you can just modify the rich text mdimporter plugin to handle the same job. Go to Macintosh HD/System/Library/Spotlight and find the RichText.mdimporter file. Right-click on it and choose "Show Package Contents". Inside the Contents folder, double-click on the info.plist file to open it in TextEdit. Near the top you will see a list that looks like this:
<string>public.rtf</string>
<string>public.html</string>
<string>public.xml</string>
<string>public.plain-text</string>
<string>com.apple.traditional-mac-plain-text</string>
<string>com.apple.rtfd</string>
<string>com.apple.webarchive</string>
<string>org.oasis-open.opendocument.text</string>
<string>org.openxmlformats.wordprocessingml.document</string>
You can add any of the following lines to the end of this list to enable Spotlight to index the contents of the specified format:
<string>public.c-header</string>
<string>public.c-plus-plus-header</string>
<string>public.c-source</string>
<string>public.objective-c-source</string>
<string>public.c-plus-plus-source</string>
<string>public.objective-c-plus-plus-source</string>
<string>com.sun.java-source</string>
<string>public.perl-script</string>
<string>public.python-script</string>
<string>public.csh-script</string>
<string>public.shell-script</string>
<string>public.ruby-script</string>
<string>public.php-script</string>
<string>com.netscape.javascript-source</string>
When you are done, you will want to double-click on RichText.mdimporter to re-index all these file formats.
If you have another file format that is a really just a text file under the hood, you can find out what to add to the info.plist file by running mdimport -n -d1 somefile.ext
in the Terminal. The output should contain a "type" that looks something like the items in the list above.
Remove something from the Spotlight index
This is really straightforward, but I thought I'd include it for completeness. Simply go to System Preferences and click on Spotlight. In the Privacy tab, click the plus (+) and choose your file or just drag it from the Finder.