 One of the most important tools on your Linux system is a command line utility called grep. What is grep? Grep is a way to search a file for a particular string. Basically what it does is you give it a pattern to match and when it finds that pattern it matches that pattern it returns the line from that file that matches that pattern. Let me show you this in action. So here is my terminal and grep basically works this way. You do grep and then some string to pattern match and then name a file or a path to file and then it will return every line from that file that contains that string. That's how that works. Now if you wanted to see this in action what I could do is I could search for this pattern here, thg. So I'm going to grep thg that particular pattern from this file. I'm in my home directory. I know my home directory has a bash rc in it so I'm going to grep thg on my dot bash rc file. I'm going to hit enter and that shows you exactly what grep does. It returns every line from that file my bash rc that contains that pattern thg. So every line that had the word the and every line that had the word then because thg is part of then or they or anytime it finds that pattern it prints out that line that's part of the output. Now of course maybe I only wanted the word the maybe I don't want then and they and every other word that contains thg within it. Well you could still do that so if I hit the up arrow on my keyboard and I give it this flag grep dash w thg bash rc what this will do is grep for a specific word not a pattern I'm not looking for a pattern within a word it has to be the word the thg and now when I run that I get a little less output because now I only get the lines that it actually thg is a word so you know there's a space before and after it where before I was getting things like then return because then a common bash statement. Now let me cd into a different directory to play around and I'm going to cd into my .xmonad directory so this contains all the files of my xmonad window manager let me clear the screen and if I do it ls these are the files within that directory. Now one thing to notice about grep is grep can't search for a pattern unless it's searching a file that's written in text right in plain text so it can't grep a binary and you can't grep a directory for example if I tried to grep this string xmonad on period period is the alias for this directory the .xmonad directory I'm going to get an error it's going to say is a directory right period is a directory give me something that's not a directory well let me hit the up arrow key what I could do is I could do some file globbing and some regular expressions with grep as well I could do a file glob right here I could grep xmonad that particular string xmonad on period slash and then give it the asterisk symbol and what that does is it tells grep hey search for this string xmonad on every file and directory within this directory now that we're still going to get some errors when I run this on the files it's going to give me something but on some things it's going to return errors because it's going to complain about anything that's a binary you see that's a binary file so it says hey I can't handle that I also can't handle the xpm directory because it's a directory so it's still going to complain about certain things well I could get rid of the directories I could give it this flag here I could grip xmonad on everything in this particular directory and then I could do dash dash exclude dash dir so exclude directory equals and there were two directories in here I could do xpm I remember that directory was there but there were two directories so let me do an ls there was also a xmonad example configs directory so what I could do is I could exclude directory equals and then give it a list of directories not to try to grip so xpm and then xmonad dash example dash configs and now it will grip just the files it's going to exclude those directories it's still going to give me an error about the binary so there's a few binary files in this particular directory grep can't handle those so since it's complaining about the directories and binaries let's just stick with gripping on a single file for now so if I wanted to I could grip and you know getting back to some of the stuff with regular expressions that you could do you know you could do list of characters you could also do ranges of characters so if you wanted to see a list you know what you need to do is grip and then do the square brackets so do inside the brackets I could give it this list of characters to search for how about all the vowels a e i o u grip for any line that contains a or e or i or o or u so practically every line from this file and then I could do xmonad.hs that particular file in this directory and again it's going to return every line I doubt there's a single line in this file that doesn't have a e i o or u it's hard to make a word without vowels let me clear the screen let me up arrow and that was a list of characters you could search for you could do a range so I could do a z that's the entire alphabet that is going to return every line as well but I could do something to narrow it down a little bit how about grip x through z so it's only going to return every line that it contains either x or y or z and it's still going to be a bunch of lines returned here let me clear the screen also a really neat thing you can do with a regular expressions make sure you guys know about the carrot symbol the carrot symbol symbolizes the start of a line the beginning of a line so if I grip space the carrot symbol im for example and then name a file xmonad.hs in this case it's only going to return the lines that begin with im which is a few lines in my xmonad config every line that begins with the word import was returned some of the flags with grip that you really need to know about are the capital a flag the capital b flag and the capital c flag and what these do is they search for your pattern and the particular file you're searching and they return the lines that contain that but they also contain the lines that are right after the match right before the match or both before and after the match let me show you so if we do the dash capital a flag grip dash a and give it a number for how many lines after three lines and then search for a pattern how about log hook i'm going to grip for this pattern log hook and my xmonad.hs so any line that contains the word log hook should be returned it's also going to return the three lines after the lines that also match I hope that makes sense let me hit enter nothing is returned I know I've got log hook in my xmonad.hs but I don't know if it's capitalized or not so let me rerun that give it the dash i flag as well the lower case i for case and sensitivity and now we actually get some stuff returned I matter of fact that I'm glad I made that mistake on camera typically when you run grip it's usually a good idea just always to give it the dash i flag for case and sensitivity because you can't you never know you know for at least for me I never know if I actually capitalize something or didn't but you see what the capital a flag does here it returns any line that contains log hook so you know these lines here contain log hook it also returned the three lines after it which in this case were a blank line followed by this my keys line followed by the dash dash line so these three lines don't match the pattern but they are three lines after a line that matched the pattern so it returns the pattern match plus the next three lines now that's useful because sometimes you just need a little context around what you're searching for and let me rerun that command and if I did grip this time dash capital B three it's going to return of course the lines that match the pattern and then the three lines before the lines that match the pattern let me hit enter so now I've got the lines that contained the word log hook and you see I also get three lines before the lines that contain the word log hook and to show you what the dash capital C does it's basically a combination of capital A and capital B so it returns the three lines both before and the three lines after the pattern match so once again searching for my log hook we get that and then I get the three lines before and then I also get the three lines after and let's use a couple of other interesting flags here so if I do a grip dash capital H capital H says hey I want you to return the name of the file you know so it basically prints the file name we're searching so dash capital H I could also give it lowercase n n is for printing the line number I want you to give me the file name and the line number for this particular pattern and how about we search for the word import I know there's a bunch of lines that contain import in my expo net dot HS and you see exactly what it does we get the file name the line number and then the actual line that contain the pattern match another interesting thing you can do with grip is sometimes other than just printing out the line sometimes you just want the number of occurrences of the pattern so I don't really need to see all those lines that contain the word import I just want to know how many things I'm importing for example in my expo net dot HS so what I could do is I could get a count so grip dash C for a number of occurrences and then the string import and expo net dot HS and it tells me that there are 58 lines in that document that contain the string import now let me clear the screen one last thing I want to mention because it's it can be kind of controversial one thing you can do is sometimes people do this they cat a file so maybe I want to cat I don't know how about my bash RC so let me do the path to my bash RC because I'm not in my home directory at the moment and you know I could cat my bash RC for some reason and then you know I want to pick out a particular segment or line from my bash RC and since I just catted it what you could do up arrow to bring the cat command back up and then just add the pipe symbol grip and then whatever it is you want to grip such as in my case I want to grip the word Rick roll and now quickly I was able to get the lines that contain Rick roll from my bash RC now command line snobs will tell you never to cat and then pipe it into grip because it's unnecessary you're basically running two programs you run cat and then you run grip when all you really need to do is just grip you know Rick roll dot bash RC you know and you get the exact same thing well of course those people they have a point but sometimes if you're at the command line you're often catting things and if you have just catted something and then want to run grip it is so much easier just to hit the up arrow and get the cat command back up and then just add grip and then the pattern you're searching for so don't let the command line snobs tell you that you can't cat and then pipe it into grip there are use cases why you might want to do that occasionally now before I go I need to think a few special people I need to think the producers of this episode I need to think Michael Gabe happily Nate Corbinian Mitchell entropy UK John arch 5530 Chris Chuck DJ Donnie Dylan George Lewis Omri Paul Robert Sean Tobias and Willie these guys they are my highest tier patrons over on patreon without these guys this episode about learning the command line utility GNU grip it wouldn't have been possible the show is also brought to you by each and every one of these ladies and gentlemen all my supporters over on patreon because this channel is supported by you guys the community if you'd like to support my work you'll find distro tube over on patreon all right guys peace