15

Sometimes I use an external drive with macOS on it. This is in conjunction to my current internal drive which also is running macOS.

I find it annoying that, whenever I search for something (such as an application) when I'm in either OS when the external drive is connected, I get results from both my external and internal drive. However, I am only interested in running the application from the drive I've booted off of.

I tried circumventing this issue while on my external drive by creating a Spotlight exception for internal drive. This prevented files and apps from my internal drive from showing up. However, this also prevents me from searching for files on my internal drive, when I'm booted off my internal drive! Clearly this is something that I don't want.

It seems as though the Spotlight exclusions are per drive, which is not ideal in this scenario.

Is there a way to make Spotlight index only the drive which is currently booted? In this way when I'm booted off my external drive I'll get results only from the external drive, and same with the internal drive.

0

5 Answers 5

20

You could have a script that runs at startup that employs the technique suggested in this post https://apple.stackexchange.com/a/91759/183505

When booting from DriveA (when you want to disable spotlight indexing for External DriveB) you could execute :

touch /Volumes/DriveB/.metadata_never_index

When booting from external DriveB and you want to re-enable spotlight perhaps you could have your startup script execute:

rm /Volumes/DriveB/.metadata_never_index

The linked post also lists other ways to programatically alter the spotlight exclusions.

Here are some ways to add a script that will launch at login : https://stackoverflow.com/questions/6442364/running-script-upon-login-mac

Good luck!


Edit : Method using bash scripts and plist files


First create a startup script. I chose to create one at ~/script.sh

Make sure it's executable chmod +x ~/script.sh

Script for OS that wants to hide a drive from spotlight

#!/bin/bash
flagLocation="/Volumes/DriveToHide"
flagRemoved=".ney_the_index"  # a new name

# if flag exists rename it.
if [ -a "$flagLocation/.metadata_never_index" ]; then 
    mv "$flagLocation/.metadata_never_index" "$flagLocation/$flagRemoved";
fi

Script on the OS that wants to index the drive

#!/bin/bash
flagLocation="/Volumes/DriveToHide"
flagRemoved=".ney_the_index"

if [ -a "$flagLocation/$flagRemoved" ]; then
    mv "$flagLocation/$flagRemoved" "$flagLocation/.metadata_never_index"
fi

if [ ! -a "$flagLocation/$flagRemoved" ] || [ ! -a "$flagLocation/.metadata_never_index" ] ; then
    touch "$flagLocation/.metadata_never_index"
fi

Create a plist file ~/Library/LaunchAgents/com.user.loginscript.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
   <key>Label</key>
   <string>com.user.loginscript</string>
   <key>Program</key>
   <string>/Users/yourusername/script.sh</string>
   <key>RunAtLoad</key>
   <true/>
</dict>
</plist>

Test it by loading and unloading it:

launchctl load ~/Library/LaunchAgents/com.user.loginscript.plist
7
  • Seems like this is the best way to do it. I was hoping for something more "native" in a way, but this should be quite reliable. Thank you.
    – Oion Akif
    Feb 15, 2017 at 13:21
  • I want to mention a problem I'm facing trying to implement this: the rm command requires that I run it using sudo. I'm using the first method in the SO link you posted. Would you happen to have any suggestions for that? Do I need to use the launch daemon method to overcome this?
    – Oion Akif
    Feb 15, 2017 at 14:24
  • I updated my answer with a slightly hacky solution.
    – David Kerr
    Feb 17, 2017 at 2:10
  • 2
    If DriveToHide has spaces, then your scripts, as currently written, will fail. If you are going to test with [ ... ] then double quote the variables to prevent blobbing and word splitting. Or use [[ ... ]] to test, then double quoting isn't necessary. Feb 17, 2017 at 2:33
  • Nice! thanks user3439894. I updated the answer to reflect your suggestion.
    – David Kerr
    Feb 17, 2017 at 3:00
6

I learned today that you can use a sudo touch /.metadata_never_index_unless_rootfs in the root directory of each drive to separate Indexes from OS X Boot-Drives. It is a special version of .metadata-never-index, because it will (re)index the drive when you boot from it, but not when you don't.

2
5

I use mdutil command to prevent indexing on external drive, I feel that is more reliable.

To check if the external drive has indexing enabled or not, run the command:

$ mdutil -s /Volumes/<VolumeName>

Indexing enabled. 

To disable indexing, run the command (require user password):

$ sudo mdutil -i off /Volumes/<VolumeName>

Indexing disabled.

You can recheck the indexing status with the first command again:

$ mdutil -s /Volumes/<VolumeName>

Indexing disabled.
2
  • If you add -d to the command (sudo mdutil -i off -d /Volumes/<VolumnName>) it will also disable searching.
    – James L.
    Nov 14, 2021 at 6:50
  • Is this change permanent? Will it still be effective if I unplug and plug my external drive?
    – Bruce Sun
    Feb 9 at 8:55
2

Apologies for the new answer (not enough rep to comment as I'm new here)

@hapi - I may be confused, but are the scripts the wrong way round?

Script for OS that wants to hide a drive from spotlight: renames .metadata_never_index

Script on the OS that wants to index the drive: creates .metadata_never_index

I thought the presence of .metadata_never_index on the volume meant Spotlight ignored it?

Thanks

2
  • 1
    My experience is that .metadata_never_index has no impact whatsoever on macOS 10.13 - superuser.com/questions/591406/… ...
    – Anon
    Sep 6, 2018 at 13:11
  • You're right. I think the scripts do the opposite of what the descriptive line above it says. Nov 17, 2020 at 15:45
-3

Whereas the other answer is a good method to do it, it can inadvertently cause problems and may not be 100% reliable. A better method would be to use Alfred, as it is more flexible than Spotlight, on the external OS.

3
  • How would you use Alfred for this? Jan 2, 2018 at 19:34
  • Good question. I wish I remembered how. I believe I was talking about the way that you can exclude folders from the Alfred search without having to use exclusion method which was suggested in the other answer.
    – Oion Akif
    Jan 2, 2018 at 22:05
  • I'm assuming that I meant that I would search for files using Alfred rather than using Spotlight search. In retrospect this seems like a silly answer to my question.
    – Oion Akif
    Jun 4, 2020 at 0:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .