Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
What if we want to grep from multiple files or search inside directories? There are a couple of ways to do it. One is we could just say well, grep for Apple inside and then provide the file path, a directory in this case. So let's just say we're going to use the current path. The dot represents the current path. We could just as easily type Users/Kevin /unix_files, same thing. It comes back and says I didn't find anything. It's because we've given it a directory. If we want it to do it in this way then we need to put the -R option in front of it for recursive.
Recursively look inside here, meaning check all of the files that are in this directory and then go into the directories below that and look inside those and so on. The same option that we used with copy, move, and remove earlier. So grep recursively for apple inside this directory. So whenever we use a directory, we want to use -R just like we did when we were doing copying. So now it tells me not only the matches it made but the file where it occurs. We could also provide the -n option after that and now it gives me the line numbers in each of those files.
Notice that it does this by default. It says ah, now because I'm searching multiple files I'm not just going to give you the results. I'm going to tell where I found them as well. You can suppress that. Let's just erase all of this and I'll just use the dot from now on just because it's a little bit shorter and instead of the n option let's use the h option. h option will suppress the file name and still show you just the line itself or if you wanted the other way around and you wanted just the file name, you can use the l option. Now it tells me ah, I've found apple in three files.
Fruit, sorted fruit, and unique sorted fruit. That can be really handy. All right you could say all right, here are the relevant files I found them, and now we could presently pipe those results into something else, right. Maybe we want to now concatenate these three files. After we find them then we could potentially concatenate them together. If we want to do the opposite of that and find the files that don't have a match it's the capital L. So that returns all files that did not match. This is very similar to the -v option that we did earlier but that returned the lines that didn't match. This is returning whole files that don't match.
So it's similar but not the same. Now this recursive approach using this R option is searching all files in all directories. Don't forget that bash also lets us use wildcards to trend pick more specific files and that might speed up our searches. For example, just using ls we can do ls*.txt. And we can see just the text files that are in the current folder. Or even more specific, we can say all right let's check just the fruit files. All right, so now I'm saying just the fruit files. That wildcard lets us do that. That's handy with grep because now for example we can say we'll grep for apple inside just the files that end in fruit.txt. It's the same results that we got but it's just searching these three files instead of searching every file and directory.
If you have a lot of files and directories this can be a lot faster. But the main point is don't just choose recursive by default. Think about what you're really trying to accomplish and try and find the best way to do that. Now these are working with files and directories. We also can change our standard input. We can use piped input. We could for example do cat fruit.txt and pipe that into grep for Apple. That's still just working with a file though, right. It's just as easy to do it the other way. Well it's really more useful as with something like ps aux. You remember that that returns a list of all of our currently running processes. Well, we can grep that list. Let's grep for Terminal.
There is my entry for Terminal. I can now see what its ID number is. I can see how long it's been running. I can see how much memory it's taking up, all of that information just by grepping for only the line that contains Terminal. Or we could say all right, show me everything that has applications in it. So now all of my Mac applications will show up and I can see just those and not have to sort to that whole list of running processes. Another place it is useful with the history. So for example we'd say history and let's grep that for Unix files.
Every time that I've made reference to the Unix files directory, we also could then use this as shortcuts to be able to run those commands. We saw how to do that when working with our history. Let's try history and let's pipe that through grep nano and then pipe that into less. Now I only have a few but if you have a lot of these we now would have a paginated list of all the times that we've ever used nano. So piper history, filter out so that we get only lines that have nano in it, and then use less pagination.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98890 Viewers
61 Video lessons · 86194 Viewers
71 Video lessons · 70032 Viewers
56 Video lessons · 102280 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.