MacOS – Prevent Spotlight from indexing future hard drives

hard drivemacosspotlight

Using Mavericks, how can I prevent Spotlight from indexing all drives except my primary internal drive? In my work, I connect dozens of new drives every week, and I do not want the .Spotlight-V100 folder to be created on them.

I know I can add each drive to the Spotlight privacy list, but that can only be done after connecting the drive, and then the .Spotlight-V100 folder has already been created.

I could disable Spotlight completely, but then I lose the convenience of indexing my internal drive. I want to use Spotlight for my internal drive only, and disable it for all other drives, including ones that have yet to be connected.

Best Answer

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