Using the grep command

Video loading...

  • 0:02
    Using the Grep Command with Addison Berry
  • 0:07
    In this Command Line Basics video,
  • 0:09
    we're going to be looking at a command called grep,
  • 0:13
    which is a very, very useful command,
  • 0:15
    and I can't tell you the number of times that I've been
  • 0:19
    looking for something,
  • 0:20
    trying to figure out, "Where does X come from in my code?"
  • 0:23
    and someone will say, "grep is your friend."
  • 0:26
    And grep is your friend.
  • 0:28
    It helps you find strings within files.
  • 0:30
    So if you're trying to find text within any kind
  • 0:34
    of files on your system, grep is going to be able
  • 0:36
    to find that for you.
  • 0:37
    grep stands for global regular expression print.
  • 0:42
    And regular expressions I find generally sort of scary,
  • 0:45
    but grep is extremely useful without needing to know
  • 0:49
    any kind of regex wizardry.
  • 0:51
    So, let's go ahead and take a look at it.
  • 0:53
    OK, so to get us started here, I am in a local
  • 0:59
    installation I have of Drupal.
  • 1:01
    It doesn't matter, this is just the example I'm going to use.
  • 1:03
    I'm going to do an ls, so you can see what I have
  • 1:05
    in this directory that I'm currently sitting in,
  • 1:07
    and I just mainly want to point out that I have
  • 1:09
    individual files, like the .php, .txt's,
  • 1:12
    and then I also have a bunch of directories,
  • 1:15
    and I just, so we can see how grep works with both of those.
  • 1:23
    So, grep is the command, g,r,e,p.
  • 1:28
    And so, I'm going to do the most basic grep command that there is,
  • 1:32
    which is just grep, and then I'm going to type
  • 1:35
    the string that I'm looking for, and in this case,
  • 1:37
    I'm going to look for the word theme,
  • 1:38
    and I'm going to put in quotes.
  • 1:41
    And then I'm going to say what file do I actually
  • 1:44
    want to try and find this string in.
  • 1:46
    So I'm looking for the word or the string theme in
  • 1:49
    index.php, and it returns 1 result,
  • 1:51
    and it actually shows me what the line is.
  • 1:55
    So, in index.php, there is a line of text that
  • 2:00
    has that and it has that string theme in it.
  • 2:03
    That's grep, and you can use grep just like that and be done,
  • 2:07
    but I want to also show you a little more stuff.
  • 2:10
    There's, there's, there's more flexibility in terms
  • 2:12
    of how you can use grep.
  • 2:14
    Now, in that command, I was looking for an individual file,
  • 2:17
    but if I put in an asterisk, it'll look through all of
  • 2:20
    the files that are in this directory.
  • 2:22
    So you can see I am getting a lot more results back now,
  • 2:25
    and when it's looking through multiple files like that,
  • 2:28
    you can see I have install.php.
  • 2:30
    So it tells me the filename and then it prints out
  • 2:33
    the actual line, and there's also stuff in update.php,
  • 2:36
    and you can see each of those files has multiple occurrences
  • 2:39
    of the string theme.
  • 2:41
    The thing that I want to point out here though,
  • 2:45
    is that it only looked through the files in this directory.
  • 2:49
    So, update.php, install, index, it didn't go through
  • 2:55
    any of the directories, like themes. I would imagine
  • 2:57
    the word theme appears in there quite a lot.
  • 2:59
    So, when you do that asterisk, it's just going to look
  • 3:04
    through all of the stuff in my current directory.
  • 3:07
    Now, I can, let's go ahead and just do a grep
  • 3:11
    on that themes folder, because I know there's going to
  • 3:13
    be stuff in there, and if I just do the grep on that folder,
  • 3:17
    I get nothing back. And that's because it's a folder,
  • 3:20
    it's not a file that contains strings.
  • 3:23
    If I put a /* and say, everything inside of
  • 3:27
    the themes folder, because there are files in there,
  • 3:30
    then I can get some results.
  • 3:32
    And you'll notice that it's telling me,
  • 3:35
    "In the themes directory, in the README.txt file,
  • 3:38
    these are the lines that have that string that I'm looking for."
  • 3:42
    So let me, I'm going to pop into the themes directory real quickly.
  • 3:46
    Here's the README.txt file and that's what it found.
  • 3:51
    Again, there's a whole bunch of directories in here,
  • 3:54
    but it didn't find any, it didn't look for the string
  • 3:57
    inside the directories, just inside files.
  • 3:59
    You need to remember this with grep.
  • 4:02
    And so, what I'm going to do to correct this,
  • 4:05
    I'm going back up to my root here.
  • 4:07
    So, instead of just doing, you know,
  • 4:11
    greping for the string inside of the themes folder,
  • 4:14
    I'm going to do the same thing, but I'm going to add a
  • 4:16
    -r, and common with other commands, -r means recurs.
  • 4:22
    So go through all of the directories and keep drilling
  • 4:25
    down into child directories, until you get to everything.
  • 4:29
    Now, when I do that, I'm getting a lot of stuff back.
  • 4:32
    You see I'm getting tons and tons of stuff back now, because,
  • 4:35
    well, the word theme appears a lot in this directory.
  • 4:37
    So you can see it's going down into themes/garland,
  • 4:41
    themes/pushbutton, and then there are files
  • 4:43
    within those directories.
  • 4:45
    So the -r, really important if you want to
  • 4:49
    look through an entire series of folders.
  • 4:51
    Don't use the -r if you just want to look right with
  • 4:55
    the files within a particular directory and not go further.
  • 4:58
    So, OK, I'm going to leave that top level directory and just
  • 5:02
    work in the themes directory here now, for a little while.
  • 5:05
    So, this is where I am.
  • 5:06
    I have 1 text file and then a bunch of directories.
  • 5:09
    OK, so I'm going to change things up a little bit
  • 5:12
    and instead of searching for the string theme,
  • 5:16
    which would give me a million hits here,
  • 5:17
    I'm going to use something else that'll limit my results.
  • 5:20
    So I'm going to switch over to looking for username.
  • 5:23
    Now, I could just, so if I want to search, you know,
  • 5:26
    throughout all of this, I could use a star, but again,
  • 5:30
    remember that using that is just going to give me the files
  • 5:34
    that are in this directory, which would just be README.txt.
  • 5:38
    None of the directories would actually get searched.
  • 5:42
    And so to do that, I need to go back and put in this
  • 5:45
    -r to say, "Go recursively through directories."
  • 5:48
    Then I have my string, and now I can do a star.
  • 5:52
    You can also do a dot, which means in Linux speak,
  • 5:57
    dot means the current directory.
  • 5:58
    So, from this directory and then that r will recurs down.
  • 6:02
    So, I run it that way, and you'll see
  • 6:07
    I have 2 different files.
  • 6:10
    So chameleon theme and this template.php,
  • 6:13
    and the Garland folder.
  • 6:14
    Multiple results within those 2 files.
  • 6:18
    That's a bunch of stuff to look at.
  • 6:20
    I'm going to show you another handy thing to help
  • 6:23
    you really zero in on what it is you're looking for.
  • 6:25
    I'm going to run the same command, but this time,
  • 6:28
    I'm going to add another option in that dash series
  • 6:32
    of options we have.
  • 6:33
    So it's -r to recurs, and I'm going to add
  • 6:36
    the letter n and when I run that,
  • 6:38
    what it's going to do is display the line
  • 6:40
    number that that actually appears on, within the file.
  • 6:44
    OK, so this is a little messy.
  • 6:47
    It's a lot on my screen.
  • 6:48
    So let me, I'm going to highlight the results set here.
  • 6:53
    So, this is, this is what I got back,
  • 6:55
    the stuff that's highlighted.
  • 6:56
    It's the same results that I got previously.
  • 6:58
    The number of lines and everything like that are
  • 7:02
    all the same. Just, you can see,
  • 7:04
    it gives me the filename and then
  • 7:06
    a colon and a number, and that number is the
  • 7:07
    line number within that file.
  • 7:09
    So I can go to that, open that file up,
  • 7:11
    and go directly to that line number and I'll be right
  • 7:14
    where I need to be.
  • 7:15
    So it's very handy.
  • 7:16
    So you could just zero straight in to where it is.
  • 7:20
    OK, let me clear the junk off of there.
  • 7:22
    Right?
  • 7:23
    We're back in our themes directory,
  • 7:25
    and let's have a look at some more fun options.
  • 7:31
    Sometimes, getting all of those strings back is overwhelming,
  • 7:35
    and you just want to know what files have the stuff,
  • 7:37
    not the actual lines.
  • 7:39
    If you use l for just listing, so I'm doing
  • 7:42
    -rl, it'll just list the file names and it
  • 7:47
    won't give me all of those results, right?
  • 7:48
    There were several results in each of these files.
  • 7:51
    So we have chameleon and template, same files,
  • 7:53
    but it's just not listing every single thing.
  • 7:56
    And then from there, I can zero in and say,
  • 7:57
    "Oh. I want, I want garland's template.php,"
  • 8:00
    and then I can grep just in that file,
  • 8:02
    and it just helps you, sort of, narrow down your
  • 8:04
    search results and not get overwhelmed with a wall
  • 8:07
    of text that gets kind of spit back at you.
  • 8:10
    So I like to use that for like an initial rough,
  • 8:13
    trying to sort out which files have it and then
  • 8:15
    I can dig in deeper.
  • 8:19
    Alright, so, let's look at some more fun options
  • 8:22
    we can do with grep.
  • 8:24
    I'm going to do, -v, and I'm going to look,
  • 8:28
    I'm going to keep this to a specific file here,
  • 8:31
    with theme, string theme, README.txt.
  • 8:36
    The -v is going to do reverse.
  • 8:39
    So I want all of the strings, all the lines in the text
  • 8:42
    file that do not have the string theme in them.
  • 8:45
    So you can see in my results that none of these,
  • 8:47
    these 3 lines that I got as a response, have the string theme
  • 8:52
    in them anywhere.
  • 8:53
    So, -v means reverse.
  • 8:55
    If I go back and take this v out and just run it straight up,
  • 8:59
    you'll see each of the lines that are returned,
  • 9:01
    has the string theme in them.
  • 9:04
    So, -v can be very handy if you're trying to weed things
  • 9:07
    out, sort of, you know, in a backwards kind of a way.
  • 9:10
    And just, I'm going to just do this again,
  • 9:15
    but with the n option.
  • 9:17
    Just so you can see n again, but also,
  • 9:20
    just to make it a little more clear,
  • 9:21
    what's happening here.
  • 9:22
    So, I'm going to do the same thing here.
  • 9:24
    I'm going to grep, with the v and the n,
  • 9:27
    so we can see line numbers, and you can see,
  • 9:30
    I'm moving the reverse.
  • 9:31
    Do not, lines that do not have theme, 3, 5, 6, 7, and 8,
  • 9:36
    which means that ones that do have
  • 9:39
    theme, should be 1, 2, and 4, which they are.
  • 9:45
    Alright, so, this is all within the same file.
  • 9:48
    It's just splitting out the lines,
  • 9:49
    whether they have theme in it, or if they don't
  • 9:51
    have theme in it.
  • 9:52
    It could be a really, really handy thing to have.
  • 9:55
    Now, one last option that I want to look at is,
  • 10:01
    has to do with case.
  • 10:04
    So, in this file, in README.txt,
  • 10:07
    we can see that there's the word This. So I'm going
  • 10:10
    to just do a grep for This.
  • 10:14
    So, grep, my string is going to be,
  • 10:18
    just the word This, and I won't do it in this
  • 10:21
    README file, so I don't get a million things back.
  • 10:24
    But I get nothing back and that's because
  • 10:28
    This is capitalized, and This is case-specific.
  • 10:32
    If I do this grep again, but change that to a capital t,
  • 10:35
    now I get my results.
  • 10:38
    So you can see, there's This and This. They're both
  • 10:39
    capitalized and there are no lower case thises.
  • 10:44
    So, there's an option so that you don't have to deal
  • 10:47
    with this, which is -i, which means ignore case.
  • 10:50
    So, if I re-run these greps now, well, this is with the capital.
  • 10:57
    So I get the same results, because it's capitals.
  • 10:59
    But if I go back and change this now to the lower case t,
  • 11:02
    I'm going to get the same results sent back,
  • 11:04
    whereas before, I got nothing.
  • 11:07
    So, -i, ignore case.
  • 11:09
    So if you're not sure if there's capital letters or not,
  • 11:12
    in what you're looking for, use -i. One thing to keep in
  • 11:16
    mind is that, you can mix and match all of these options, right.
  • 11:21
    You've seen I've done, like, I did rl and I did nv.
  • 11:23
    You can put those together after the dash.
  • 11:26
    So you could do, you know, -rniv, if you wanted to.
  • 11:30
    No problem.
  • 11:31
    Now, next, I'm going to show sort of an example of how
  • 11:36
    to use this in the real world,
  • 11:37
    like, where this might be actually be handy in other ways.
  • 11:40
    I'm just in a website here, and I've got some,
  • 11:42
    some text that's being generated by the site,
  • 11:45
    and I want to figure out where it's coming from.
  • 11:47
    How is that being generated?
  • 11:49
    So, I'm just going copying that string from the interface that
  • 11:53
    I saw, and, see I'm in my, the root of this website,
  • 11:58
    of the Drupal sites directory, and I'm going to grep.
  • 12:01
    I'm just going to grep for the strings throughout the entire
  • 12:03
    file structure of the site and see who is generating it.
  • 12:08
    And I get a result back and you can see that it's in the coder
  • 12:11
    module, includes folder, coder_style.inc.
  • 12:14
    And if I do this same command again, and put
  • 12:16
    the n, I also get the line number.
  • 12:19
    So now I can go to coder_style.inc, line 30,
  • 12:22
    and see all of the stuff that's going on around that,
  • 12:26
    that's generating that particular text, and help me,
  • 12:30
    you know, if I need to troubleshoot or something like that.
  • 12:32
    That's a place to get started.
  • 12:34
    I'll show 1 other example here,
  • 12:36
    which is for, handy for themeing.
  • 12:40
    I was just looking, I'm using Firebug here to just look at
  • 12:44
    at the HTML here, and you can see I have
  • 12:47
    this particular class on that particular box,
  • 12:50
    and let's say I wanted to figure out how
  • 12:51
    that class is being generated, because maybe
  • 12:53
    I want to change it.
  • 12:55
    Again, I can grep, and I'll do an -rn because
  • 12:59
    I want to go through the entire directory structure,
  • 13:02
    and I want the line number, and I'm just going to put
  • 13:04
    that class name in and say go.
  • 13:07
    And you'll see when I get my results here,
  • 13:11
    it gives me the filename and the line number and there's
  • 13:15
    only 1 file that's doing this.
  • 13:18
    And so what I can do is go to that particular file.
  • 13:24
    So I'm just going to grab this whole path here and I'm going to just
  • 13:28
    pop this thing open in Vi. Let me copy that, paste that in.
  • 13:34
    So open that file up and the line was 1880.
  • 13:37
    So I'll go to the line 1880, and there you go.
  • 13:41
    This is the actual line that's creating that class,
  • 13:44
    and like within Drupal here, if I go up to the function,
  • 13:48
    I can see the function that's doing this.
  • 13:50
    And this is actually a theme function,
  • 13:52
    which means if I wanted to, I could, I could override
  • 13:54
    this theme function and completely change that
  • 13:56
    class that's being output.
  • 13:57
    Now, it might not be a real-world example,
  • 14:00
    in this particular instance, but it shows you how you can
  • 14:04
    use this to figure out where things are coming from,
  • 14:07
    so that you can then troubleshoot or go in
  • 14:10
    and modify them as you need to.
  • 14:12
    So, grep is super useful, this is just the tip of the iceberg.
  • 14:16
    You can do full regular expression stuff,
  • 14:19
    but even without all that, awesome tool.
Loading ...

Using the Grep Command

Loading...

This video in the command line series will get you started using the very powerful grep command. This is one of the most used commands, especially if you are working with a lot of text (like code, for instance). Grep will let you search through files to find strings and it's great for tracking down where something is coming from. Grep is a very powerful tool with lots of options, but you'll see you can do a lot even with just the basics. We cover the basic command with a file, how to use it through directories, and then we use a few of the most popular options:

-r Recurse
-n Line Number
-l List
-v Reverse
-i Ignore Case

Note: this video was originally released June 22, 2010 on Lullabot.com.

Additional resources:
There are no resources for this video. If you believe there should be, please contact us.