Linux Crash Course - The grep Command

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello again everyone and welcome back to my linux essentials series in today's video we're going to go over the grep command now this is a command that you've no doubt seen all over the place because it's very rare that i see a single tutorial that doesn't include the grep command at least once it's a very popular command so it's definitely something that we'll need to check out in the series but there's more to this command than what you normally see when you look up a tutorial so there's some examples i definitely want to show you but before we get into that i want to mention the sponsor for today's video lenode and lenode has been a sponsor of learn linux tv for longer than i can remember and they're a great fit they're a linux focused cloud server provider and well this is a linux focused youtube channel so it all works out you could use their platform to quickly and easily spin up your very own linux server and you can use that server for all kinds of different things like setting up your own vpn server perhaps you want to start a blog set up next cloud there's all kinds of different things that you can create on their platform and since it's linux based you can choose your favorite distribution they have all kinds from centos to fedora debian ubuntu and so on they have a lot of different choices there and once you get your very own server who knows what you might build and even if you don't have a use case in mind you can spin up a server to simply use as a test server while going through any of the tutorial videos that are available on learn linux tv if you don't already have your own account you could create one with the url that you see on the screen right now and that'll give you 100 in credit that you can use to do all kinds of linux related activities and that credit is good for up to 60 days so there's all kinds of linux that you could fit within that credit thank you so much to lenode for your continued sponsorship of learnlinux tv i really appreciate it and now with that out of the way let's go ahead and dive into the grep command [Music] so what exactly is grep well grep stands for global regular expression print and although regular expressions are supported that's not something that we're going to go over in this particular video but the whole purpose of grep is to search for text within files so what i'm going to do right now is give you an example of the grep command in the same style that you normally see if you were to see it within a tutorial so what i'm going to do is use the cat command to display the contents of a file and if you're already thinking this might be a bit redundant you're right but we'll get to that i'm going to use as an example the ssh config file which is this file right here and it's quite long actually so just to give you an example we have 33 lines within that file so in my case it's not the biggest config file i actually trimmed mine down so it only has the config that i need that's a whole other video but we have 33 lines of text to look through here so if i'm looking for a specific thing it would be great if i could just find that specific thing and that's one of the things that the grep command helps us achieve so if i recall that command and instead of piping it to the word count command i'll instead pipe it into grep and what i want to search for is port with a capital p and there it is this particular server right here is using the default port of port 22 for ssh now the file that you look through doesn't really matter i'm going to give you an example file in this video anyway but i use the ssh config file as an example because it's something that we edit quite often in this example i use the cat command to print the entire contents of the file but instead of showing the contents to the screen i piped that command into the grep command and i grepped for port so i guess you could say grep is also a verb in addition to a command now another trick we could do is we could actually reverse this output by typing dash v as an option and with quite a few commands in linux the dash v is for verbose but when it comes to grep dash v means something else let's see what happens so what did happen is that it printed every line that did not contain port so the first example we're looking for a line that contains port regrepping for port as you see here but the dash v option is an exclusion so if you include dash v it's going to give you every line that doesn't contain that particular term it's perfectly valid to use the cat command to cap the contents of a file and then pipe that into grep to search for a particular word but that is a bit redundant check this out i'm not going to pipe something into grep i'm just going to use grep by itself i want to search for the word port and the file that i want to search in is slash etsy ssh sshd config and this is the same file that we were searching in earlier i'll press enter and it does the exact same thing as this example right here but it's shorter so like i mentioned there's nothing wrong with using the cat command and piping it into grep but you don't have to do that grep is able to search within files by itself it doesn't need the help of cat or any other command this is what grep does so using the cat command and then piping that into grep that is redundant because grep is able to search within files by itself so let's go ahead and start from the beginning and what i'll do is i'll build your knowledge of the grep command and give you a more well-rounded look at the command and to facilitate that i've created a text file off camera i called it characters.txt and as you can see here every line is a different character and some sort of fandom you have characters from ninja turtles we have superheroes here a couple villains you get the idea now this is just a random file that i created to serve as the example within this video and i'll make sure to have this text file available in the blog post for this particular video the link will be in the description but again it doesn't matter what text file you use grep can search through any text file so let's have some fun what i'm going to do is use the grep command and what i want to do is show every line from that file where the species is turtle so i'll use turtle as the keyword here and characters.txt is the file that i want the grep command to look in and if i press enter you can see that i have four lines here we have the species of mutant turtles so i use the word turtle as my search term and on your end it might not color code the output here that's a shell customization that i've implemented but anyway as you can see the grep command worked it searched through the characters.txt file i was searching for specifically the word turtle and it printed every line that contains that word now i know this example isn't very practical but this is just an example so we'll just go along with it for now similarly i could change the search word and search for something else and then we have the human characters right there and as i mentioned earlier if i use dash v then it's going to show me every line that does not include that search term and those are probably the two most important examples of the grep command but actually there's more there's more that we can do with the command so let's see some additional examples so i'm recalling this command right here and what i'm going to do is add the dash n option to the grep command and let's see what happens now as you can see here i have line numbers on the left hand side so this is really useful because the grep command is actually able to give me the line number that the search term was found in so lines 1 2 6 11 and 12 all contain the word human in addition to that we have the dash c option and what do you think this is going to do well let's press enter and find out if i add the dash c option it gives us the count the number of times that that word appears in the file but it doesn't actually show the lines themselves just the number of times that particular search term was found in the file now by default grep is actually case sensitive so if i search for a human in lowercase it's not going to find anything because i don't actually have that term with an uppercase h within that file so what we can do instead is add the dash i option and what that allows us to do is not care about case sensitivity and as you can see it works human with a capital h is a match even though we didn't type a capital h it was still able to match it that's pretty cool so what i'm going to do right now is switch gears away from the characters file i mean it was really fun but i want to show you some more practical examples that mirror some real world scenarios and maybe that'll help commit the grep command to memory so what i'm going to do and it's okay that you're not able to follow along with this because you're not going to have the same files that i have on my file system you can just watch and take notes that's totally fine or you could just adjust the examples to fit files that you do have but in my case i have a git directory inside there i have a personal directory and this means that these are get files that are not actually uploaded anywhere they're only stored here locally and i have a local ansible repository and don't worry so much about ansible i have an entire series about ansible if you want to learn it but you don't have to understand the syntax of the files that i'm about to look through in order to understand exactly what i'm doing inside ansible i have a roles directory and there i have workstation then tasks desktop environments and then gnome i actually use ansible to set up all of my computers long story anyway let's go in there and we have some yaml files so what i'm going to do is use grep the keyword that i want to search for is g edit that's the default text editor for the gnome desktop it's going to be switching to a different text editor here very soon with the next version so perhaps i want to find every occurrence of g edit and replace it to the new text editor that's coming so that might be one reason why i might want to search for this particular keyword but anyway what i'm going to do instead of giving it a file is just tell it to look through every file and that's what a star does so press enter and as you can see i have several files right here that include the word g edit and as we saw earlier i could use dash n to show me the line numbers that's very useful so we can see we have matches on line 1 5 159 and 9. we see those numbers right here so let's just say for example that i want to search for a particular term i don't know what file that term might be in and maybe i don't even know what directory that term might be in so is there a way that i could use the grep command to recursively search through files well yeah i mean if there wasn't why would i even bring it up so grepdash r is one of my favorite variations of the grep command because that lets you do exactly what i just said it allows you to recursively search through files even through directories to find your match so what we'll do is we'll use r and maybe i want to search for g edit i want to know if this term is in any other file even a file outside the directory that i was in earlier and then after that we type the path where we want to start the search from and the closer you can get the better it doesn't really matter i could actually start the search from right here but i have a bunch of files in my home directory so if you can narrow this down it's better you don't have to be exact but i know that anything i'm interested in will be in my git directory and i know that i've only used g edit within the roles directory so i think that's good enough so grep dash r search term and then the directory that you want to start your search from let's see what happens so it looks like this particular command is providing value to me because i know that there's other occurrences of g edit outside the directory that i was in and i found that out by using grep r another example we might use is grep dash r maybe we want to search through log files so i'll go through slash var log and the search term that i want to search for is error and what i'm going to do is add the dash i option to that as well because i don't know if the e is going to be capital in any errors that might exist or what might be there so i'm just going to use this variation of grep right here r for recursive i for case insensitive search term error and then var log is where i want to start my search from so i want to see all the errors let's see what happens and as you can see i have a number of matches right here i see the word air in red and again it's only red in my case because of a shell customization but the command did work we have errors listed here and if this was a server that actually was used for something more important this one isn't really the most important server in the world then maybe i might actually have some more relevant results but as you can see the grep command is very useful as for the examples we've used you've seen that we can use the cat command to cap the contents of a file we could pipe that into grep and then search for a particular term that's probably the most common way that people seem to use the grep command and i'm one of them it's muscle memory for me nowadays but actually all you have to do is grab for a term and then the path to a file if you don't know the file then of course you can use dash r just like that to search for every file underneath a folder for a particular term and that's also a perfectly valid way to use grab [Music] so there you go the grep command is very easy to get started with and in this video we've gone over the basics so i hope it helped you out if this video did help you out please click that like button because that lets youtube know that other people might benefit from this content as well and i would really appreciate that i have other episodes in this series coming very soon so make sure that you subscribe so you'll be the first to see those when i do have those uploaded but in the meantime thank you so much for watching [Music] you
Info
Channel: Learn Linux TV
Views: 47,771
Rating: undefined out of 5
Keywords: Linux, Tutorial, Howto, Guide, Learn Linux, open-source, open source, gnu/linux, LearnLinuxTV, grep, grep command, linux commands, linux essentials, linux tutorial, linux tutorial 2021, linux tutorial 2022, linux tutorial for beginners, command line, linux command line tutorial, linux grep command, linux grep, linux basic commands, grep tutorial, linux command line, grep unix, grep command in linux, grep linux, grep (software), linux pipes, linux grep tutorial, unix grep, how to
Id: Tc_jntovCM0
Channel Id: undefined
Length: 14min 57sec (897 seconds)
Published: Tue Mar 29 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.