Search Using Wildcards On a Mac

While it may not seem like you can use wildcards in a Spotlight search on a Mac, you can use a variety of options to get the same result with a normal search, and then regular wildcard searches when using Raw Query as your search type.
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 search for files on your Mac with Wildcards.
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.
 It's fairly simple to search for files on your Mac. But I'm often asked how you can also use Wildcards to search for part of a file name or part of the contents of a file. In most cases you don't need to do anything special to search for wildcards. Here I am in a folder on my Mac and I'm going to use Command F for Find. I'm not going to use the search box up here because I want to go right to the more advanced features for what we'll do later on. So I'm going to use Command F and it will open up a new tab in my case. Then I could see I'm going to search for Kind as Any. I'm going to change that. So I can search for File Name and then I'm going to type part of the file name. Now if you look at where we're searching we're not searching the entire Mac. Just inside the folder where I started. So if I look in this folder here I can see a variety of different files. Let's say I want to find this file right here. I can start searching for the file name and by just typing a few letters I can find it. So as you can see it's already using one of the most important parts of a Wildcard search. It's not requiring me to type the entire file name, every single letter. I can type just part of it and it will come up with the result. 
Now if I were to type even less you can see it gives me more. There are two files here that start with the letter w. Now I can change matches to a few other things. Begins With and Ends With are pretty obvious. Let's do Begins With and we see that this works in this case. I get these two that have w in it. If I were to search for some other letter you could see it doesn't give me anything. Now what if I were to switch to is. Well, then it's going to ask for an exact match. You can see it still doesn't find it. But if I type the entire file name it will find it. Notice its case insensitive. So it finds it although there's a capital W in the name but I'm only searching for a lower case w. 
Now what's the difference between Matches and Contains. Well matches is kind of the default spotlight search. It's going to look by word and try to match the beginning of each word. So I could start typing the beginning of the word. But if I were to type something inside the word it won't find it. However, contains will do just that. As I said this works per word. So let's go here and add a second word to this file name here. So now if I were to search for the beginning of the second word you can see it finds it. But it won't just find anything in here because I'm using matches. It's going to look at the beginning of the word. So if I look for something inside the word like that or like that it doesn't find it. Only if it Contains will it find something that actually is at the beginning of the word. 
So what if you want to use real Wildcards to do more complex searches and maybe even do things like search by case. Well in order to do that you need to speak directly in the language of a Spotlight Search. All of this stuff here, filling these out, that's basically creating a piece of code that's then implementing the search. You can skip this and go directly to create that code. The key to do that is to use a special type of search. I'm going to go to Other here and search for Raw and there's something called Raw Query. I'm going to select it but I'm not just going to select it. I'm also going to Add it to the Menu so it's easy to get to in the future. So now it says Raw Query and it's actually one of the default items in this list. So Ray Query gives me this space here and I could type what I want. Now if I start searching for something like that it's not going to come up with anything. It's not a search term here. It's an actual query. So you have to specify the property you want to search and what you want to search for. In this case we want to search for the file name. 
That is in the property called kMDitemDisplayName. Then I need to do =. Technically I should do two == but Spotlight will work with either one. Now I can put in quotes, or I don't necessarily need to use the quotes but I will anyway, and I can do something like, for instance, wombat, and it's not going to find it. It's looking for the wildcard which is the traditional computer wildcard character the asterisk. But you can see it still didn't find it cause now it's case sensitive. I need to specify capital W. That's what I'm looking for and there you have it. You can actually make it case insensitive if you want by after the quote put the letter c for case insensitive. Usually you add the letter d as well, d makes it accent insensitive. So, for instance, I could put an A with the accent mark there if I wanted to for some reason and it will still find it. If I take away the d it won't. 
Now I can do all sorts of different things like, for instance, I could search for bat like that. So it's looking for any number of letters followed by bat followed by any number of letters. If I were to take this away from the beginning it wouldn't match it because it's looking for something starting with bat. Now if I did file like that it doesn't match it. But remember we have Sample File but it's not looking by word anymore. It's actually looking for the entire name. So it will find it if I say any letters before then the letters file and then any letters after. Then c and d to make it case and accent insensitive. 
So an example of how you might want to use this is, notice here, I've got some of these images with capital JPG after it. This can happen sometimes. So what if you just want to change these two to be lower case and maybe there aren't three but three hundred of them. Well if you did a search for an *.jpg" I'll get the one that's lower case. But if I search for capital JPG I'll get the two that are upper case. It will ignore the ones that are lower case. Then I can click in here, select All, go to File, Rename and do a batch rename of all of those changing this to that. 
So that's the basics. But in addition to using something like c and d afterwards to make it case insensitive, there's another one called w. What w will do is it will look word for word. So we have that file called Sample Files. So if I search for f it's not going to find it. There's nothing that starts with the letter f and then has letters after it. But if I type w it will find it because now it's going to compare that to every word of the title. 
Now before I show you some other ways to use Wildcards besides with file names I want to show you how to combine several in one Raw Query because you can certainly add another thing here and add another Raw Query and do it that way. But you can also use things like parentheses and you can use and and or to look for things. So I can do an or, which in this case is two straight up and down bars. It's the key above the Return key on American keyboards and I could copy and paste this. Unfortunately this is very little field here but if I paste it a second time you actually have two of these now. I can use the arrow keys to go back and forth. I'm going to take the second one and change this to an s. So now what I'm saying here is, if the name starts with w or it starts with s then show it. So I get all the w's and all the s's. If I change this to two ampersands, like that, it's an and. So I'm looking for files that start with w and also start with s which, of course, is impossible. A file name could only start with one thing. But instead if I put a wildcard here like * t.txt I'm going to get files that start with w and end with t.txt which only this one matches. You can also use not. Put an exclamation point before any of these things in parentheses and it changes to a not. So it's hard to see here but there is actually the exclamation point. So anything that starts with w and anything that does not end with t.txt and that's how you get this result. 
Okay. So names aren't the only thing that you can use wildcards for. For instance you can use them for Tags as well. Instead of using this for a name I can do UserTags like that. What it is going to look for now is any Tags that starts with w. 
Now, of course, one of the main benefits of a system like Spotlight is you can do much more than file name searching because you can search for the actual content of the file. That is represented by kMDitemTextContent. So this is going to look for any content that has in it a word that starts with the letter b. I can look for something like fo and sure enough these have a word like that. Now if I search for something like jumps over these, which all have that word in them, will show up. But what if I were to take away that. Well that doesn't work because there is nothing with jump over. I can put an asterisk there and it will find it. Of course I can remove the c here and it will be case sensitive. So I have a capital J. It's not going to find those. But what if one of these files didn't have jumps and over right after each other, like that. Now I can see it doesn't find it. There is no jumps over in the watermelon.txt because it jumps nearly over. But if I put an asterisk inbetween it then it is going to look for jumps followed by any word followed by the word over. 
So there are a lot of useful things like that that you can do to narrow down your searches using wildcards and Raw Query. However there's also a lot you can do using just regular search using either the Matches Search to look for words that start with some letters or using Contains to find any group of letters in the title or the contents of the document. Hope you found this useful. Thanks for watching. 

