macdevcenter.com
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

The Power of mdfind
Pages: 1, 2, 3, 4

Listing Metadata with mdls

The mdls command is the partner to mdfind. The ls portion of mdls is an analogue to the Unix command ls which lists files in a directory. In this case, mdls lists the metadata attributes associated with a given file.

Here's the metadata for a Word document I created when updating Mac OS X Tiger In A Nutshell.

$ mdls ~/mosxnut3/ch02-addendum.doc
ch02-addendum.doc -------------
kMDItemAttributeChangeDate
    = 2005-12-11 21:45:50 -0600
kMDItemAuthors                 = ("Andy Lester")
kMDItemContentCreationDate
    = 2005-09-14 21:26:58 -0500
kMDItemContentModificationDate
    = 2005-09-14 21:26:58 -0500
kMDItemContentType
    = "com.microsoft.word.doc"
kMDItemContentTypeTree
    = ("com.microsoft.word.doc", "public.data",
    "public.item")
kMDItemDisplayName
    = "ch02-addendum.doc"
kMDItemFSContentChangeDate
    = 2005-09-14 21:26:58 -0500
kMDItemFSCreationDate
    = 2005-09-14 21:26:58 -0500
kMDItemFSCreatorCode           = 0
kMDItemFSFinderFlags           = 0
kMDItemFSInvisible             = 0
kMDItemFSIsExtensionHidden     = 0
kMDItemFSLabel                 = 0
kMDItemFSName
    = "ch02-addendum.doc"
kMDItemFSNodeCount             = 0
kMDItemFSOwnerGroupID          = 501
kMDItemFSOwnerUserID           = 501
kMDItemFSSize                  = 68382
kMDItemFSTypeCode              = 0
kMDItemID                      = 246252
kMDItemKeywords
    = (Tiger, Nutshell, macosx)
kMDItemKind
    = "Microsoft Word document"
kMDItemLastUsedDate
    = 2005-12-11 21:45:47 -0600
kMDItemTitle
    = "Mac OS X Tiger In A Nutshell -- Chapter
    2 -- additional commands"
kMDItemUsedDates
    = (2005-09-14 21:26:58 -0500, 2005-12-11
    18:00:00 -0600)

The attribute names should be pretty self-explanatory. FS refers to the filesystem, the name for how files are stored on the hard drive, so all the kMDItemFS attributes give information about the files themselves, and not the content. Note that this may be different than information held internally in a specific format.

Each different file format may have specific information unique to that format. The values Tiger, Nutshell, and macosx were entered by me in Microsoft Word in File Properties, which Spotlight then indexed into the kMDItemKeywords attribute. Some metadata is figured out by Spotlight itself, as with the dimensions of a JPEG image.

The attributes for a media file are very different.

$ mdls "05 Power Of Two.m4a"
05 Power Of Two.m4a -------------
kMDItemAlbum                    = "Swamp Ophelia"
kMDItemAttributeChangeDate
    = 2005-11-03 19:08:52 -0600
kMDItemAudioBitRate             = 112024
kMDItemAudioChannelCount        = 2
kMDItemAudioEncodingApplication
    = "iTunes v6.0.1, QuickTime 7.0.3"
kMDItemAudioTrackNumber         = 5
kMDItemAuthors                  = ("Indigo Girls")
kMDItemCodecs                   = (AAC)
kMDItemComposer                 = "Saliers, Emily"
kMDItemContentCreationDate
    = 2005-11-03 19:08:28 -0600
kMDItemContentModificationDate
    = 2005-11-03 19:08:52 -0600
kMDItemContentType
    = "public.mpeg-4-audio"
kMDItemContentTypeTree          = (
    "public.mpeg-4-audio",
    "public.audio",
    "public.audiovisual-content",
    "public.data",
    "public.item",
    "public.content"
)
kMDItemDisplayName
    = "05 Power Of Two.m4a"
kMDItemDurationSeconds
    = 322.5483900226757
kMDItemFSContentChangeDate
    = 2005-11-03 19:08:52 -0600
kMDItemFSCreationDate
    = 2005-11-03 19:08:28 -0600
kMDItemFSCreatorCode            = 1752133483
kMDItemFSFinderFlags            = 0
kMDItemFSInvisible              = 0
kMDItemFSIsExtensionHidden      = 0
kMDItemFSLabel                  = 0
kMDItemFSName
    = "05 Power Of Two.m4a"
kMDItemFSNodeCount              = 0
kMDItemFSOwnerGroupID           = 20
kMDItemFSOwnerUserID            = 501
kMDItemFSSize                   = 4582797
kMDItemFSTypeCode               = 1295270176
kMDItemID                       = 2099725
kMDItemKind
    = "MPEG-4 Audio File"
kMDItemLastUsedDate
    = 2005-11-03 19:08:29 -0600
kMDItemMediaTypes               = (Sound)
kMDItemMusicalGenre             = "Rock"
kMDItemStreamable               = 0
kMDItemTitle                    = "Power Of Two"
kMDItemTotalBitRate             = 112024
kMDItemUsedDates
    = (2005-11-03 19:08:29 -0600)

If I'm only interested in certain attributes, I can use the -name option:

$ mdls -name kMDItemComposer "11 Space Truckin'.m4a"
11 Space Truckin'.m4a -------------
kMDItemComposer = "Blackmore/Gillan/Glover/Lord/Paice"

Now that I know some attribute names, I can get very precise in how I search. Say I want to find songs composed by Roger Waters. I need to search the kMDItemComposer attribute for "Waters". I'll put the string I'm searching for in double quotes, and then the entire search expression in single quotes.

$ mdfind 'kMDItemComposer = "Waters"'
/Users/andy/Music/iTunes/iTunes Music/Pink Floyd/
    Pulse/2-06 Money.m4a
/Users/andy/Music/iTunes/iTunes Music/Pink Floyd/
    Pulse/2-09 Brain Damage.m4a
/Users/andy/Music/iTunes/iTunes Music/Pink Floyd/
    Pulse/2-10 Eclipse.m4a

I know that I have more than three songs written by Roger Waters, so I'll rerun the search with wildcards, with an asterisk to mean "any string."

$ mdfind 'kMDItemComposer = "*Waters*"'
/Users/andy/Music/iTunes/iTunes Music/Pink Floyd/
    Animals/02 Dogs.m4a
/Users/andy/Music/iTunes/iTunes Music/Pink Floyd/
    Dark Side Of The Moon/01 Speak To Me _ Breathe.m4a
/Users/andy/Music/iTunes/iTunes Music/Pink Floyd/
    Dark Side Of The Moon/02 On The Run.m4a
... 42 more tracks ...

If I want a case-insensitive search, I can put the letter c outside the double quotes, as in this search to find all forms of "McCartney", regardless of the capitalization.

$ mdfind 'kMDItemComposer = "*mccartney*"c'

Of course, all of this searching for data like composer names depends on the accuracy of the data in the files themselves. Chances are, if you ripped a CD into your iTunes, the data about the music came from an automatic lookup to the Gracenote database, which may or may not have such information entered. If the data's not in the file, then Spotlight can't search against it.

I'm not limited to testing for strings. I can also compare numeric values, with standard arithmetic operators. Maybe I want to find all my music files that were sampled at a bit rate lower than 128K:

$ mdfind 'kMDItemAudioBitRate < 128000'

or all the songs longer than 10 minutes

$ mdfind 'MDItemDurationSeconds > 600'

Pages: 1, 2, 3, 4

Next Pagearrow