mac OS X file metadata utilities

mdimport, mdutil, mdcheckschema, mdls

mdfind -- finds files matching a metadata query

mdfind [-live] [-count] [-onlyin directory] query

Consults the central metadata store and returns a list of files that match the metadata query.
The query can be a string or a query expression.
-0 outputs an NULL after each result path. useful with xargs -0.
-live Causes the mdfind command to provide live-updates to the number of files matching the query. When an update causes the query results to change the number of matches is updated. The find can be cancelled by typing ctrl-C.
-count
-onlyin dir Limit the scope of the search to dir
‑interpret as if the user had typed the string into the Spotlight menu. For example, the string "search" would produce the following query string:
                   (* = search* cdw || kMDItemTextContent = search* cdw)
-literal don't interpret
EXAMPLES returns all files with any metadata attribute value matching the string "image":

  mdfind image
returns all files that contain "MyFavoriteAuthor" in the kMDItemAuthor metadata attribute:
   mdfind "kMDItemAuthor == '*MyFavoriteAuthor*'"
This returns all files with any metadata attribute value matching the string "skateboard". The find continues to run after gathering the initial results, providing a count of the number of files that match the query.
   mdfind -live skateboard

To open all the files in Preview :

 
$ mdfind -onlyin /Users/shared/books "kiss kate" | xargs open
To make the search term more selective, use grep and regular expressions.
$ cat file1.txt 
Kiss me 
$ cat file3.txt 
Kiss me, kate 
$ mdfind -onlyin /Users/saruman/splotlight-test "kiss me," 
/Users/saruman/splotlight-test/file1.txt
/Users/saruman/splotlight-test/file3.txt 
The comma is ignored by spotlight. Using grep:
$ mdfind -onlyin /Users/saruman/splotlight-test "kiss me," | xargs grep -i "Kiss me,"
/Users/saruman/splotlight-test/file3.txt:Kiss me, kate
grep: /Users/saruman/splotlight-test/file: No such file or directory
grep: 4.txt: No such file or directory
$
Use grep to make the search term case sensitive. The command failed on the filename file 4.txt because it contains a space. Specify option -0 (zero) to both mdfind and xargs .
$ mdfind -0 -onlyin /Users/saruman/splotlight-test "kiss me," | xargs -0 grep -i "Kiss me,"
/Users/saruman/splotlight-test/file3.txt:Kiss me, kate
/Users/saruman/splotlight-test/file 4.txt:Kiss me, kate

mdfind, mdutil, mdcheckschema, mdls

mdimport -- import file hierarchies into the metadata datastore.

mdimport [-VXLrg] [-d level | category] [-w delay] file | directory import metadata from files or file system hierarchies.
-A output the attributes and their localizations in the current language
-X output the schema
mdimport -X
2008-03-27 20:54:56.169 mdimport[287:10b] Schema: id(501) (null)
-r Ask the server to reimport files for UTIs claimed by the listed plugin.
reimport all of the chat files on the system to be :
mdimport -r /System/Library/Spotlight/Chat.mdimporter
-L list of installed importers
-d level set debug level 1-4
-p output performance information
-V output timing information
-n Don't actually set the attributes on the file. Useful for debugging.
‑w msecs Wait for the specified interval between scanning files.
-g plugin Load the plugin at the provided path, ignoring system installed plugins. Useful for testing plugins that have not yet been installed.


mdfind, mdimport, mdcheckschema, mdls

mdutil -- manage the metadata stores used by Spotlight

mdutil [-pEsavd] [-i on | off] volumei ...

-i on | off Sets the indexing status for volumes .
may be delayed due to low disk space or other conditions.
-E ERASE volume's store.
-pflush (purge) locally cached files to the network device
-a all volumes.
-s output status
-ddisable SEARCHes on volume
-v verbose
 mdutil -s -a
/Volumes/PHOTOS:
    Indexing and searching disabled.
/:
    Indexing and searching disabled.
/Volumes/BACKUPS/Backups.backupdb:
    Indexing enabled.
    Scan base time: 2008-03-24 19:29:35 -0400 (264962 seconds ago), reasoning: 'scan required - mount side'
/Volumes/DATA-1:
    Indexing disabled.
/Volumes/DATA:
    Indexing and searching disabled.
/Volumes/BACKUPS:
    Indexing and searching disabled.

-p Flush local caches to the network device.


mdfind, mdimport, mdutil, mdls

mdcheckschema schema.xml [...]

The mdcheckschema command validates the mdimporter schema files provided as arguments and indicates whether the schema could be successfully parsed.


mdfind, mdimport, mdutil, mdcheckschema

mdls -- lists the metadata attributes for the specified file

mdls [-name attributeName] [-raw [-nullMarker markerString]] file ...

outputs the values of all the metadata attributes associated with files

