Where does Spotlight store its metadata index?
Where does Mac OS X store Spotlight comments associated with a file? Is it stored as HFS+ file metadata, or is the information kept by the OS somewhere else?
The metadata is inside a hidden .Spotlight-V100
folder at the root of the indexed volume.
The values of the kMDItemFinderComment
attributes that get stored in metadata store directories like /.Spotlight-V100/
depend on the com.apple.metadata:kMDItemFinderComment
extended attributes.
Finder also stores the Spotlight comments of all items in a folder in a .DS_Store
file.
You can print Spotlight comments with mdls
or xattr
:
mdls -n kMDItemFinderComment test.txt
xattr -p com.apple.metadata:kMDItemFinderComment file.txt | xxd -r -p | plutil -convert xml1 -o - - | ruby -rcgi -e 'puts CGI.unescapeHTML(STDIN.read.scan(/<string>(.*)<\/string>/m)[0][0])'
If you use Finder to add a Spotlight comment for a file you don't have write permission to (like some application bundles in /Applications/
), the comment won't be saved as an extended attribute, and it won't get picked up by Spotlight. You can open Finder as the superuser or use sudo xattr -w
though.
If you delete a .DS_Store
file from a folder and quit and reopen Finder, the Spotlight comments of all files in the folder disappear from Finder's information windows. If there are still extended attributes for the Spotlight comments, the comments are seen by Spotlight though.
If you use xattr
to add a com.apple.metadata:kMDItemFinderComment
extended attribute, the comment gets picked up by Spotligt, but it won't be shown in Finder's information windows.
xattr -w com.apple.metadata:kMDItemFinderComment comment file.txt
If you use AppleScript to change the comment
attribute of a file, the comment is also saved in a .DS_Store
file and shown in Finder.
osascript -e 'on run {f, c}' -e 'tell app "Finder" to set comment of (POSIX file f as alias) to c' -e end file.txt comment
(There is a warning like CFURLGetFSRef was passed this URL which has no scheme
in 10.8 when a relative path is converted to an alias, but you can ignore it.)