Using Find In the Terminal to Locate Files

An alternative to using Spotlight or the Finder to search for files is to use the Find command in the Terminal. You can search for files by name and refund the search by location, time and other factors. While not useful for everyday file searches, this could be used to search through large libraries of files for particular results.
Video Transcript / Captions
Closed captioning for this video is available on YouTube: Using Find In the Terminal to Locate Files.

So most of the time when you want to search your Mac for a file you just do it in the Finder window. Just Command F and you would be doing a Spotlight search for the file. It's quick and it's easy. You can also use the Spotlight menu, Command space, and search there as well. But if you want to do it in a more techy kind of way in the Terminal you can use the Find command. Now the Find command doesn't use the Spotlight index at all. It's not dependent on that big index that your Mac is always updating of all your files. It will actually look through all the directories, look at each file, and see what matches and return the results.

So to use the Find command you just go into Terminal and type find. The next thing you're going to type is the path to the Directory you want to search. So you could do, for instance, slash and that's it. This would search your entire hard drive. Now this is a really bad idea because it's going to look in your system folder and your library folder and they are filled with tons of files. It's going to find all sorts of matches you didn't expect and it's going to fill up the window will all sorts of results you don't want. So you probably want to search something like, maybe, your Home folder. You can do that with a tilde, that's the Shift and the key above the tab on American keyboards, and slash. That searches the Home directory. You can also type the entire thing out, like Users/macmost, that's your Home directory, and then you can do documents.

You can search the current directory very easily by using a dot. That will look wherever you currently are. Now if you want to specify where you are you can type the entire path. But like, for instance, in this project folder here I can simply use the command cd, change directory, and can drag the icon here into it and hit Return. Now I can check pwd to see where I am and you can see I'm there. So now when I do a find with a dot it will be looking in there. You can also do the same thing with any folder. So I could have, for instance, said cd and then dragged the project A folder into there and done the same thing like that.

So now that I know I'm checking in here let's look at what's in here. There's a Text.rtf. There's another file called Another Test.rtf, a New Test.rtf and all of that. So we would expect that if we were to search for Test.rtf we would find maybe that one file that matches exactly, maybe three files that match somewhat. So let's go back to the find command. find . for the current directory and then do dash name, this says I want to search by name, Test.rtf. It will return that one file. It'll say it finds it in that current directory dot and slash Test.rtf.

Now if I were to have specified test with a lower case t it's not going to return anything at all. That's because it is case sensitive. But you can make it case insensitive by hitting i instead of just name, so iname, a case insensitive name, and now it will find it. By the way to get these commands back quickly I just do Up arrow and it goes back to the previous command and then I can edit it. Now what if I wanted all three of those files. I can put an asterisk there, which is a wild card meaning anything followed by test.rtf. Now you can see I get those three files there. But also if I didn't know they were rtf's I could do anything *, , followed by test* and it will return those same three files.

Now I can also look for files by modification date or creation date. So let's go back to the previous command, a new command so start with a dash, and say mtime, modify time, and I can now use a minus and then a time. Now this minus is an actual minus not a dash like before mtime and before iname. I can do minus and then I can say things like 1h for one hour or 2h for two hours or 5d for five days or a combination like 5d6h, five days six hours. When I do that search I'm only going to get the one file there because that's the only one that is newer than 5 days 6 hours from right now.

Now I could also, if I want the results to be in a different way, like say I get some big results. Like let's say for instance that I'm going to search not for files that were modified here and not for just rtf's but for anything that has test in it and I'm going to look in my Home directory, so it's going to include my library folder and everything. I'm going to get a ton of results and it's going to scroll up off the page. I can go to that same command and use the pipe character, which is Shift and the key above return, to pipe it to more. More is a paging method for the Terminal window. So now I get the first page and I can hit Space for the second page. I can hit Return to just go one line. I can hit Up arrow to go up and down in it and Q to quit out of that paging kind of method. That's kind of nice.

I can also go, instead of pipe, I can do a greater than > which is then kind of, you know, a send to command and give it like searchresults.txt and now it looks like I get nothing but if I look in the Finder here I can see I have a searchresults.txt file. I can open that up in TextEdit and the entire thing was sent to a TextEdit file. I can look through that as well.

Let me clear the window here and show you one last thing. What if you want to search for something that's got spaces in it. I do find . iname and remember I had that Another Test file. So I search for Another Test. I'm going to get an error there because it interprets the Another as what it's looking for in the name and then Test is all by itself and it doesn't understand what that is. So I can do two things. I can put quotes around this and I'll include the wild card * in there so it finds it. There it goes. I can also, without quotes, I can just put a backslash, a backwards slash which is the key above Return on American keyboards, just before every space. That means basically take the next character literally. Don't, you know, interpret it as part of the command. That will work just like a quote.

Now one last thing I want to show you. There's tons more functionality in here if you really want to dig into it. Just like with any other command in the Terminal, type man for manual and then type the command. So man find and you get a ton of information about every single option you can possibly imagine inside of the Find command. You can page through this using the space bar and look through it and see all the different options. If you're not a coder it's a little hard to understand all of them but sometimes you can do searches for something. So if you don't understand what path or perm or print or prune mean you can do searches online for terminal command find and prune and see what comes up and maybe there's somebody that has explained it. But you can see different things here like, for instance, I can look for files by size so that could be kind of handy to look for large files. Things like that. So there's a lot of stuff in here that you can use to refine the search.

Comments: 3 Responses to “Using Find In the Terminal to Locate Files”

    1 year ago

    Basically the same as the old Dos Command prompts…?

    S Keene
    1 year ago

    Hello, surely one can do the same by just using cmd-F when in Finder?

    1 year ago

    Keene: If you look in the man pages, there are a lot of options that aren’t easy to do in the Finder. But for typical file searches for most users, yes, just use the Finder.

Comments Closed.