Hey guys, welcome back. In this video we're gonna be talking about the "grep" command,
and how we can use that to find things in files. So, in the previous video we
learned about the "find" command and how we could use that to find files, and now
we're going to be covering how to find things in file. So, we do that using the grep
command, which you can use like this. So, first I've got a bunch of .php files here,
remove these two files there, so I've got a bunch of .php files here and what I've
done was put some functions in them. So, this one has two functions and the other
ones have some other ones as well, and we're gonna be using this to demonstrate
how to use grep. So, you type the command "grep" and then what you're looking for,
and then what files you're looking for them in. So, if I were to type "file2.php"
this would return the three functions that I have in that file. If I
were to just keep adding on files here, it will look through each file and it
will actually return which file they're found in. It's pretty neat, two things that
I want to talk about regarding grep before we do anything a little more
extreme, is two flags that it can take. So, a flag in a command is a way to pass it more
information about what we want to do, we can can get really specific. So, we're
gonna run grep using "-i" which means ignore the case, so case
sensitivity won't be a problem here, and then we're going to look in all files.
Oh, I forgot the search string we're looking for a function, and this finds
not only the functions that we saw in the previous command, but it also found
this function which has a capital "F". If we were to run the grep command without
the "-i" on all files here,
you'll notice that the only difference between the output is in "file2" here
it found function with a "F" and it didn't in this result. So, that's how you
use the "-i", it's pretty standard across Linux usually "I" will be
integrated into commands as you know ignore case sensitivity. Now the second flag I wanna
talk to you guys about is really neat, it's "-n" and it can be used in
conjunction with other flags.
We're gonna take the string that we're looking for, and the files were looking for it in,
and what this "-n" flag does is it gives you the line number where the
instances of the string is found, and then the rest of that line. So, this is
pretty cool, this is how use grep.