How To Easily Search For Files Using AND, OR and NOT

When you search for files you can use boolean operators like AND, OR and NOT. The hard way is to use the convoluted search interface. But you can also just type out your search request to get it done quickly and simply.
You can also watch this video at YouTube.
Watch more videos about related subjects: Finder (314 videos).

Video Transcript

Hi, this is Gary with MacMost.com. Let me show you how you can easily use terms like AND, OR, and NOT when searching in the Finder. 
MacMost is brought to you thanks to a great group of more than 1000 supporters. Go to MacMost.com/patreon. There you can read more about the Patreon Campaign. Join us and get exclusive content and course discounts.
Now in the Finder let's say you want to do a Search for jpeg files that have the word Test in them. You can click on the Search button here at the top right or use Command F and then search for Test and then I have the choices, Name Contains Test or Content Contains Test. I can select Name Contains Test and get results that have test somewhere in the name.
Now instead I could have just typed Name colon and then Test with no space there and then a Return and gets me the same thing. Anything with that phrase in the name. Let's say I wanted to narrow this down to only be jpeg files. I could see a few of them here. Here's one. Here's another one. Notice it is a different extension for this one with an e in the extension. So I want to keep that in mind when I'm searching. Now the straight forward way to do this is to click the Plus button next to Save here in the Search line in the window. Then I can go and select something else like Name Matches and put dot jpg and then I get file names that match this. But I'm missing the jpeg. If I change it to that I get those but now I'm missing the other kind too. Plus this is kind of convoluted. You're doing the first part of the search up here in the Search box and then you're adding additional criteria in different lines here. I can continue to add more things here. Like I can say Name Matches and then .jpg and now I see I get nothing because there are no files in all three conditions having text having dot jpeg in the title and dot jpg in the title. So how would I do this?
Well, it gets even more convoluted than that because what I would do is I would remove this one, hold the Option key down and the + changes to three dots. Click on that and then I would remove this one here and then say Any of The Following are True, which is equivalent to OR and say Name Matches .jpg and then another one Name Matches .jpeg. Now I finally get the results I want matching everything. But you could see how convoluted this was to do. There's an easier way.
If we just go back to the Search box here we could do it all by typing. Now if I were to type Name colon Test then I get everything with test in it. You would think that I would type And Name colon .jpg that I would get things that match both of those. But I don't. The reason is because you can't just use And. You have to use AND in all uppercase with capital letters. Then the Search box will recognize that as the boolean operator that it is and you get things that match both of those. Now I can do OR as well. So I can do Name:.jpeg but I get really weird results now because I get things that don't have test in them. What this is actually doing is saying, anything with the name test and jpg in there OR anything that has jpeg in there which you can see I get some of the results there.
So you can use parentheses in here to make it clearer what you want. So I'll put one after this and one after that and now that's my complete boolean structure there to search for everything with the name test and either of these two spellings of the jpeg extension. You can see how much straight forward this is to having to do that by building it in the Finder by different lines of search criteria. The key to remember here is using capital letters for AND and capital letters for OR.
I can do the same thing here with NOT. So for instance if I were to change AND to NOT then my results would be everything with test in the name but not including anything with jpg or jpeg in the title. You can see that is what I get here. You can use parentheses to make this as complex as you want all on one line. If you want to save them you could Save it as a Saved Search, also known as a Smart Folder. You could simply Copy and Paste it as a piece of text somewhere to reuse at a later time so you don't have to type it all.
By the way you could also search for things like Kind and then put a standard term like Image there and it will give you all the images. You could do Dates as well. But you don't want to put date. You want to put Created or Modified and then colon and then format your date like this and then you'll get results. If you want things that are from before that date put a less than in front of it. If you want things that are after that date you put a greater than in front of it. Of course you can use AND and then put another date there to get a range.
So I hope you found this tip useful. Thanks for watching.

Comments: 13 Comments

    Neil
    2 years ago

    Good tip.

    This doesn’t seem to work in the search field in Photos.

    Searching for two or more “Family_Bob Family_Sue” keywords uses ‘and’ operation. Doesn’t seem to be an ‘or’ operation available to get two groups of photos combined.

    Smart albums are clumsy if you just want something temporary.

    Is there a better way?

    2 years ago

    Neil: This video is for searching in the Finder, not in Photos. Using a Smart Album would be the way to go in Photos.

    Chris in CT
    2 years ago

    Very helpful!
    Where would one find a list/dictionary of search terms beyond Name, Kind, Created, etc.?

    2 years ago

    Chris: I don't know of any comprehensive list, but Apple shows some here: https://support.apple.com/guide/mac-help/narrow-search-results-mh15155/mac
    You can also sometimes get more by creating a Smart Folder, then opening that Smart Folder's file in a text editor to examine how the search terms are represented in the text.

    Jasper
    2 years ago

    Hi Gary,
    1. When I open a SavedSearch, I get a long xml file, which includes, e.g., 'kMDItemUserTags = "Flock 🐑"cd) Query. What would one do with this? It's not the same format as the Finder search tokens...although pasting it in retrieves the SavedSearch file.

    2. And I'm I right in thinking that there's no Finder token to restrict the search to a specific file? Something like 'folder:Documents'

    TIA, Jasper

    2 years ago

    You use it in the same way you use the tokens in my examples. So instead of name:myname you would use kMDItemUserTags:mytag. It is just the "kMDItemUserTags" you are looking for.
    You don't include the folder (path) in the search part, you do that by starting in that folder. See at the top in my example it shows "This Mac" and "Documents" and Documents is chosen because that is where I was when I started the search.

    Bernard
    2 years ago

    When I type the search string “name:test” into the search field the system internally uses the matches option. How can I change it to the contains option?
    In the “savedSearch” XMLs it looks the following:
    1) matches: “(kMDItemDisplayName = "test*"cdw)"
    2) contains: “(kMDItemDisplayName = "*test*”cd)”
    Both options “kMDItemDisplayName:test*" or “...Name:*test*” don’t work in the Finder search field. Only without asterisks I get results but then I won't find compound words like “retest”.

    2 years ago

    Use command+option+f and then the fields and menus below to do complex searches like that instead

    Bernard
    2 years ago

    Sure, but that's very cumbersome to select the same items from two drop-down lists over and over again when I principally want to use the contains option. I wonder why the option in the standard search dialog was renamed to “Name Contains” while “matches” is still used in the background? In older OS X versions the option was correctly called “Name matches”.
    https://discussions.apple.com/content/attachment/c5f27137-a5d9-4f2d-947b-db07a6de2ea6

    Terry
    2 years ago

    When I search in finder I will only get results if I select my This Mac. If I try to search within any of the folders I get 0 results. The file does exist in the searched folder and will only be found if I choose This Mac. A full Spotlight search will also find the file.

    2 years ago

    Terry: Not sure why that would be the case. Maybe work with Apple Support to figure out what could be going on.

    Rick
    2 years ago

    I want to exclude a particular file type from seeing it in my "Godot projects" directory and its subdirectories. I can simply type "NOT:import" in the filter field. However, I want to keep this filter turned on as long as I'm working and regardless of in which directory I'm in. But as soon as I enter into any subdirectory of the "project" folder, the search bar is reset. Is there a way to keep it fixed? And Saved Search saves the results for the current folder only, so that's not much help eithe

    2 years ago

    Rick: No, you have to tell it each time that you ant it excluded.

Comments are closed for this post.