Spotlight is Apple's system-wide search function and metadata store, first introduced in Mac OS 10.4. It not only indexes files' name and content, but also other assorted attributes, such as the dimensions of an image or the BPM of an mp3.
Accessing Spotlight is easy—just click the magnifying glass icon in the upper-right corner on any Macintosh running Tiger. Start typing, and search results appear in realtime. In order to achieve such a near-instantaneous response, Spotlight keeps an index of all the files on your hard drive, as well as their metadata. When you restart your computer after installing 10.4, this index must be built from scratch; your computer will run slowly for an hour or two. After this initial indexing, Mac OS X uses kqueue to keep Spotlight in sync with the filesystem.
While the menu item is the easiest way to use Spotlight, you can also search from the Finder's search box or from the Terminal. Mac OS 10.4 installs several command-line utilities for searching and managing the Spotlight metadata cache:
mdfind has the same general function as the spotlight menu item. Typing
mdfind something into your terminal window gives you the same results as typing "something" into the Spotlight window would. The power of mdfind doesn't stop there, however. If you know the particular metadata key you'd like to query, such as an "kMDItemUrl" containing "google," type
mdfind "kMDItemUrl == '*google*'". Raw queries also support more advanced boolean operators like OR (||) and AND (&&). The Finder can access this "raw query" capability also—start a new search or smart folder, and instead of "Kind" select "Other..." then "Raw Query." Enter your query in the box that appears.
mdls is a simple command with a singular purpose: list all the metadata associated with a particular file. Whisper
mdls path/to/file into a Terminal window to view path/to/file's attributes in all their glory. If you'd like to see just one attribute, you can use
mdls -name myFavoriteAttribute path/to/file. This will print the value of "myFavoriteAttribute," if it even exists.
mdimport is how you tell Spotlight to import files. Usually it will do this automatically, but there are some instances in which you'd rather do it yourself (e.g., you're writing your own importer and would like to debug it). If you ever feel like reindexing a file or directory, enter
mdimport path/to/file/or/directory, and Spotlight will comply.
mdutil manages metadata indexes for whole volumes. Since this has potentially destructive capabilities, it must be run as root, usually via sudo. If Spotlight is acting up or can't find anything, sometimes the best option is just to wipe it out and start over. Don't worry, this doesn't destroy any actual data, and Spotlight will begin rebuilding the index right away. To erase the Spotlight index for a volume, yell
sudo mdutil -E /Volumes/Your_Hard_Drive into a Terminal window. If you want the index to stay dead instead of rebuilding, seal its tomb with the incantation
sudo mdutil -i off /Volumes/Your_Hard_Drive. You can bring it back again with
sudo mdutil -i on /Volumes/Your_Hard_Drive.
Still want more information on Spotlight? Apple has a nice tips page, which you can view at http://www.apple.com/macosx/tips/spotlight.html. Technical information lives at http://developer.apple.com/macosx/spotlight.html. If you're looking for a huge list of metadata attributes to use in your raw queries, that's over at http://developer.apple.com/documentation/Carbon/Reference/MetadataAttributesRef/Reference/CommonAttrs.html.