Terminal 101: Rebuild the Spotlight Index On the Fly

Every Monday, we'll show you how to do something new and simple with Apple's built-in command line application.

Every Monday, we'll show you how to do something new and simple with Apple's built-in command line application. You don't need any fancy software, or a knowledge of coding to do any of these. All you need is a keyboard to type 'em out!

Sometimes Spotlight doesn't behave properly, or perhaps doesn't know about files or folders that have just been created. Sometimes, it just forgets about files on external drives. When this happens, you can invoke a hidden feature (shown only through the Terminal commands) to cause your Spotlight Indexes to be rebuilt on either your Mac or any external drives connected to it. Let's walk through the process of how to do this.

Rebuild Spotlight on your Mac

If you're having issues finding files on your Mac, then you can easily rebuild the Spotlight index for your Mac by typing the following command into the Terminal app (located in /Applications/Utilities):

sudo mdutil -E /

After pressing enter, you'll be required to enter your password (because this command runs as a super user). After you do that, you will notice after a few minutes that when clicking the Spotlight button in the menu bar, Spotlight will show that it is currently indexing your system.

When Spotlight is indexing, it is building an extensive file that lets it quickly and easily search your system for just the item that you are looking for. This is why rebuilding the Spotlight index can fix a lot of issues stemming from wrong files showing in searches.

Rebuild Spotlight on any drive

Spotlight can build an index on each drive that is connected to the Mac for better searching on external drives. Fortunately, you can rebuild the Spotlight indexes on these drives as well. To do this, the the following command into the Terminal utility:

sudo mdutil -E /Volumes/[DriveName]

In the above command, you can either manually enter the drive name, or type in just the following portion of the command:

sudo mdutil -E

Then drag and drop a drive from the Finder on your Mac into the Terminal window. This will automatically enter the drive path and name for you. After pressing enter, you will once again be required to type in your account password. Again, Spotlight will begin building the index for the external drive within a few minutes of issuing this command.

Cory Bohon is a freelance technology writer, indie Mac and iOS developer, and amateur photographer. Follow this article's author on Twitter.