Prevent Spotlight from indexing future hard drives

A file called

.metadata_never_index

place in the root of any volume will suppress indexing behaviour.

You can generate this file automatically

the following files need to be created as root

the following shell script will create the requisite file should an active (less than 30 days old) spotlight entry not be found.

/var/root/unindex.sh

to create this

sudo vi /var/root/unindex.sh

or use your preferred text editor

The contents should be

#!/bin/bash
# suppress spotlight indexing of new volumes
# pc 26-june-2014 v0.1
# v 0.2 27-june-2014 add mdutil

# get the last volume mounted

vol=$(df)
vol=${vol##*%}
echo $vol




# is this a volume that has been indexed in the past month

count=$(find ${vol}/.Spotlight-V100 -mtime -30 | wc -l )
[[ $count -gt 1 ]] && {

# then it remains so

logger -t unindex spotlight activity detected in last 30 days
exit 0
}

# else create index supression file
logger -t unindex creating ${vol}/.metadata_never_index
touch ${vol}/.metadata_never_index
# and stop spotlight indexing the drive
logger -t unindex mdutil -i off /Volumes/${vol}
mdutil -i off /Volumes/${vol}

make sure it can be executed...

sudo chmod a+x /var/root/unindex.sh

now add an entry to launchd, create the file

/Library/LaunchDaemons/org.misctools.unindex.plist

to create this

sudo vi /Library/LaunchDaemons/org.misctools.unindex.plist

with contents

<?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>org.misctools.unindex</string>
    <key>ProgramArguments</key>
    <array>
        <string>/var/root/unindex.sh</string>
    </array>
    <key>RunAtLoad</key>
    <false/>
    <key>StartOnMount</key>
    <true/>
</dict>
</plist>

now activate the job

sudo launchctl load /Library/LaunchDaemons/org.misctools.unindex.plist

the job will launch every time you mount a disk, look in the console for messages filtered by 'unindex' for it's actions e.g. from my machine

 26/06/2014 09:02:35.824 unindex[1362]: creating /Volumes/hfs/.metadata_never_index

Use the command line tool mdutil.

For example, say you connect an HFS+ journaled volume named "LEAVE_ME_BE"

Use the following command to disable indexing of the volume. If ownership is enabled, running as sudo may be necessary:

sudo mdutil -i off /Volumes/LEAVE_ME_BE

Check indexing status of all connected volumes:

mdutil -s /Volumes/*

Read the man page linked above for further options.