Comments: 14 Comments

    SteveH
    3 years ago

    Gary, I'm forever amazed at the depth of knowledge you demonstrate on a daily basis. Where you discover all these life-saving goodies is beyond me. The only way to make this wildcard subject even better is; perhaps you'd be willing to create a cheat sheet.

    Joe Grillo
    3 years ago

    I agree with SteveH's comments. I usually learn a lot with your vignettes and this was one of the better ones for me. One thing I was really looking for was the next level search ie how do I search a pdf for a word.
    Good job - Thank you

    3 years ago

    Joe: In general, just search for the word. The most basic search in the Finder will look for the name and contents. So if the a word appears in a PDF, it should show up. Of course a PDF could contain images with words printed on them instead of actual text, so it may look like it contains that word, but in fact it only contains an image with the word on it. Or, sometimes PDFs can be locked or protected so they can't be indexed.

    Senor Wheel
    3 years ago

    Searching in the Finder is super easy. How do you list certain files in a directory in Terminal using wildcards?

    3 years ago

    Senor: You could do that with something like this, which would show all files ending with .txt

    ls *.txt
    Corky
    3 years ago

    How do I find non-blank fields?
    Specifically, I want to find all cards in Contacts that have text in the Notes field.
    The Search for "is set" option does not work. Once a field "is set" to any value the field always has a "is set" status even if all data in the field is deleted.

    3 years ago

    Corky: Not sure if that is possible. You may just have to flip (arrow key) down the list and see which notes have text in them. Or, export to Numbers and look there in the Notes column.

    JC193
    2 years ago

    Hi Gary - thank you for your videos.

    I have done raw queries using wildcards, but also wish to search for files modified within a specific date range. I can see kMD... ideas, but I cannot find the syntax to actually type in a date.

    Can you post a link or advise how to search for files modified within last 30 or 100 days of today, and for files modified between say 18 January 2020 and 17 February 2020 for example please?

    Many thanks,
    J

    2 years ago

    JC193: You would need to use things like kMDItemFSContentChangeDate and kMDItemFSContentChangeDate, but it is difficult to enter those values. It is much easier if you just add normal search criteria for the dates in the Finder window. So use the wildcard search, and also add Date Modified once or twice to narrow the dates.

    JC193
    2 years ago

    Hi Gary,
    Thank you for your reply. I was hoping to avoid 'building a search' in Finder as this exercise is something I repeat every quarter and annually for a certain set of files on my computer. If I could save the syntax so that I could cut and paste it into future raw queries, rather than having to start from scratch each time it would be quicker, but I haven't found an obvious way to do it yet.
    However, thanks to your videos etc I am now able to get the info I need - many thanks. J

    2 years ago

    JC193: Then use a Saved Search (AKA Smart Folder). https://macmost.com/creating-convenient-saved-searches-for-your-mac-finder-sidebar.html You can easily edit it every quarter to change the dates.

    Christopher Feldmann
    2 years ago

    I have a template project folder I duplicate for each of my video projects. All my exports go to an "OUTPUT" folder for that specific project. I want to look at all the video files in all the OUTPUT folders in a directory and use that search to quickly see all the old versions that can now be deleted. Is there a kDM lookup rule for file paths? is there a list where I can find the different kDM identifiers?

    Christopher Feldmann
    2 years ago

    I'm so sorry - please replace any use of KDM above request with "kMD"- goodness

    2 years ago

    Christopher: No way to use path in a search like that. Join the long line of people frustrated by that over the decades.

Comments are closed for this post.