Sub topic 5
Finding everything that shares a tag
This chapter covers how to find every item that carries a given tag. It assumes you can already apply and remove tags, and that you know Spotlight keeps an index of file metadata. You should also be comfortable with Finder windows, the sidebar, and the search field.
By the end you will be able to list tagged items from the sidebar, build tag searches by hand, combine several tags, and read the equivalent query in Terminal.
What a tag search actually does
A tag is stored as metadata on the file. Finder does not scan your folders for it. It asks Spotlight, which keeps an index of metadata for each item, to return the items whose tag attribute matches. That attribute is kMDItemUserTags.
This has two consequences:
- A tag search spans every indexed location at once. Files in Documents, Desktop, iCloud Drive and external volumes all appear in one list.
- A tag search is only as good as the index. If an item is not indexed, it will not show up, even if it is tagged.
The diagram below shows the path from your click to the result list.
Method 1: click a tag in the sidebar
The quickest way is the sidebar. Under the Tags heading, click a tag such as Red. Finder opens a results window that lists every item with that tag.
If you do not see the Tags section, open Finder > Settings > Sidebar and turn on the tags you want. The next chapters cover that setting in detail.
A few behaviours worth knowing:
- Results are not limited to one folder. The window title is the tag name, and the items come from across your Mac.
- You can change the view (icons, list, columns, gallery) and the sort order like any other Finder window.
- In list view, add the Tags column through View > Show View Options so you can see every tag on each result.
- Selecting a result and pressing Command-R (Show Original) reveals where the file actually lives.
Method 2: type the tag into the search field
You can also search from any Finder window.
- Press Command-F, or click the search field.
- Type the tag name, for example
Red. - In the suggestions list, choose the entry marked Tags rather than a plain text match.
The distinction matters. A plain text search for Red matches file names and contents that contain the word. Choosing the tag suggestion creates a search token that matches only items carrying the tag. If you press Return without choosing the suggestion, you get the broader text search, and you will see unrelated files.
Method 3: add a Tags criterion to a search
For precise control, use the criteria rows.
- Press Command-F in a Finder window.
- Click the plus (+) button under the search field to add a row.
- Open the first pop-up menu and choose Other…. In the dialog, search for "Tags" and tick it. Tick In Menu if you want it available directly next time.
- Set the row to the tag name.
This approach lets you combine the tag with other conditions, such as kind or date. For example, you can find only PDFs tagged Red. That same set of rows is what a smart folder saves, which is the subject of a later chapter.
Scope: where Finder looks
A search has a scope, shown as buttons under the search field.
| Scope button | What it searches |
|---|---|
| This Mac | Everything indexed on the Mac, including connected volumes that are indexed |
| Current folder (named after the folder) | Only the folder you were viewing, and its subfolders |
| Recent (shown in some versions) | Items you used recently |
Clicking a tag in the sidebar always uses the whole Mac. If you start a search from inside a folder and then type a tag, Finder may default to the current folder. Check the scope buttons when results look incomplete.
Combining several tags
Finder does not offer a single checkbox for "has both Red and Work". You have these options:
- Add two Tags rows. Each row is a separate condition, and by default the rows are combined with AND, so only items with both tags appear.
- Use the Any/All choice. When you hold the Option key, the plus button changes to an ellipsis (…). Choosing it adds a compound group where you can select any, all or none of the nested conditions. Use any to find items with Red or Work.
- Exclude a tag. Use the none option in a compound group to find items that do not carry a tag.
Be aware that tag matching is by name. Two tags with the same name but different colors are treated as one name in most cases, and renaming a tag later does not rewrite old matches in a saved search stored under the old name. Check any saved search after you rename a tag.
The same search in Terminal
Terminal shows what Finder asks of Spotlight. Start by reading the tags on one file.
mdls -name kMDItemUserTags ~/Documents/report.pdfThe output is a list of tag names. A tag with a color may show the name followed by a newline and a number, for example Red then 6. The number is the color label index.
Now run the search that matches clicking Red in the sidebar, limited to Documents.
mdfind -onlyin ~/Documents 'kMDItemUserTags == "Red"'mdfind prints one full path per line. Leave out -onlyin to search the whole index.
Note the exact comparison ==. It matches an item that has a tag named exactly Red. If you want a looser match, add a wildcard and the case-insensitive modifier.
mdfind -onlyin ~/Documents 'kMDItemUserTags == "*red*"c'This returns tags that contain "red" in any case, so it would also match a tag called Credit. Because of that, use wildcards only when you accept the extra matches.
Common problems with tag searches
| Symptom | Likely cause | What to try |
|---|---|---|
| A tagged file is missing | The item is in a folder excluded from Spotlight, or not yet indexed | Check Spotlight settings under Privacy and wait for indexing to finish |
| Extra files appear | You ran a text search, not a tag search | Pick the Tags suggestion so the token appears |
| Only some folders appear | The scope is the current folder | Switch the scope to This Mac |
| Results differ on an external drive | The volume is not indexed | Check indexing for that volume |
| Two similar tags both show | Tags with similar names, such as `Work` and `Work ` | Rename or merge them in tag management |
Exercises
Exercise
You click the tag Blue in the sidebar and the results include a document that does not carry the Blue tag but contains the word "blue" in its text. What most likely happened, and how do you fix it?
Show answer
You ran a plain text search for "blue" instead of a tag search, because you did not choose the Tags suggestion in the search field. Delete the text, type the name again and select the entry marked Tags, so the search uses a tag token that matches only kMDItemUserTags.
Exercise
Write an mdfind command that lists items under your Desktop that carry the tag Work. Then say how you would also require the tag Urgent.
Show answer
mdfind -onlyin ~/Desktop 'kMDItemUserTags == "Work"'. To require both tags, join two conditions with &&: mdfind -onlyin ~/Desktop 'kMDItemUserTags == "Work" && kMDItemUserTags == "Urgent"'.