Sub topic 3
Choosing What Spotlight Shows and Excluding Folders
Assume you can open Finder, use basic keyboard shortcuts, and know where your files are stored. Terminal commands are optional for this chapter.
Control Spotlight results
Open System Settings > Spotlight > Search Results to choose which categories appear, such as Applications, Documents, Messages, or System Settings. Turn categories off when their results add noise.
To prevent private or irrelevant locations from appearing, open System Settings > Spotlight > Spotlight Privacy. Add a folder or disk. Spotlight then excludes that location from searches and stops indexing it for normal Spotlight use.
Search in Finder
Finder provides more control than the Spotlight pop-up. Press Command-F, enter a term, and choose This Mac or the current folder. Use the filter button to add criteria such as file type, creation date, or location.
A Finder search can also become a saved search. Choose File > New Smart Folder, add search criteria, and click Save. A Smart Folder stores the rules, not copies of the matching files, so its contents change as files change.
The index and file metadata
Spotlight searches an index, which is a database of file names, text, locations, and metadata. Metadata is information about a file, such as its kind, size, author, or creation date. The mdls command displays the metadata stored for one file.
mdls ~/Documents/report.pdfIf a file was recently created, its contents may not appear until indexing finishes.
When results are missing
First check whether indexing is enabled:
mdutil -s /Check Spotlight Privacy for an accidental exclusion. If the index is stale or incomplete, rebuilding it can help:
sudo mdutil -E /Rebuilding takes time and may use extra CPU. Avoid interrupting it unless necessary.
Spotlight from Terminal
mdfind searches the Spotlight index. This command searches file names for “invoice”:
mdfind -name "invoice"This command searches only Documents for PDF files:
mdfind -onlyin ~/Documents "kMDItemContentType == 'com.adobe.pdf'"Use mdls to discover metadata attributes before writing more specific queries.
Exercise
Which tool should you use to inspect a file's indexed metadata, and which tool searches that index?
Show answer
Use mdls to inspect metadata and mdfind to search the Spotlight index.