Linux/Mac Terminal Tutorial: The Grep Command - Search Files and Directories for Patterns of Text
20:16

Linux/Mac Terminal Tutorial: The Grep Command - Search Files and Directories for Patterns of Text

Corey Schafer 07.03.2018 370 054 просмотров 9 244 лайков

Machine-readable: Markdown · JSON API · Site index

Поделиться Telegram VK Бот
Транскрипт Скачать .md
Анализ с AI
Описание видео
In this Linux/Mac terminal tutorial, we will be learning how to use the grep command. The grep command allows us to search files and directories for patterns of text. You can also pipe the output of one command into grep to get certain matches. It's extremely useful once you learn the ins and outs. Let's get started... The code from this video can be found at: https://github.com/CoreyMSchafer/code_snippets/tree/master/Terminal/Grep Regular Expressions Tutorial: https://youtu.be/sa-TUpSx1JA Regular Expressions Tutorial (Python): https://youtu.be/K8L6KVGG-7o ✅ Support My Channel Through Patreon: https://www.patreon.com/coreyms ✅ Become a Channel Member: https://www.youtube.com/channel/UCCezIgC97PvUuR4_gbFUs5g/join ✅ One-Time Contribution Through PayPal: https://goo.gl/649HFY ✅ Cryptocurrency Donations: Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3 Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33 Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot ✅ Corey's Public Amazon Wishlist http://a.co/inIyro1 ✅ Equipment I Use and Books I Recommend: https://www.amazon.com/shop/coreyschafer ▶️ You Can Find Me On: My Website - http://coreyms.com/ My Second Channel - https://www.youtube.com/c/coreymschafer Facebook - https://www.facebook.com/CoreyMSchafer Twitter - https://twitter.com/CoreyMSchafer Instagram - https://www.instagram.com/coreymschafer/ #Linux #Mac

Оглавление (14 сегментов)

<Untitled Chapter 1>

hey there how's it going everybody in this video we'll be learning how to use the grep command from within the Linux or Mac terminal so grep stands for global regular expression print and it allows us to search for some text within files on our system and this is something that you'll find yourself using all the time once you become comfortable with it so let's go ahead and look at some practical examples where you might use this so first of all the most basic example is simply

searching for some text within a normal file

searching for some text within a normal file so I'm here in my terminal and in my current directory there is a file named names txt that has a bunch of random names and dummy data so let's search this file using grep to see if it contains the text Jane Williams so I'll just say grip and search for this name of Jane Williams within this file and we want to search the names txt file so what we're saying here is we are doing our grep command and then what we want to search and in this case we're searching for this name and then where we want to search so that is within the names txt file so if I run that then you can see it didn't find any results when there are no results it just jumps to the next line like it did here so instead let's search for John Williams and see if that has any results and we can see that we got two results here where it found two lines containing John Williams and return those entire lines now the second line was John Williamson but it still found Williams within this name so it returned that anyway so now let's say that we only wanted it to return a. match if it was the whole word so we only wanted to search for John Williams and not return like John Williamson here so to do this we can use the dash W option so if I pull back up that last command that we ran if I do a dash W before our search term here and run that then you can see that it didn't return this John Williamson it only returned the John Williams because we put in that we only wanted whole words now I also added a lowercase version of that name into that file also but it's currently not returning that because grep is case-sensitive so we can tell it to not be case sensitive by using the - I and so if I pull up the command that we just ran if I go back here and add the I to our current options and run that then we can see that now we get the lowercase version of that match and the regular case as well so I clear my screen but let me rerun at that last command just to pull those names back up again now just finding those matches doesn't give us information other than knowing our text is actually there but sometimes you want some additional information so for example it's common to want to know the line number of where it found our match and

add the dash in option to our search

to do this we can add the dash in option to our search also so I'm just going to pull up that last search and we can add the in option on to there as well and if I run that then we can see that now we're getting some information about these matches it found this lowercase version on line 51 of that file and this uppercase version on line 431 of that file now another common bit of information that you might want is some additional context of where this match is found so we might want to also see a certain number of lines before and after the match to get an idea of the context of where this match was found and this is especially common if you're searching for something in a computer program that you wrote and want to see the surrounding code of the match and there are several ways that we can do this so if we want to see a certain number of lines that come before our match then we can use the upper case B option and you can remember that by thinking that you know I want to see the lines behind my match so I'm going to go ahead and clear the screen here and pull up the last option that we just ran and now we want to add in this upper case B option and I'm doing this separate from the other options that we added in because we want to pass in an argument to this so let's just pass in a for which we'll say that we want to say C for lines before our match so if I run this then we can see that we got both of those matches that we got before but now there's some additional context here so we have four lines before our match here and now if we want to see a certain number of lines after our match

use the uppercase a option

