Reindex Spotlight from the Command Line

Feb 2, 2012 - 8 Comments

Spotlight 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/

Use of the mdutil command will spin up mds and mdworker processes as Spotlight goes to work.

Individually Reindexing Selected Files

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:

mdimport /path/to/file

The mdimport command can be used on directories as well.

Enjoy this tip? Subscribe to the OSXDaily newsletter to get more of our great Apple tips, tricks, and important news delivered to your inbox! Enter your email address below:

Related articles:

Posted by: Paul Horowitz in Command Line, Mac OS X, Tips & Tricks

8 Comments

» Comments RSS Feed

  1. 911 says:

    Serious question. I see these posts on here quite often, do this or that from the command line.. Hmm. Do people actually do this?

    • Teddster says:

      Yes of course. It’s often quicker and more specific for advanced users, but the CLI gets a lot of use for sysadmins and others too. SSH in, rebuild clients Spotlight, etc. Remote administration. etc

  2. I still use Quicksilver, its still faster, and finds more.
    also it can find 2 names pr file.
    say you use Handbrake – but ½ of the time cant rember that strange name and rename it to “video converter and DVD ripper” then Quicksilver will find it on both the new and the old name.

  3. Brad says:

    Alright so tried out this fix.

    Returned:
    No index.

    From there goes back to “home” command line.
    Any ideas?

  4. Daniel says:

    When I run this for my external drive it says:

    Indexing disabled.

    How can I enable indexing on an external drive? Thanks.

    • Fake says:

      to enable indexing of a drive that you aren’t currently indexing:

      mdutil -i on “/Volumes/ExternalDrive”

      If you don’t know the correct name of the external drive, do this and look at the output:

      cd /Volumes
      ls

      (that’s L S in lowercase)

      If you want to see the indexing status of a particular drive, do this:

      mdutil -s “/Volumes/ExternalDrive”

      To ERASE THE SPOTLIGHT INDEX (NOT THE DRIVE ITSELF) and re-index a particular drive, do this:

      mdutil -E “/Volumes/ExternalDrive”

      To stop indexing an external drive, do this:

      mdutil -i off “/Volumes/ExternalDrive”

      Note that you must be an administrator on your computer. If you are not, precede each command with the word “sudo” which temporarily grants you administrative rights. You’ll need to type in the administrator’s password (which won’t display on the screen; but the administrator can read the log to know you’ve used his/her password, so be sure you have permission).

  5. Nacho says:

    it ask me for the password!?!? i own this mac but never been ask for one?!?!

    any ideas!?

    • Frederic says:

      The account you are using is an administrative account but not the root account

      An easy way to enable the root account

      sudo passwd

      sudo will ask for a password. This is the password for the administrative account you’re currently using. Administrative accounts have super user rights so you can start programs that are otherwise restricted to the root account.

      With passwd you can set a password for the root user.

Leave a Reply

 

Shop for Apple & Mac Deals on Amazon.com

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates