Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Now we've covered most of the fundamentals of working with files and directories, we want us to see how we can go about searching files and directories to find certain files. The Finder offers a really powerful search tool already. It's called Spotlight. You can access it from the upper right-hand corner of the Finder or most Finder windows also have a field that where you can type in what you are looking for and it will help you find all the files that match that criteria and it's very powerful and it's pretty fast. However, we also need to learn to do it the Unix way. First of all, you may be on another Unix system at some point. Maybe you're even logged in remotely to a Unix web server and want to search for things there.
Well, Spotlight won't do you any good on that remote server and also sometimes it will be useful to be able to just do it from Unix without switching out and in some cases, it will actually be faster. You will be able to find exactly the results you want by doing it this way. So the way we'll execute a Unix search is using the Find command. Find, space, and the first argument is the path that we want to search in so we can limit the search results to certain folders, and then the expression and the expression could be a lot of different things, a lot of complexity can go there, but that's going to tell it what it should try and find.
So to just give you a very simple example, we could have find inside the user Documents folder anything that had name "someimage.jpg". Those quotes are optional. You don't have to have them there. Notice that name, even though it has the dash in front of it, is not an option in the typical sense of being a command option. It is part of the expression. name and someimage are the expression that we are using here. That's why the option comes after the first argument, not in the traditional place where options come between the command and the arguments.
It is an argument itself. It is not an option to find. Find can have some options to it. You can use the man pages to look those up. Now that will return anything that's in Documents that has exactly the name someimage.jpg. Well, that's great if we know exactly the name of the file we are looking for, but most times we don't. Most times we are looking for something that's a little fuzzier than that. Maybe we are looking for everything that has the word image in it or everything that ends in .jpg. When we were to execute those kinds of finds, we need to employ wildcard characters. So we'll use wildcard characters to hold the place of where we want to say well, anything could potentially go here.
So if we use an asterisk that means that zero or more characters can be represented by that. It's often called the glob. If we use the question mark, it represents any one character. It can hold that place and with square brackets any of the characters in the brackets could have that position. If we try these out, I think you'll get the hang of the wildcards pretty quick. For this movie, I am actually not inside my Unix files. I am just inside the root of my User folder so you can use cd ~ to get in the same place. So from here, most of you should have a Sites folder and inside there should be something called index.html. ls ~/Sites/.
You can see I have something called index.html. That's a default web page that Apple gives you on your Mac. If you don't have it, then you can pick any other file. I just wanted to pick something that I thought everyone would probably have. So if we find inside our Sites folder something with the name index.html, it comes back and it says "Ah, here is what I found for you." If we pick something else, let's say, index2 .html, it says "Nope, didn't find anything." Okay, so you see the difference in behavior whether it finds something or not. It lists everything that it finds. For the wildcards, what we do is we would just say all right, you know what, I am looking for not just anything that is index, but anything that has index dot and four characters after it.
I don't care what four they are, has to be four, but those four can be anything. That's what I mean by a wildcard. It's sort of like a joker being wild in the card game. So it still returns the same result. Let's take away one of those. You'll see that comes back and it says no. If we don't know how many characters, you might have two, might have three, might have four, well then we would use the glob, that asterisk, and that says yeah, I recognize that. So let's say for example we wanted everything in that folder that ends in .html. You might have a circumstance where you have several index files.
We might have index and we might have index1, index2, index3 and we want to get all of them, well, then we would say index(123) and now it would return anything to us that had index 1, 2 or 3. Now obviously we don't have those files, so it didn't return anything but I wanted to show you how those square brackets work. It basically says this character position can contain any of the things that's inside the set, 1, 2, or 3. So that's how the wildcards work. Let's try a couple more examples. Let's do find anything inside our user directory. I'll just clear the screen so you can see this little better. And where the name ends in .plist.
That's the file extension that Apple gives to a lot of its preferences. So that will be anything in my User preferences folder that is an application preference or something like that and a whole lot of these scroll bar and now you see I have got some for Safari, I have got some for QuickTime, and so on. So that's sort of a classic use case for how you would do this. Now so far, examples have just included showing name, but you can also read the man pages and see that you can put in path, the last modified time, the size of the file, things that match certain regular expressions.
You can do all of that and you can read the man pages to find out exactly how to do what you are looking for. Find can be pretty complex. Let me show you one other thing though that I think is pretty cool, which is that we can also provide some modifier options to the expression. So for example, I had find everything that was a plist, but let's say I don't want to have the ones that were QuickTime. There were whole lot of those that were QuickTime. Find everything and not path and then with quotes or without we can do QuickTime. Notice I have the asterisk at the beginning and at the end.
There may be stuff before it, there maybe stuff after it. I don't care. I am looking for anything whose path contains this string, QuickTime. I don't want to see those. So you see how you use and, not, and then path. Path is the same as name, but the operators and, not, and or are all things that you could use. So let's try that. Now you'll see I get another list slightly different. Now we can say okay, but what about everything that's not in our Preferences file? Let's say -and -not -path * Preferences. I get a narrower list, dropped everything up that had preferences.
So you can see how it starts to become very powerful. You can start to hone in on exactly what you're looking for. As I said, find is a really powerful tool. It goes much deeper than this. I just want to make sure that I gave you a good introduction to it so that it at least gets you started with the basics.
Get unlimited access to all courses for just $25/month.Become a member
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.