Compound mdfind search

戏子无情 提交于 2021-02-08 07:56:41

问题


The goal is to find all images that are in a certain state or country.

mdfind kind:image

will find all images

mdfind "kMDItemCountry == 'United States'"

will find all files what have this property.

mdfind kind:image "kMDItemCountry == 'United States'" 

fails. I've tried using && and various groupings of () but can't make it work. How can I successfully combine 'kind:image' with attribute searches like kMDIxxx?


回答1:


I have had a chance to do some experiments as to how mdfind really works, as opposed to how it is documented - the two differ.

First, let's look at the Spotlight data for a file called tardis.png that was hanging around on my Mac.

mdls tardis.png

Output

_kMDItemOwnerUserID            = 501
kMDItemBitsPerSample           = 32
kMDItemColorSpace              = "RGB"
kMDItemContentCreationDate     = 2017-05-31 14:15:03 +0000
kMDItemContentModificationDate = 2017-05-31 14:15:03 +0000
kMDItemContentType             = "public.png"
kMDItemContentTypeTree         = (
    "public.png",
    "public.item",
    "public.png",
    "public.data",
    "public.image",
    "public.content"
)
kMDItemCreator                 = "Adobe Photoshop CC 2017 (Macintosh)"
kMDItemDateAdded               = 2017-05-31 14:15:23 +0000
kMDItemDisplayName             = "tardis.png"
kMDItemFSContentChangeDate     = 2017-05-31 14:15:23 +0000
kMDItemFSCreationDate          = 2017-05-31 14:15:22 +0000
kMDItemFSCreatorCode           = "8BIM"
kMDItemFSFinderFlags           = 0
kMDItemFSHasCustomIcon         = (null)
kMDItemFSInvisible             = 0
kMDItemFSIsExtensionHidden     = 0
kMDItemFSIsStationery          = (null)
kMDItemFSLabel                 = 0
kMDItemFSName                  = "tardis.png"
kMDItemFSNodeCount             = (null)
kMDItemFSOwnerGroupID          = 20
kMDItemFSOwnerUserID           = 501
kMDItemFSSize                  = 1488497
kMDItemFSTypeCode              = "PNGf"
kMDItemHasAlphaChannel         = 0
kMDItemKind                    = "Portable Network Graphics image"
kMDItemLogicalSize             = 1488497
kMDItemOrientation             = 1
kMDItemPhysicalSize            = 1490944
kMDItemPixelCount              = 2091600
kMDItemPixelHeight             = 1494
kMDItemPixelWidth              = 1400
kMDItemProfileName             = "sRGB IEC61966-2.1"
kMDItemResolutionHeightDPI     = 72
kMDItemResolutionWidthDPI      = 72

And here is a query that matches and lists that file:

mdfind -onlyin . "(kMDItemPixelHeight>1500 || kMDItemPixelWidth=1400)&& kMDItemKind='*image*' && kMDItemProfileName='srgb*'c"

That shows how to do compound searches with && (AND) and || (OR), how to group conditions within parentheses (conditionA || conditionB) and also how to do case-insensitive search (by appending c after the search string).


Examples: maybe more for my own reference ;-)

List all cameras and scanners that I have used to make pictures on my Mac:

mdfind -0 kind:image |  xargs -0 mdls -n kMDItemAcquisitionModel | sort -u
kMDItemAcquisitionModel = "645 PRO Mk III for iOS"
kMDItemAcquisitionModel = "BlackBerry 8900"
kMDItemAcquisitionModel = "BlackBerry 9000"
kMDItemAcquisitionModel = "Canon EOS 5D Mark III"
kMDItemAcquisitionModel = "Canon EOS 5D"
kMDItemAcquisitionModel = "Canon EOS 7D Mark II"
kMDItemAcquisitionModel = "Canon EOS 7D"
kMDItemAcquisitionModel = "Canon EOS-1D Mark II N"
kMDItemAcquisitionModel = "Canon EOS-1D Mark IV"
kMDItemAcquisitionModel = "Canon EOS-1D X"
kMDItemAcquisitionModel = "Canon EOS-1DS"
kMDItemAcquisitionModel = "Canon EOS-1Ds Mark II"
kMDItemAcquisitionModel = "Canon EOS-1Ds Mark III"
kMDItemAcquisitionModel = "EOS-1Ds Mark III"
kMDItemAcquisitionModel = "EPSON Perfection 4990"
kMDItemAcquisitionModel = "NIKON D800"
kMDItemAcquisitionModel = "NIKON D800E"
kMDItemAcquisitionModel = "Perfection 4990"
kMDItemAcquisitionModel = "Perfection4990"
kMDItemAcquisitionModel = "PerfectionV700  "
kMDItemAcquisitionModel = "PerfectionV700"
kMDItemAcquisitionModel = "TANGO           "
kMDItemAcquisitionModel = "TANGO"
kMDItemAcquisitionModel = "iPad mini 4"
kMDItemAcquisitionModel = "iPad mini"
kMDItemAcquisitionModel = "iPad"
kMDItemAcquisitionModel = "iPhone 3G"
kMDItemAcquisitionModel = "iPhone 3GS"
kMDItemAcquisitionModel = "iPhone 4"
kMDItemAcquisitionModel = "iPhone 4S"
kMDItemAcquisitionModel = "iPhone 5"
kMDItemAcquisitionModel = "iPhone 5s"
kMDItemAcquisitionModel = "iPhone 6"
kMDItemAcquisitionModel = "iPhone 6s"
kMDItemAcquisitionModel = "iPhone SE"
kMDItemAcquisitionModel = "iPhone"

Find all photos shot on iPhone 6:

mdfind "kMDItemAcquisitionModel='*iPhone*6*'"

Notes:

  • The man-page says -onlyin limits the scope of the search to the specified directory. That is not really correct, because it recurses into contained directories too.

  • It seems to make no difference whether you use single (=) or double (==) equals signs in comparisons.

  • The documentation says you can use string==[c]"pattern" for case-insensitive search, that is incorrect and the syntax I have shown above does work on macOS Sierra at least.



来源:https://stackoverflow.com/questions/45899411/compound-mdfind-search

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!