-name attributeNameonly matching metadata attribute. multiple occurances permitted
-raw output raw attribute data in the order that was requested.
Fields separated with a NULL useful with xargs -0.
-nullMarker markerString Sets a marker string to be used when a requested attribute is null. Only with -raw . Default is null

> mdls 0installScreen-090502
kMDItemContentCreationDate     = 2009-05-03 10:16:14 -0400
kMDItemContentModificationDate = 2009-05-03 10:16:14 -0400
kMDItemContentType             = "public.data"
kMDItemContentTypeTree         = (
    "public.data",
    "public.item"
)
kMDItemDisplayName             = "0installScreen-090502"
kMDItemFSContentChangeDate    = 2009-05-03 10:16:14 -0400
kMDItemFSCreationDate         = 2009-05-03 10:16:14 -0400
kMDItemFSCreatorCode          = ""
kMDItemFSFinderFlags          = 0
kMDItemFSHasCustomIcon        = 0
kMDItemFSInvisible            = 0
kMDItemFSIsExtensionHidden    = 0
kMDItemFSIsStationery         = 0
kMDItemFSLabel                = 0
kMDItemFSName                 = "0installScreen-090502"
kMDItemFSNodeCount            = 0
kMDItemFSOwnerGroupID         = 80
kMDItemFSOwnerUserID          = 501
kMDItemFSSize                 = 185107
kMDItemFSTypeCode             = ""
kMDItemKind                   = "Plain text"
kMDItemLastUsedDate           = 2009-05-03 10:16:14 -0400
kMDItemUsedDates              = ( 2009-05-03 00:00:00 -0400)

mdls hereWeGo.aiff
kMDItemAudioBitRate            = 352800
kMDItemAudioChannelCount       = 1
kMDItemAudioSampleRate         = 22050
kMDItemBitsPerSample           = 16
kMDItemContentCreationDate     = 2015-01-08 22:31:31 +0000
kMDItemContentModificationDate = 2015-01-08 22:31:34 +0000
kMDItemContentType             = "public.aifc-audio"
kMDItemContentTypeTree         = (
    "public.aifc-audio",
    "public.audio",
    "public.audiovisual-content",
    "public.data",
    "public.item",
    "public.content"
)
kMDItemDateAdded               = 2015-01-08 22:31:31 +0000
kMDItemDisplayName             = "hereWeGo.aiff"
kMDItemDurationSeconds         = 0.884172335600907
kMDItemFSContentChangeDate     = 2015-01-08 22:31:34 +0000
kMDItemFSCreationDate          = 2015-01-08 22:31:31 +0000
kMDItemFSCreatorCode           = ""
kMDItemFSFinderFlags           = 0
kMDItemFSHasCustomIcon         = (null)
kMDItemFSInvisible             = 0
kMDItemFSIsExtensionHidden     = 0
kMDItemFSIsStationery          = (null)
kMDItemFSLabel                 = 0
kMDItemFSName                  = "hereWeGo.aiff"
kMDItemFSNodeCount             = (null)
kMDItemFSOwnerGroupID          = 20
kMDItemFSOwnerUserID           = 501
kMDItemFSSize                  = 43088
kMDItemFSTypeCode              = ""
kMDItemKind                    = "AIFF-C audio"
kMDItemLogicalSize             = 43088
kMDItemMediaTypes              = (
    Sound
)
kMDItemPhysicalSize            = 45056
kMDItemTotalBitRate            = 352800

 mdls /Volumes/DATA/dgerman/CreditCardF.jpg
kMDItemAcquisitionMake         = "Canon"
kMDItemAcquisitionModel        = "CanoScan 5600F"
kMDItemBitsPerSample           = 32
kMDItemColorSpace              = "RGB"
kMDItemContentCreationDate     = 2013-08-30 16:35:08 +0000
kMDItemContentModificationDate = 2013-08-30 16:35:08 +0000
kMDItemContentType             = "public.jpeg"
kMDItemContentTypeTree         = (
    "public.jpeg",
    "public.image",
    "public.data",
    "public.item",
    "public.content"
)
kMDItemCreator                 = "MP Navigator EX 2.0"
kMDItemDateAdded               = 2014-08-22 01:36:06 +0000
kMDItemDisplayName             = "CreditCardF.jpg"
kMDItemEXIFVersion             = "2.2.1"
kMDItemFSContentChangeDate     = 2013-08-30 16:35:08 +0000
kMDItemFSCreationDate          = 2013-08-30 16:35:08 +0000
kMDItemFSCreatorCode           = ""
kMDItemFSFinderFlags           = 0
kMDItemFSHasCustomIcon         = (null)
kMDItemFSInvisible             = 0
kMDItemFSIsExtensionHidden     = 0
kMDItemFSIsStationery          = (null)
kMDItemFSLabel                 = 0
kMDItemFSName                  = "CreditCardF.jpg"
kMDItemFSNodeCount             = (null)
kMDItemFSOwnerGroupID          = 20
kMDItemFSOwnerUserID           = 501
kMDItemFSSize                  = 1250728
kMDItemFSTypeCode              = ""
kMDItemHasAlphaChannel         = 0
kMDItemKind                    = "JPEG image"
kMDItemLogicalSize             = 1250728
kMDItemOrientation             = 0
kMDItemPhysicalSize            = 1253376
kMDItemPixelCount              = 4436545
kMDItemPixelHeight             = 1945
kMDItemPixelWidth              = 2281
kMDItemProfileName             = "sRGB IEC61966-2.1"
kMDItemResolutionHeightDPI     = 600
kMDItemResolutionWidthDPI      = 600
kMDItemWhiteBalance            = 0
mdattributes

