Prevent Spotlight from indexing external hard drives

May 03, '10 07:30:00AM

Contributed by: Anonymous

If you, like me, get annoyed by the index files Spotlight creates any time you create an external hard drive, you might want to try this hint. We're going to set up a launchd service which runs a script every time a hard drive is mounted. This script will touch the .metadata_never_index file (which prevents Spotlight from creating the index; see this hint) on the hard drive.

Let's start with the launchd service. Open Terminal and type the following, then press Return:

$ cd ~/Library/LaunchAgents/ 
Create the file com.index.no_index.plist with your favorite editor (vi, nano, etc.), and insert the following XML code:
 
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>Label</key>
	<string>com.index.no_index</string>
	<key>Disabled</key>
	<false/>
	<key>Program</key>
	<string>/Users/*YOUR_NAME*/no_index</string>
	<key>StartOnMount</key>
	<true/>
	<key>RunAtLoad</key>
	<false/>
	<key>KeepAlive</key>
	<false/>
	<key>EnableTransactions</key>
	<true/>
</dict>
</plist>
Do not forget to change *YOUR_NAME* to the name of you home folder. If you don't know what to put there type echo $HOME in Terminal.

Now we create the actual script. Go to you home folder by typing cd ~ , and create a file named no_index with your favorite text editor, with these contents:

#!/bin/bash

for filename in /Volumes/*
do
  if [[ $filename != "/Volumes/Macintosh HD" && $filename != "/Volumes/BOOTCAMP" ]]
then
  file=$filename"/.metadata_never_index"
  touch "$file"
fi
done;
You may want to make the file executable with chmod +x no_index . Now everything is set up, and we can start the service with this command: launchctl load -w ~/Library/LaunchAgents/com.index.no_index.plist

I hope everything will work for you; for me, it works very well in Snow Leopard.

[robg adds: I haven't tested this one.]

Comments (6)


Mac OS X Hints
http://hints.macworld.com/article.php?story=2010050112180751