Terminal commands for improving/fixing Spotlight & Indexing volume

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.

Add a file to the Spotlight index

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.

Add a folder to the Spotlight index

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. In 10.7.5, “The -f option has been obsoleted and does nothing.

Search Spotlight from the Terminal

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

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.

sudo mdutil -i off /
sudo rm -rf /.Spotlight*
sudo mdutil -i on /
sudo mdutil -E /

1 – turn indexing off

2 – delete Spotlight folder

3 – turn indexing on

4 – rebuild

Does this help?

Very Useful

Also you could use these commands:

sudo mdutil -a -i off 
sudo mdutil -a -i on 
Flushing your cache and rebuilt it with the following command: sudo mdutil -pEsav 

I had the exact same problem, with all solutions above failing identically. Then I reexamined the directory listing for / and found a hidden file .metadata_never_index in the root directory. I removed this file and was able to turn spotlight on again with mdutil -i on /.

mds is now reindexing the hard disk, all looks good for now.

In terminal, I ran “mdutil -sa” that shows whats indexed

Also I have seen most of the above command-lines would produce 'Indexing and searching disabled'sudo mdutil -pEsav gave me 'datastore publishing not implemented.'

Below is the solution that worked:

  • edit /etc/hostconfig
  • add the line 
SPOTLIGHT=-YES- at the bottom (this line was missing)
  • reboot

Categorised as: Linux/OSX, OS X

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.