ContentTreeType

public
 .data, .item, .folder, .directory, .content .text, .plain-text, .html, .executable
 .unix-executable, .composite-content, .source-code, .shell-script, .script, .content,
 .perl-script, .log

    com.microsoft.word.doc,
    com.adobe.pdf,
    com.netscape.javascript-source,
    com.microsoft.excel.xls,
    com.apple.log,
    com.apple.finder.burn-folder,


/System/Library/LaunchDaemons/com.apple.metadata.com.apple.metadata.mds.scan.plist <key>Label <string>com.apple.metadata.mds.scan <key>ProgramArguments <string>/System/Library/Frameworks/CoreServices.framework/Frameworks/Metadata.framework/Versions/A/Support/mdworker <string>-s <string>mdworker-scan <string>-c <string>MDSSyncScanWorker <string>-m <string>com.apple.metadata.mds.scan <key>MachServices <key>com.apple.metadata.mds.scan <key>ResetAtClose <true> <key>POSIXSpawnType <string>Interactive <key>CFBundleIdentifier <string>com.apple.Spotlight <key>LowPriorityBackgroundIO <false>
<key>Label <string>com.apple.metadata.mds <key>ProgramArguments <string>/System/Library/Frameworks/CoreServices.framework/Frameworks/Metadata.framework/Support/mds <key>MachServices <dict> <key>com.apple.metadata.mds <true> <key>com.apple.metadata.mds.legacy <true> <key>com.apple.metadata.mds.xpcs <true> <key>com.apple.metadata.mds.cachedelete <true> </dict> <key>POSIXSpawnType <string>Interactive <key>KeepAlive <true> <key>SoftResourceLimits < <key>NumberOfFiles 2048 < <key>HardResourceLimits < <key>NumberOfFiles 2048 < <key>CFBundleIdentifier <string>com.apple.Spotlight <key>LowPriorityBackgroundIO <false> <key>EnvironmentVariables <
/System/Library/LaunchDaemons/com.apple.metadata.mds.index.plist <key>Label <string>com.apple.metadata.mds.index <key>ProgramArguments <string>/System/Library/Frameworks/CoreServices.framework/Frameworks/Metadata.framework/Versions/A/Support/mds_stores <key>MachServices <key>com.apple.metadata.mds.index <true> <key>com.apple.metadata.mds.index.xpc <true> <key>SoftResourceLimits <key>NumberOfFiles 2048 <key>HardResourceLimits <key>NumberOfFiles 2048 <key>LowPriorityBackgroundIO <true> <key>CFBundleIdentifier <string>com.apple.Spotlight <key>POSIXSpawnType <string>Interactive <key>EnableTransactions <true> <key>EnvironmentVariables <dict/> apple.spotlight.IndexAgent apple.Spotlight
/System/Library/LaunchDaemons/com.apple.diagnosticextensions.osx.spotlight.helper.plist <key>Label <string>com.apple.diagnosticextensions.osx.spotlight.helper <key>Program(sic)?Arguments? <string>/System/Library/PrivateFrameworks/DiagnosticExtensions.framework/PlugIns/osx-spotlight.appex/Contents/XPCServices/spotlighthelper <key>MachServices <key>com.apple.diagnosticextensions.osx.spotlight.helper <true> <key>EnableTransactions <true> <key>EnablePressuredExit <true> <key>RunAtLoad <false> <key>POSIXSpawnType <string>Adaptive >launchctl list |grep 'md' |cut -f3-4 -d '.'|sortc /usr/bin/sort --ignore-leading-blanks --ignore-case | uniq -c | sort -n |more 1 metadata.mdbulkimport 1 metadata.mdflagwriter 1 metadata.mdwrite 1 mdmclient.agent 1 mdworker.bundles 1 mdworker.isolation 1 mdworker.sizing 3 mdworker.32bit 3 mdworker.lsb 5 mdworker.mail 5 mdworker.shared 9 mdworker.single

errors

mds[64]: (DiskStore.Error:235) Unable to create/find index location 974713FF-7724-4479-9EF1-48A552026443 0