In terminal I am using mdfind 'kMDItemFSLabel = 6'
to find everything with a Red file label on my Mac. However, it seems to be excluding folders with a red label. I'm trying to get it turn also return folders but I can't seem to even get any of these folder-only queries to work let alone add it to the current file results. mdfind kind:folders
and mdfind 'kMDItemFSLabel=6'
work independently but I can't figure out how to get both:
mdfind kind:folders AND 'kMDItemFSLabel == 6'
mdfind 'kind:folders' AND 'kMDItemFSLabel == 6'
mdfind 'kind:folders kMDItemFSLabel == 6'
mdfind kind:folders kMDItemFSLabel == 6
etc
All I get is Failed to create query for [query above]
What is the correct syntax?
And once I get that right, how do I say "folders AND all files"? Something like `kind:folders+all'?
Edit: Also tried:
mdfind kind:folders && 'kMDItemFSLabel == 6' returns every folder on my computer
mdfind kind:folders && kMDItemFSLabel == 6 returns every folder on my computer
mdfind 'kind:folders' && 'kMDItemFSLabel == 6' returns every folder on my computer
mdfind 'kind:folders && kMDItemFSLabel == 6' failed to create query
tl; dr
Use the following, locale-independent commands:
mdfind 'kMDItemUserTags == Red'
mdfind 'kMDItemUserTags == Red && ! kMDItemContentType == public.folder'
mdfind 'kMDItemUserTags == Red && kMDItemContentType == public.folder'
Simpler, but locale-specific equivalents - these examples only work in English-language locales and must be localized to work with other locales:
mdfind 'tag:red'
mdfind 'tag:red AND NOT kind:folder'
mdfind 'tag:red AND kind:folder'
For background information, read on.
As it turns out, Spotlight - and therefore mdfind
- speak two distinct languages:
<attribName>:<attribVal
pairs
kind
(kMDItemKind
) is explicitly designed to report a localized valueAND
(default), OR
, and NOT
(note that these do not get localized).&&
(default), ||
, and !
If anyone knows what to properly call these two languages, please let me know.
Caveat: These two languages cannot be mixed - a given query string must use either one or the other syntax.
The solutions below are tagged with SL and FL accordingly. Given that SL uses localized names, I recommend using the SL solutions for predictable, locale-independent results, despite being more complex and verbose.
To find all files and folders that are tagged with the red label, use:
FL:
mdfind 'kMDItemUserTags == Red'
SL (English):
mdfind 'tag:red' # in German, `tag:red` would be `attribut:red`
To find files only (as opposed to folders) that are tagged with the red file/folder label, use:
FL:
mdfind 'kMDItemUserTags == Red && ! kMDItemContentType == public.folder'
SL (English):
mdfind 'tag:red AND NOT kind:folder' # in German, `kind:folder` would be `art:ordner`
To find folders only (as opposed to files) that are tagged with the red file/folder label, use:
FL:
mdfind 'kMDItemUserTags == Red && kMDItemContentType == public.folder'
SL (English):
mdfind 'tag:red AND kind:folder'
General tips for using mdfind
:
+
in the top-right corner for creating multiple, AND-combined criteria line by line with the assistance of pop-up lists)*.savedSearch
file (Save
button in the top-right corner, which saves to ~/Library/Saved Searches/
by default),Query:
field.mdimport -A
.
-onlyin <folder>
option.-attr <attribName>
options.Documentation:
Note: The documentation is woefully lacking and, in parts, incorrect.as of 16 May 2015
c
and d
options are explained as needing to be appended to the operator as [c]
and [d]
, as opposed to the (right-hand-side) operand, which is what OSX itself does when you save interactively constructed queries; also, at least on other option exits that's not even mentioned: w
; if you append one or more option letters to a quoted string, their meaning is as followsc
... match case-INsensitivelyd
... ignore diacritics (marks above and below the base letter of foreign characters) when matchingw
... match as a whole word (from what I can tell)*
(any sequence of chars.) and ?
(one char.) wildcards.mdimport -X
and look at the entries of the Types
key.All in all, as of macOS 10.10.3, mdfind
and the Spotlight query language are poorly documented, finicky beasts - trial and error are often your best friends, sadly.
The following seems to work:
mdfind 'kMDItemContentType = "public.folder" && kMDItemUserTags = "Red"cd'
By the way, the cd
on the end of "Red"
means the match should ignore case (upper vs. lower) and diacritical marks (accents, etc.).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With