This article talks about ways to hide files from being indexed by Spotlight (mds) on macOS.

What is Spotlight?

How to interact with Spotlight from CLI?

  • /usr/bin/mdls: Lists the metadata attributes for the specified file.
    • mdls <filename>: List the metadata attributes for the specified file.
  • /usr/bin/mdfind: Search for files using metadata queries.
    • mdfind -name <filename>: Search for files with the given filename.
    • mdfind -onlyin <path_to_directory> <query>: Search for files with the given query in the specified directory.
    • mdfind "com_apple_backup_excludeItem = 'com.apple.backupd'": Search for files that are excluded from Time Machine backups.
  • /usr/bin/mdimport: Import file hierarchies into the metadata datastore.
  • /usr/bin/mdutil: Manage the metadata stores used by Spotlight, such as enabling/disabling indexing.
    • mdutil -s /: Show the indexing status of the startup volume.
    • mdutil -a -i on|off: Turn on/off indexing for all volumes.
    • mdutil -i on|off <path_to_volume>: Turn on/off the Spotlight indexing for a given volume.
    • mdutil -E <path_to_volume>: Erase the metadata stores and restart the indexing process.

Where is the config for Spotlight?

The configuration file for Spotlight is located at:

~/Library/Preferences/com.apple.Spotlight.plist

Use the command plutil to read the plist file.

plutil -p ~/Library/Preferences/com.apple.Spotlight.plist

img
Comparison of the System Preferences and the output of `plutil`. Note that the list is "ordered".

Another way is to use defaults.

defaults read com.apple.spotlight

Where is the index stored?

.Spotlight-V100

This directory .Spotlight-V100 is created in the root directory of every volume.

e.g.:

/System/Volumes/Data/.Spotlight-V100/

Hands-on

.metadata_never_index

The file .metadata_never_index stops Spotlight from indexing the directory and its contents. If this file is at the root of a volume, Spotlight will not index that volume.

.metadata_never_index_unless_rootfs

The file .metadata_never_index_unless_rootfs is almost the same as .metadata_never_index, but it will not stop Spotlight from indexing the volume if it is the root volume.

<DirectoryName>.noindex

Naming a directory with the .noindex suffix might work, but its effectiveness within mds processes is unclear and I personally could not find any evidence of this in mds. If you have any information, please let me know.