then we can use the uppercase a option so I'm going to clear this out and pull up that last command and do an uppercase a instead of an uppercase B and if we run that then we can see that now instead of getting the lines before our matches now we're getting four lines before and four lines or four lines after for both of our matches and if you want to see the lines before and after your match then you can use the uppercase C option and you can remember that by thinking that we want to see the context surrounding our match and this is also the one that I use most often as well so if we clear this out and pull up the last command that we just ran if we do an uppercase C and I'm only going to do two lines here before and after so if we run that then we can see that we got our matches and there are two lines before the match and two lines after the match for both of those matches so like I said I especially use this if I'm searching for some text in a computer program or a function or something and I want to see the context surrounding that match okay so that is useful for finding texts in a certain file but what if we wanted to search for text and multiple files at once so let's say that we wanted to run the same search that we've been doing but against every file in our current directory now you might think that we could just do something like this where we let me clear the screen here so let me remove the context option here and just do the search of the whole word and not case-sensitive and get the line numbers and now let's try to search this through every file in our current directory now you might think that we can just do something like this where instead of searching in a specific file that we could just pass in at the current directory as where we want to search but if we run that then we can see that we get this error that this our current directory is a directory so what we need to do is use an asterisk

use an asterisk wildcard

wildcard to say that we want everything in this directory so if I rerun that same command but put an asterisk there and run that then we can see that this worked it said that it found a match in this memo dot txt file on line 4 and also the two matches and the name dot txt file that we saw before you can see that it also tried to search in this subdirectory that we have in our current folder here so it couldn't search that and we got an error there so maybe a better search for us there would be to just search all of the text files

search all of the text files

and we could do that by running the same command and instead just adding dot txt after that wildcard so if we run that then we can see that we got all of our matches but that it didn't try to search this subdirectory here ok so now let me clear that out ok but maybe we wanted it to actually search that subdirectory and this is very common when working on large projects maybe you have a lot of files and subdirectories and are searching for something that you can't remember exactly what directory or file where it's located so in that case you'd just want to

search every single file and subdirectory in your project

search every single file and subdirectory in your project and to do this we can use a recursive search with the - our option and using a recursive search we actually can specify a

specify a directory as the starting point

directory as the starting point so let's do a recursive search for our text within the current directory so I'm going to pull up the search that we tried to do before on our current directory now remember without the asterisks here we got an error before saying that we couldn't search a directory but if we add that - our option onto the end it will do a recursive search and it'll actually search all of the files and subdirectories using that our option so if we run this then we can see that we got those same results from within our current directory within the memo txt file and the - from the names txt file but you can see that it also searched this subdirectory of personnel and found some matches in this emails txt file on line 5 and this phone numbers dot as well and actually to clean that up a bit I could have done that without that ford slash at the end there so let me clear this out and rerun that same command there and that just cleans it up a little bit ok so that's nice that we were also able to search that personnel subdirectory and get the matches within there as well but you do want to be careful with recursive sir just because if you know if you're in a big directory that has a ton of subdirectories then it's going to try to search all of those and can take an extremely long time to return any results and if it takes too long you might just have to kill it so just be aware that those recursive searches could be searching through lots and lots of files now if you're searching through multiple files like this then you might not actually be interested in the matches themselves you might only be interested in seeing what files contain a match and if you only wanted that information then you can add on the - l

add on the-l

option and only return the files with matches so if I pull back up our last command and go back here and I'm gonna take off the in that shows the line because it wouldn't show this here anyway and then I'm gonna add on the L and this will just show us what files contain a match so if I run this then we can see that there that it doesn't display the match it just comes up and says that we found a match in memo txt name's not txt and all the matches within that subdirectory and if you remember we actually had two matches in names. txt file but this result just returns name's not txt once because it's just listing any file that has a match now if we wanted to see how many matches are in each file then we can use the C option so if we pull back up let me clear the screen here if we pull back up the command that we just ran and instead replace that L with a C and run that then we can see that we get some similar output as the L option but it also

displays the number of matches in each file

displays the number of matches in each file and don't worry about this D store file here that's just something that pops up on the Mac sometimes okay so now let's look at one practical use case for using grep that we haven't covered yet so we can pipe the output of other

pipe the output of other commands into grep

commands into grep to search for certain things so for example let's say that we wanted to search our history for our latest get commits so if we just display our history then we can see that if we wanted to find our get commits then there's a lot of information here to sift through so let's pipe this in the grep to narrow it down a bit so we could search for all of our git commits and our git history just by saying so first let me clear the screen here so that we can see a little bit better so we can just run history like we did before but now we're going to use a pipe and pipe that in to grip so now we'll say that we want to grip that output for get commits so if I run this then we can see that it narrows down our history two lines that match our grep search and we can even pipe this output into another grep command to narrow it down even further so if I only wanted to see git commits about dot files then I could run that same last command do another pipe and another grip and instead grep for dot files so first it's going to pass the output of history and narrow those down to the ones that have a git commit which are all right here and then just going to take this output and pipe that into grep again and search for the ones that have a dot file match so if I run that then we can see that now we only get our git commits with the dot files okay so now let me clear this okay so we're just about finished up but I wanted to show one more thing here so let's see how we can do some more advanced searches using regular expressions now I'm not going to go into detail about how to write regular expressions in this video but if you're interested then I'll put a link to my regular expression videos in the description section below so grep uses POSIX regular expressions by default now if you've watched my other videos on regular expressions those videos are actually using something called Perl compatible regular expressions which is also what Python uses as well so let me show you how this might affect your grep searches so let's say that we wanted to search a file for phone numbers so we might do something like this so I would do a grep and now we'll write our regular expression so I'm just going to do something simple I'll do dot dot and the dot and a regular expression is just matches any character so then I'll do a dash and then dot dot and then so this will find any three characters then a dash then any 3 characters than four characters so this should match a phone number and now let's pass in the file that we want to search so we'll names. txt so if I run that then we can see that works and that we got back the phone numbers from that file but let's say that we wanted to make our search a little bit more specific and instead of searching for any character using the dot we wanted to search specifically for digits using backslash d so if you've seen my other reg X videos then you might think that we can do that search with something like this so let me erase this so let's say that we want to only get a digit so we'll do a backslash D and we want three of those so I'll pass in three is the quantifier there and then a dash and then the same thing digit and three of those and then a dash then a backslash D for a digit again and four of those so now if we run this then we don't get the results that we expect and that's because grep isn't using perl compatible regular expressions which is what that is so can we get those Perl compatible expressions to work well if you're on Linux then you can get this to work just by passing in a - uppercase P here before the search but if we run this if you're on a Linux machine and then that would work but I'm on a Mac and we can see that it doesn't now we can get this to work on a Mac but it's a little complicated so this next step is completely optional but you can follow along if you want to grep using these regular expressions on a Mac and if you're using Linux like I said then you won't have to do this step because Linux already has that functionality so in most of these Linux and Mac tutorials all of the commands usually run the same on both systems for the most part but these grep regular expressions are one example of something that is different between Mac and Linux Mac uses a version of grep called BSD grep and Linux GNU grep good new grip and you can see that version if you type grep and then - uppercase v and you can see that here we are using BSD grip and many people prefer the ganoub version of grep and that's what I prefer as well so I'll show you a trick for how

install the ganoub version of grep on the mac

you can actually install the ganoub version of grep on the Mac by installing it through homebrew so if I pull up homebrew in my browser here so I'm just in chrome and this is at brew SH now if you've never used homebrew it's basically a package manager for the Mac so you can follow through the installation instructions from their website and once you have that installed you can open a new terminal and then you should be able to install the new version of grep just by saying let me clear out my screen here we can install this by saying brew install grep and we also want to add in some options here so we'll say with default names the with default names option means that it will install it as grep and if you leave that off then it would actually install it as G grep which would allow you to use both the new version of grep and the mac bsd grep but ganoub grep as the default is fine with me so I'm gonna go ahead and run that and I will let this installation finish and once that's finished we'll likely need to open up a new terminal for that change to take effect if I do a grep - V you can see we're still using a BSD grep so let me open up a new terminal and I can maximize this and I am in full-screen mode here so let me take this down and then pull this tab out here into its own window now let me make this text larger so that everyone can see still okay so now if I say grep - V in our new window then we can see that now we are using the new grep so that's what we wanted okay so back to our example the reason we did all that was so that we could use

use the perl compatible regular expressions on the mac

the perl compatible regular expressions on the Mac and the canoe version of grep accepts that - uppercase P option that tells grep that we want to use those perl compatible expressions so if i go back to my other window here and grab that command that didn't run before on the mac so this is still using the bsd version if i run that you can see that we get an error if i navigate to my desktop and the directory that's where I was running those searches in the other window now if I paste in that command that didn't work before then we can see that by using that uppercase P option that we can now get the results that we expect and we can use these regular expressions with the options that we saw before too so if I wanted to do a more advanced search and see what files contain phone numbers under my current directory then I could do something like this so let me pull up the last command that we just ran and let me make this text a little larger just one more time here okay so if we wanted to see all the files that contain phone numbers under our current directory then what we could do is I

pass in my current directory as the search location

could pass in my current directory as the search location and back here with our options we want to keep that uppercase P so that we can use our regular expression here and now let's also use some options that we saw before so I'll use a W for the whole word search I will use I for case-insensitive even though that doesn't really matter with this current regular expression I will use R to say that we want a recursive search since we're running this on the current directory it'll also search the sub directories and remember the L is what displays the files that contain our match instead of the matches themselves so with all of those options in place if I run that then we can see that it found phone numbers in our names txt file and our phone numbers file within the sub directory and you can do all of the other searches with regular expressions that we saw earlier in this video as well okay so I think that is going to do it for this video hopefully now you have a pretty good idea for how you can use the grep command to search for patterns of text and a lot of different ways throughout your file system but if you do have any questions about what we covered in the video then feel free to ask in the comment section below and I'll do my best to answer those and if you enjoy these tutorials and would like to support them then there are several ways you can do that the easiest ways is simply like the video and give it a thumbs up and also it's a huge help to share these videos with anyone who you think would find them useful and if you have the means you can contribute through patreon and there's a link to that page in the description section below be sure to subscribe for future videos and thank you all for watching

Другие видео автора — Corey Schafer

Ctrl+V

Экстракт Знаний в Telegram

Экстракты и дистилляты из лучших YouTube-каналов — сразу после публикации.

Подписаться

Дайджест Экстрактов

Лучшие методички за неделю — каждый понедельник