The Adventures of Systems Boy!

Confessions of a Mac SysAdmin...

Getting Back to (Search) Basics

NOTE: A new and improved version of this script has been posted. Please use the new version. It's way better.

First, a few brief DISCLAIMERS:
1. The information and script provided in this post will disable Spotlight indexing.
2. It will also (optionally) erase your Spotlight index.
3. This means that if you want to re-enable Spotlight indexing, you will have to wait for the index to rebuild, which can take a long time.
4. This method only disables Spotlight indexing for firewire drives that are present when the script is run. Once a firewire drive has had Spotlight disabled, it will be disabled forevermore on all systems, as the indexing status of any given volume is stored on the volume itself.
5. Finally, it has come to my attention that using this script to disable Spotlight indexing will also disable searching by "Entire Contents" in Apple's Mail app. (Searching by "From," "To," and "Subject" all work fine.) This is a major drawback for some, and less so for others, but it's something you should be aware of before you proceed to disable Spotlight indexing on your system. If you can't live without Mail searches by content, do not use the script at the end of this post.

These are the only issues I am aware of regarding this script. If you discover others, please post them in the comments section.

And away we go!

I really miss the old Panther-style, find-by-name searches. (And apparently so do a lot of people!) I use them much more than the Tiger method of searching by metadata and file content, which just returns way too many results for my tastes, generally speaking. (I mean, if you don't know the name of your file, how do you know you've found it anyway, right?)

Ideally, Apple would have (or still will, I hope) given us some options here. I think it would be best if searches from the toolbar functioned as they did in Panther (i.e. quick 'n' dirty seach-by-name-in-current-folder) or there were a preference for how toolbar searches work. Spotlight searches should function differently, based on the metadata concept. That's basically how I think it should work: two search methods; two types of results. And bring back the old Finder toolbar search. I love(d) that thing!

That said, I've spent some time researching the matter, looking for a way to get back search-by-name functionality, since, if I have to choose between one way or the other (and right now it looks like I do), I choose to search by file name. I found some tasty info on Apple's Tiger discussion forums which I will briefly outline:

1. Moving all volumes to the "Privacy" tab of Spotlight's preferences will disable Spotlight. However, you will then lose the ability to find files at all -- by name or otherwise -- in the Finder. Also, if you mount an external drive, Spotlight will try to index it until you then add it to the "Privacy" tab. Not the best solution.

2. Spotlight can be completely disabled by simply adding the line:
to your /etc/hostconfig file and rebooting. This method is very thorough, it seems. Drives later added to the system will never be indexed. Spotlight won't even try. Unfortunately, this method again has the side effect of rendering all disks unsearchable from the Finder. Not what I'm looking for.

3. Enter mdutil. (And, BTW, I want to credit Ondrej Zacek from the Apple Discussions. Though the info is freely available, it was his "Finder will fall back to old behavior when searching..." comment that gave me hope. Thanks, dude, whoever you are.) Using mdutil to disable Spotlight indexing on a given volume results in Finder and Spotlight behavior I can live with. Essentially, that is, after you've disabled indexing with mdutil, you can search files in the Finder or with Spotlight, and the results returned will only be ones that include your search term in the file name. And in Spotlight searches, you still get the extra added benefit of having things sorted by type (somehow Spotlight can still identify and sort files by type, even if the search is only by name).

Now that's pretty durned cool. I have the ability to search by name (only) again, and I still get some of the usefulness of Spotlight. (And, P.S., Finder toolbar searches will also be sorted by kind. A simple window style change to list view -- or command-2 -- will get you the flat list you're used to.)

Anyway, here is the one command that will disable Spotlight on ALL your volumes:
sudo mdutil -i off / /Volumes/*

Additionally, you might want to remove the existing databases from all volumes as well, for good measure:
sudo mdutil -E / /Volumes/*

NOTE: Before you run either of these, be aware that if you want to reverse this process you can, but you will have to wait for Spotlight to reindex all your volumes again, and this can take a looong time. Also, if you add an external firewire drive, Spotlight will try to index it. Running these commands will stop it, however.

And, so, to reverse the process:
sudo mdutil -i on / /Volumes/*

A few seconds after running this, Spotlight will begin reindexing.

Finally, some scripting goodness. I wrote a simple shell script that automates all of this. It has options for both enabling and disabling Spotlight metadata searching. Feel free to use it, or modify it, to your heart's content. Ideally there would be a way to run the mdutil commands whenever a firewire drive is mounted, but I haven't tried that yet. In any case, this works really well for me at this point. Hopefully someone else will find it useful as well.

To use this script, open TextEdit (in the standard Rich Text mode, for now) copy the text below "Begin Shell Script" (starting with #!/bin/bash) into a new text file, and then convert this file to plain text (Format->Make Plain Text). Name it, save it, chmod it to 755 (open Terminal, type chmod 755 and then drag the file to the Terminal window and hit return). Finally, drag the file again into the Terminal window, hit return and follow the instructions. Also, if you want to make it double-clickably executable, give the name a .command suffix. Yum!

Hopefully Apple will give us some flexibility in how we search our files in the not-too-distant future. Until then, I'll be using my trusty script and old-world search methods in my shiny new Tiger Finder.


Begin Shell Script


echo "
############# DISABLE OR ENABLE SPOTLIGHT #############"

echo "
This script will enable or disable Spotlight searching
and (optionally) indexing on ALL LOCAL VOLUMES.
Panther-style finds-by-name will still function after
disabling Spotlight using this script.

echo "
Choose your poison...

[1] Disable Spotlight
[2] Enable Spotlight
[Ctrl-c] to Quit at any time
read poison

if [ $poison = 1 ]

echo "


echo "
Disabling Spotlight for all volumes...
sudo mdutil -i off / /Volumes/*
echo "
Would you also like to erase the Spotlight index (Y/N)?"
read index
if [ $index = y -o $index = Y ]
sudo mdutil -E / /Volumes/*
echo "


Spotlight search is no longer in effect.
You may now search by file name only.

Have fun with your Tiger, Tiger.


exit 0

if [ $poison = 2 ]
echo "


echo "
Enabling Spotlight for all volumes...
sudo mdutil -i on / /Volumes/*
echo "


Spotlight has been enabled on all volumes.
Indexing should begin momentarily.
It may take quite some time to complete.

Have fun with your Tiger, Tiger.



echo "
You did not choose from the available options.
Please rerun the script to try again.

exit 0

End Shell Script

Labels: ,

« Home | Next »

8:12 PM

thanks alot for this. i am giving it a try. fingers crossed!


p.s. however, pasting the shell into a plaint text file, saving and chmodding it doesnt work (cannot open binary file...). what could be the problem? the encoding? i will try to enter the commands directly into the terminal...    

10:10 PM

Don't know what the problem is. I am using TextEdit with the encoding set to "Automatic." Here's what I do that works for me:
1. Copy the text.
2. Open TextEdit (which by default opens in Rich Text format).
3. Change to Plain Text format (under the "Format" menu choose "Make Plain Text") if not already in Plain Text format.
4. Paste the text into the Plain Text doc.
5. Save it.
6. Chmod 755 on the document (in Terminal, type chmod 755 and a space, then drag the document to Terminal and hit Return).
7. Run script by dragging to the Terminal window. (You may have to type the letters sh and a space before dragging in the script.)    

4:51 AM

no go. i did it exactly the way u did but still the same error message. however, everything worked perfectly via terminal and finally i got rid of spotlight while still having the search for file name feature - sheesh! just one question: this is permanent i suppose? (i dont have to reenter it after every reboot?!) anyway now let's see how to get rid of the dashboard... :]

thanks again!    

7:09 PM

Yes. This survives a reboot.

Glad it worked out for you.    

3:02 AM

I pasted the full script into a vi session and saved and then used "chmod 775" and then added ".command" to the filename. You double click the script and it seems to work perfectly.    

11:18 AM

Mail 2.0, though, does not seem to fall back to Mail 1's search engine.

So with "sudo mdutil -i off / /Volumes/*" you miss the ability to search ANYTHING on Mail 2.    

2:09 PM

Re: Mail Searches...
Hmmm... I see what you mean. Actually, I am able to search by everything but "Entire Contents." Meaning, I can search by "To," "From," and "Subject," but when I click on "Entire Contents" I get nothing.


I will see if I can come up with a solution and if so post it here.

Thanks for the heads up.


2:54 PM

FYI, you can do a name/keyword only search by putting your search string in double-quotes (actually only the front double-quote is needed). This isn't exactly name-only, but it's pretty close.    

6:22 PM

Well, the Mail problem is unsolvable as far as I can see, or at least not solvable to my satisfaction. It might be possible, but getting Mail's search-by-content to work would probably require re-indexing my whole drive, and then excluding all folders except Mail somehow. I'm not sure that would even work.

The heart of the problem is that the command used in the script -- mdutil -- disables Spotlight on a drive-by-drive (or partition-by-partition) basis. If your mail is on a disabled partition (which, if you used my script, it most certainly is), it will not get indexed, and searching mail by content evidently requires an active Spotlight index on the volume.

The only other workaround I could see would be to put all your mail (i.e. the contents of ~/Library/Mail) onto another volume -- either another partition, drive, or disk image -- and then symlink it to the appropriate area of your home account. If you left Spotlight indexing on for this Mail-only volume, in theory it would work. I may try it, just for kicks, but I'm not going to even attempt to script something like that. The script I've provided is really only meant to be a temporary workaround until Spotlight is working better.

(And by better I mean, one day it would nice to have the option to specify exactly what does and doesn't get indexed, among other things. Right now this is difficult and error prone and only allows specifying what doesn't get indexed, not what does. I want both.)

In any case, sure is a bummer about the Mail index. I'll put a warning on the post page.


12:46 AM


The following will find files named README (or whatever, you get the wildcard).

Also check out the "mdls" command which will show all the other meta data within a given file.

From the CLI:
mdfind "kMDItemDisplayName == *EAD*"

From Spotlight:
kMDItemDisplayName == *EAD*

Thanks for taking the time to document and post your script!


1:22 PM

Been chewing on using locate and Sebastian Krauß's Locator. Then I found that our lovely friends at Apple hadn't bothered to implement a proper updatedb, but used a pithy C-shell script. I fixed it up good enough for my own machine.    

7:04 PM

I would add the phrases: "This applies to ALL MOUNTED VOLUMES!" and "Control-C cancels the operation."

I have modified a copy of the script to just apply to my external FireWire connected HDs, since I have a unique prefix for those volume names; ie: "/Volumes/WD160*"

Seems to work during command execution.
... checking on a test "name" search on the external HD ... ... Yes, file name found!

FINALLY I can search on my external HD without having to have the Gigabytes indexed.

P.S., the chmod I applied to first script seems to be carried over to the copy I made for my external HD, even after I modified it.

THank you. B.Y.    

7:15 PM

Follow up on the disable, re-enable on my main HD.
It re-enabled indexing for two of my partition/volumes, which are bootable & journaled. But did not re-enable for the two non-journaled data-only partition/volumes.

So perhaps I have to enable journaling on the data partition/volumes to get the indexing back?

or perhaps I should use the mdutil command directly on them?    

7:17 PM

Oops, I mean, it only re-enabled on my boot volume. the other three returned a message: "Could not set indexing status for volume."
I verified this with a new test text file containing a unique nonsense word, which was found on my boot volume, but not on the other vols.    

7:34 PM

Confirming: even though my 3 other partition volumes did not get indexing re-enabled with the script (while boot vol did), using the manual mdutil command did get the indexing find-by-content re-enabled.

Thus: "sudo mdutil -i on /Volumes/D-Data" does get indexed find-by-content activated.
(again, tested by creation of small text text file with unique nonsense word)

The only difference with your command is mine does not have an extra "/" in it...
Tested with the extra "/" removed, and this re-enabled indexing on three vols (which is an improvement on first results), but got the "Could not set indexing..." on the fourth volume. Something strange here. Oh well, at least the command line mdutil -i on gets it all back.    

12:48 PM


It's strange that the script is not behaving correctly for you. The script, as you know, simply runs mdutil -- the same command you ran -- on all drives and partitions listed in /Volumes. The extra "/" is just a precautionary measure to make sure the command runs on the root (system) partition as the system partition was not linked in older versions of OSX the way it is Tiger. It's probably not necessary, but by the same token, should not cause any problems. Definitely somthing strange going on.

I agree, though, that the script could be a bit more user-friendly. I was even planning on posting one that disables Spotlight on a per-drive basis, but I just haven't had time. It's pretty easy, though, as you discovered. To enable, just type:
sudo mdutil -i on /Drag/Your/Drive

To disable:
sudo mdutil -i off /Drag/Your/Drive

Hopefully, by the time I get around to posting a smarter script, Apple will have fixed Spotlight and made my script unnecessary. If not, perhaps I'll write a new, better disable/enable Spotlight script.


2:43 PM

What I like best is that the script calls me Tiger.

Spotlight has made my PowerBook (G4 1.5GHz, 80-GB 5400-RPM HD) miserable since installing 10.4.0 so it's been entirely off, and I'm elated to get filename-based finding working again. Thanks also for the imaginative tip with the disk image for Mail. I'll definitely try that.    

5:29 PM

Someone a long time ago wrote:

"p.s. however, pasting the shell into a plaint text file, saving and chmodding it doesnt work (cannot open binary file...). what could be the problem? the encoding? i will try to enter the commands directly into the terminal..."

I've discovered the source, and the cure for this problem. The problem is the browser. Copying the script from Firefox (which I now use) will put non-UNIX line breaks throughout the text file. The solution is this:
1) Copy the script text.
2) Paste it into a Rich Text document in TextEdit.
3) Then hit "Format->Make Plain Text" (command-shift-t).
4) Then save the file out and set the permissions.

Converting the text this way (in TextEdit) converts the line breaks to something usable by the shell.

Mystery solved. (Whew!)


10:39 AM

I was about to try this when half way down the page I saw this tidbit....

FYI, you can do a name/keyword only search by putting your search string in double-quotes (actually only the front double-quote is needed). This isn't exactly name-only, but it's pretty close.

... That solves the whole problem soooooo easily!!! You need to put that at the top of the page! I can't believe I've been suffering with spotlight for so long when all I needed was a little quote mark.    

1:47 PM

Yeah, that's great. Except that it doesn't work. It is by no means a name-only search. It is (as the poster said) name/keyword. Try it. You'll get LOTS of results that don't contain your term in the name. If this works for you, great. It doesn't for me.    

6:15 AM

Many comments and URGES to make this needed small addition to spotlight have to be made to APPLE.
This can be done via Safari Menu where it says "Report an error to Apple"; It is an error! This quote hint is of some use, my Provider is called 1&1, and if I enter that in Spotlight it finds over 10500 items. If I enter "1&1, I get 12 items, which is a lot better. I have helped myself with Utility FileBuddy, it searches fast, about 3 seconds for 80 GB on a 800 MHz-Mac (wheras some FindFile Programs take 18 Seconds). So mention it to Apple!!    

» Post a Comment