Sub topic 6
Building a Finder search with criteria
This chapter covers how Finder turns a typed query into a structured search with several conditions. A smart folder is a saved version of this search, so the rest of the tutorial builds on it.
It assumes you can navigate Finder windows and folders, know common file types, and are comfortable with menus and keyboard shortcuts. You should also have read the earlier chapters on tags and on how Spotlight metadata works.
The search model: scope plus criteria
Every Finder search has two parts:
- Scope is where Finder looks. The options are This Mac, the current folder, or in some cases Shared.
- Criteria are the conditions an item must meet. Each is a metadata attribute, a comparison, and a value, such as Kind is PDF.
Finder does not scan folders when you search. It sends the scope and criteria to the Spotlight index and shows what comes back. The attributes you pick in the criteria rows are the same metadata fields covered in the Spotlight chapter.
Starting a search
There are two common ways to start:
- Press Command-F in a Finder window. This focuses the search field and, in recent macOS versions, shows the criteria bar.
- Click the search field in the toolbar and start typing.
As you type, Finder suggests search tokens in a menu below the field. Examples are Name Matches, Contents Matches, Kind, and tag names. Choosing a suggestion converts your text into a token with a defined meaning. Plain text with no token searches several attributes at once, such as name and contents, which is why a bare word often returns more than you expect.
A tag token is the quickest way to find tagged items. If you type the name of a tag, such as Red, Finder offers a tag suggestion. Choosing it limits results to items carrying that tag, instead of items that merely contain the word "red".
Choosing the scope
Below the search field is a scope bar. It shows This Mac and the name of the folder you were in. Click one to switch. Two details matter:
- Searching a folder includes its subfolders.
- The default scope is a setting. In Finder > Settings > Advanced, the option "When performing a search" lets you choose Search This Mac, Search the Current Folder, or Use the Previous Search Scope.
A scope that differs from what you assumed is a very common reason a search seems to miss files. Check the scope bar first.
Adding criteria rows
When a search is active, a plus button (+) appears at the right of the criteria bar. Click it to add a row. Each row has three parts:
| Part | What it does | Example |
|---|---|---|
| Attribute (left menu) | Chooses the metadata field | Kind |
| Operator (middle menu) | Chooses the comparison | is, is not, contains |
| Value (right side) | The text, option, or date to match |
The attribute menu starts with a short list: Kind, Last opened date, Last modified date, Created date, Name, and Contents. Choose Other… at the bottom to open the full list of Spotlight attributes. Each attribute has an "In Menu" checkbox. Ticking it adds that attribute to the short list, which is useful for fields you use often, such as Authors, Tags, or File Size.
The operator and value controls change with the attribute type:
- Text attributes offer contains, begins with, ends with, and is.
- Date attributes offer is, is before, is after, or is within the last, and a relative range such as the last 7 days.
- Size attributes offer is greater than and is less than, with a unit menu.
- Kind uses a fixed list: Document, Image, PDF, Folder, and so on.
Remove a row with the minus button (−) at its right.
All, any, and none
By default, a stack of rows works as AND. An item must satisfy every row. If you find one row too strict, the rows are probably combining as AND when you meant OR.
To change this, hold the Option key. The plus button turns into an ellipsis (…). Clicking it adds a compound row with a menu of any, all, or none. Nested rows go under it. For example, "Kind is PDF" AND (any of: Name contains invoice, Name contains receipt) finds PDFs whose name has either word.
Worked example: recent PDFs
Suppose you want PDFs you changed this week, anywhere in Documents.
- Open the Documents folder and press Command-F.
- In the scope bar, click Documents.
- Click + to add a row. Set it to Kind, is, PDF.
- Click + again. Choose Last modified date, then "within the last", then 7 days.
Finder shows only items that match both rows. Nothing has moved, and the results will change as files are edited or added.
Switch to List view and add the Date Modified and Kind columns. This lets you check at a glance that the criteria did what you meant.
The same search in Terminal
The criteria rows map onto Spotlight query expressions. You can run the same query with mdfind, which is a good way to confirm what Finder is really asking for.
mdfind -onlyin ~/Documents 'kMDItemKind == "PDF Document" && kMDItemFSContentChangeDate >= $time.today(-7)'Here -onlyin plays the role of the scope. kMDItemKind is the Kind attribute. kMDItemFSContentChangeDate is the content modification date. && is AND, and || is OR. Note two things:
- The Kind value is the display string,
PDF Document, and it follows your system language. On a Mac set to another language, the string differs. - Values you type in the Finder interface, such as "PDF", are friendlier labels that Finder converts for you.
For name matching, add modifiers after the string. The modifier c ignores case and d ignores diacritics:
mdfind -onlyin ~/Documents 'kMDItemDisplayName == "*invoice*"cd'You can also list attribute names for a known file with mdls. This is how you discover which attribute to use for a criterion:
mdls ~/Documents/report.pdfThe output lists every attribute Spotlight has stored for that file, such as kMDItemKind, kMDItemFSSize, and kMDItemUserTags.
Combining tags with criteria
A tag is just another attribute, so it can appear in a criteria row. A common pattern is a tag row combined with a date or kind row. For example, Tags is Red, Kind is Document, Last opened date within the last 30 days gives you active red-tagged documents. Because the tag row is just one condition, the same search can be made broader or narrower by editing rows rather than by retagging files.
Common problems
These are the errors people most often hit with criteria searches:
- Wrong scope. The search covers only the current folder, or only This Mac when the files are on a network volume. Check the scope bar.
- AND instead of OR. Rows combine as AND by default. Use the Option-key compound row for OR.
- Contents versus Name. A bare word searches contents too, which returns unrelated files. Pick the Name Matches token to restrict it.
- Missing results from unindexed locations. Files in folders excluded from Spotlight, or on volumes that are not indexed, never match. The troubleshooting chapter covers re-indexing.
- System and hidden files. These are excluded unless you add a row for System files, found under Other….
- Date wording. "Is within the last 7 days" is relative to now. "Is" a specific date matches that single day only.
Reading the search before you save it
Before saving a search as a smart folder (the next chapter), test it as a live search:
- Check the scope.
- Read each row aloud as a sentence: "Kind is PDF, and last modified within the last 7 days."
- Skim the results for items that should not be there. They show where a row is too loose.
- Look for items you expected that are missing. They show where a row is too strict, or where metadata is absent.
Exercise
You want to find files in your Documents folder whose name contains "budget" or "forecast", and that are spreadsheets modified in the last 30 days. The rows you added are Name contains budget, Name contains forecast, Kind is Spreadsheet, and Last modified date within the last 30 days, but Finder returns nothing. Why, and how do you fix it?
Show answer
By default the rows combine as AND, so a file would need "budget" and "forecast" in its name. Hold Option and click the ellipsis button to add a compound row set to "any". Put the two Name rows inside it. Keep the Kind and date rows outside it so they still apply as AND.
Exercise
Which Terminal command runs the Spotlight query behind "Kind is PDF" within the folder ~/Documents, and what does the -onlyin option correspond to in the Finder window?
Show answer
mdfind -onlyin ~/Documents 'kMDItemKind == "PDF Document"'. The -onlyin option corresponds to the search scope, which is the current folder in the scope bar.