Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Spotlight is a fast and powerful Macintosh find tool that's based on file metadata. We can access Spotlight from the command line too and I'm going to be honest with you. This isn't my first choice for finding files. Most times if I'm working in the Finder I need to find something based on metadata and typically going to go up here to the Spotlight menu in the upper right corner and type my query there. And most times if I'm working in Unix in the command line, I'm going to tend to use find and rep, but there are times when you really need Unix to have access to the metadata of the Spotlight keeps.
So let's see how we can use it from the command line. The command that we're going to using is mdfind. The md stands for metadata. Now the metadata for file contains much more information about the file than we normally have access to from the command line. As an example, let's say that you have an MP3 or a song that you've purchased on iTunes. Well that has a file name. It's probably the same name as the song and we can see that in the file system. We could search it for using find or grep, but it also has a lot more information associated with it. It has the artists name, the album name, the track number, the running time, the bit rate that it is encoded at.
All of that information is stored in the files metadata and we have the ability to access it by using Spotlight or by using mdfind. The way that we use it is just simply to tell mdfind what our query is. So mdfind and then new and then we hit Return and it does the exact same find as if we'd come up here and typed New into Spotlight. Now you could add more to your search the same ways as you can type other terms into the Spotlight window, so I reduced the number that I got here. But really whenever possible I really recommend very strongly that you use -onlyin option and then tell it where you want it to look and then just give it a regular path.
This will massively limit the amount of searching that it does. So it'll make it a faster search and it'll also give you less results that you don't want. Now if you really do want to search your entire hard drive, then by all means go ahead and leave it out, but most times you at least know roughly where you want to be looking. So you can go ahead and say ah this is in my documents folder, this is in my music folder, this is in my sites folder, and limit it that way. I also want to show you that you could include negatives in there. So for example, I got three files back I could say well I don't want anything that has join in it.
Well now left out the one joins.txt. It just gave me the other two files results back. So again it's just like if we typed in the Spotlight menu. The documentation actually says that it's a really good idea to use the interpret option passed as well to make sure that it really does interpreted exactly like you typed in there. Now my experience is that most times you don't need it. Most times that interpret option is overkill, but I just want to mention it just in case you're wondering why a certain search here isn't coming up. You may need to force it to strictly interpret the same with the spotlight query window would.
In addition to asking for a query string we can also use the name option and specify something that has exactly a file name that matches. So you see we got back something slightly different this time. Now the hits were based on the file name new and new. If you remember when we're using the regular find it also had a print O option that allowed it use null as a separator between each of the results. That made it really easy than the pass off to xargs and xargs was enable to use it reliably. Well, we have the same thing here and it's the -o option, so then we can type that into xargs and then in the xargs we would need to use the -o option there as well and then we can do something with it. Let's say we'll use our open command.
So now we'll pop off those open in text made for me. Now this is the simple usage of it and probably the way that you'll use it most of the time, but I do want to show you that you can go even deeper and we'll take a look at that in a next movie.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97244 Viewers
61 Video lessons · 84522 Viewers
71 Video lessons · 68728 Viewers
56 Video lessons · 101214 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.