The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,860 other subscribers

When Spotlight fails to find some files: mdutil revisited

Posted by jpluimers on 2025/11/07

On my M1 MacBook, somehow Spotlight started to fail returning some indexed files (especially recently added ones would not show up or it would take a very long while for some of them to show up).

So I revisited mdutil and related commands while writing a comment on the YouTube movie [Wayback/Archive] How To Rebuild the Spotlight Index on Your Mac – YouTube which since then vanished, but luckily I saved it and here I amended it with formatting:

My go to quick resource for terminal commands is ss64 is has cmd, PowerShell and *n*x derived ones all in one place.

For the Spotlight tool mdutil, it has this page: https://ss64.com/mac/mdutil.html

First step, check status (-s) of all (-a) indexed volumes which combines into -sa to see which volumes have indexes turned on.

mdutil -sa

Then either turn off (-i off) then on (-i on) volumes that don’t return spotlight search results:

sudo mdutil -i off /Volumes/SanDisk400GB/

sudo mdutil -i on /Volumes/SanDisk400GB/

If that fails, erase (-E) the index of that volume:

sudo mdutil -E /Volumes/SanDisk400GB/

If that fails, erase (-E) all (-a) indices (indexes?) which combined forms -Ea

sudo mdutil -Ea

Regrettably, mdutil lacks a progress of the indexing process, so you have to resort to ⌘-space then search for a term to see that or revert to counting mdworker processes:

ps -ceo command | grep -c mdworker

In the meantime, there are more mdutil options than the above page shows. In practice I have never used them, but this command shows them:

man mdutil

A less repository of macOS man pages is at https://github.com/jpluimers/keith.xcode-man-pages with instructions how to export yourself.

Strange things can happen: in 2011 (post published in 2012) had one occasion where mysteriously all indexes had turned off.

https://wiert.wordpress.com/?p=6957

Last login: Mon Oct 31 19:31:01 on ttys000
macminiserver01:~ jeroenp$ mdutil -s /
/:
No index.
macminiserver01:~ jeroenp$ sudo mdutil -i on /
Password:
/:
Indexing enabled.
macminiserver01:~ jeroenp$

For volume names, I now used /Volumes/Macintosh HD and /Volumes/JeroensM1MBP1TB, but likely with newer disk/volume partitioning layout, I should have included /Volumes/Macintosh HD - Data as well. The first two solved my problem though, so I am happy.

The (for me not so big) drawback of using the command-line is that it requires sudo,  so what you can also try in the MacOS Spotlight GUI is to exclude the affected volumes from Spotlight, then re-enable them. The place where you do this has changed over the various macOS versions (here mdutil is clearly at an advantage: rock solid user interface), so I include the most recent one:

  1. Click the  in the top-left of the screen
  2. Choose “System Settings…”
  3. On the left, browse down to “Siri & Spotlight” (this is in the third group of icons, in that group the second one from the bottom): (yup, macOS has tiny icons)
  4. In the “Siri & Spotlight” pane on the right, browse to the bottom
  5. Click the “Spotlight Privacy…” button
  6. In the “Privacy” dialog, click the “+” button on the list
  7. In the Finder folder selection dialog, choose a folder that represents an affected volume, then click the “Choose” button on the bottom right of the dialog
  8. Back in the “Privacy” dialog, click the “Done” button on the bottom right of the dialog
  9. Back in the “Siri & Spotlight”, again click the “Spotlight Privacy…” button on the bottom right of the dialog
  10. Again in the “Privacy” dialog, select the volume you just added in the list, then click the “-” button, confirm you removed the volume and click the “Done” button on the bottom right of the “Privacy” dialog
  11. Close the Preferences dialog.
  12. On the keyboard press ⌘-space to open Spotlight
  13. Type a search term like “Index” then watch the indexing progress bar.

Sounds like a long list, right? That’s why I like mdutil, so:

Back to the terminal

All Spotlight related commands and other man pages:

  • [Wayback/Archive] mdfind(1) finds files matching a given query
  • [Wayback/Archive] mdimport(1) import file hierarchies into the metadata datastore
  • [Wayback/Archive] mdls(1) lists the metadata attributes for the specified file
  • [Wayback/Archive] mdutil(1) manage the metadata stores used by Spotlight
  • [Wayback/Archive] mddiagnose(8) gather information to aid in diagnosing Spotlight issues (on my system, this generates a folder in /tmp which totals to half a gigabyte, so I didn’t investigate this yet)
  • [Wayback/Archive] mds(8)
  • [Wayback/Archive] mdworker(8) metadata server worker (this does all the heavy lifting, so watch their resource usage: amount of processes, thread, memory, CPU usage, disk access)
  • [Wayback/Archive] grep(1) file pattern searcher

    -c--countOnly a count of selected lines is written to standard output.

    See below: needed to estimate on the terminal how busy the Spotlight indexer is.

  • [Wayback/Archive] ps(1) process status
    -A
    Display information about other users’ processes, including those without controlling terminals.
    -c
    Change the “command” column output to just contain the executable name, rather than the full command line.
    -e
    Identical to -A.
    -o
    Display information associated with the space or comma separated list of keywords specified. Multiple keywords may also be given in the form of more than one -o option. Keywords may be appended with an equals (‘=’) sign and a string. This causes the printed header to use the specified string instead of the standard header. If all keywords have empty header texts, no header line is written.

    Also needed to estimate on the terminal how busy the Spotlight indexer is, and since I never can remember single-character command-line switches this is much needed documentation.

All of these are from [Wayback/Archive] Xcode’s man pages which is the web-site generated by the below referenced GitHub repository by these scripts:

Links from the comment above:

Then some pages that confirmed fingin Spotlight indexing progress from a Terminal is next to impossible despite mdworker doing the heavy lifting:

Past blog-posts:

Siri icon: [Wayback/Archive] siri_1200x1200.jpg (1200×480)

Queries:

--jeroen

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.