Let's talk about grep.
Grep is one of the most important utilities on Linux.
We have already seen it. That's how important it is.
So grep is used to search text strings or regular
expressions in files, or using a pipe in command output.
A regular expression is an advanced text pattern. We will talk about it in more detail in lesson seven.
So grab is one of the most important tools on Linux because it's always about
vals, it's always about output and how to get the relevant information out of it.
Couple of examples. Grab lynda searches attacks Linda in all files in
the current Directory or PSAUX Pipe GrabHDP uses a pipe to show
all lines that contain the text HTTP in the output of ps.
Very important for advanced use of grep. Regular expresses can be
used to match file patterns. More about that in lesson seven.
Now grep has a couple of options that can be useful, like I for ignore
case V which will show all lines that do not contain the selected pattern.
L to list file names that contain the pattern without showing matching lines.
A5, which shows lines that match the pattern pattern as well as five
lines after and B5, which shows lines that matches the pattern as well
as five lines before. You can also combine A and B by using
C or R, which is useful to recursively search for a specific pattern.
Let's do a quick grep demo.
So I'm starting with psaox. Psaox is showing all the processes currently running and I want
to know if SSH is running. So I'm using grep ssh and what do we see?
We see information about the SSHD process, which happens to be what I was looking for.
We also see stuff that I don't want to see, and that's the grep command itself.
And you know, if I'm going to add grep v grep that
means show me the output. But from the output, remove all lines
that have the text grep and that is showing the clean result.
Next let's do a grab lynda on etc which is showing all files in etc containing
the text Linda, maybe we want to do that with a sudo in front of it
so that we can see all files and oh boy, a lot of directories.
Well, if we don't want to struggle with the directories R so that we
can see files in etc and its subdirectories. Now we have a clean result.
As you can see, in the result we have the name of the
file followed by the line that contains the text. We were looking for.
If that's not what you want, use l with the command that only will list the files.
Now next, you might be interested in the after
and before options. Let me show you psfaux.
Psfaux is showing a process forest.
So let's say that in P.S. faux I'm interested in the line
faux, but I want to see that including the five lines before.
And that's because in process management there is a hierarchy of processes.
A command is always a child of some other process which is a child of some other process.
By using this b we can see the lines before and the lines before
make sense in hierarchical tree output. You can also use a for after.
That's more common if you are looking for headerfiles.
So let me do a sudo. I'm not sure if it needs sudo, but let's do it anyway.
Sudo grep a5 root on etc sshdconfig
and there we can see. Aha.
We have the root directory and that's a five lines following. That's a little
bit disappointing, but maybe that's because I haven't included minus I for case insensitive.
Yeah, this is what I was more expecting.
Permit root login with an uppercase r. If you don't want to have any
surprises because of case, use grep I to make it case insensitive so that
it matches lower as well as uppercase and any mix of that.
That's all for now. We'll get back to grep when we talk about regular expressions.