- In this video, you are going to meet your new best friend,
the grep utility.
I'm not kidding.
Grep is one of the most useful utilities in Linux.
But why? Well, that is because grep
is what you use to find text.
You can use find text using strings or regular expressions,
which are special patterns that we'll discuss
in the next lesson.
And you can find text in files,
as well as using a pipe in command output.
And grep, seriously, is one
of the most important tools on Linux.
You will be using it so often.
Let's have a look at two examples.
If you would to "grep linda *",
that's a basic use
where you will search the text "linda"
in all files in the current directory.
And you can use it in a pipe,
as in "ps aux | grep http",
which uses a pipe
to show all lines that contain the text "http"
in the output of PS.
For advanced grep use,
regular expressions can be used to match file patterns.
We'll discuss that in the next lesson.
For now, we need to understand the basic grep use.
So let's first talk about some
of the most useful options.
To start with, there's minus i. Minus i is to ignore case.
So if you're looking for "linda", you don't know
if you created "linda" with an uppercase L
or not, use minus i.
Minus v is convenient.
It will exclude a pattern.
Minus l lists files that contain a pattern,
without showing matching lines.
And then we have a couple of options,
minus A5, minus B5, minus C5,
to show you also the lines before and the lines after.
And minus C, is for combined minus A as well as minus B.
And grep minus R, we are recursively search for a pattern.
Let me show you how this works.
All right, let me start with "sudo grep linda /etc/*",
which will look in "*" in "etc"
to see if there is occurrence of the text "linda".
There we go.
And oh boy, I'm getting a lot of messages.
Like, "Is a directory" and whatever else.
That is one side effect of grep.
If it finds a file that is a directory,
it'll tell you, "Hey, I have directory."
I don't wanna see this.
So I'm using "2>/dev/null".
We've already seen that this is redirecting errors
to the null device, which makes you don't see them.
And here we see the result.
In the result, it starts with a file name,
and after the file name, you see the matching line.
If you don't want to see the matching line,
and just the file name,
you can also consider using "grep -l".
Grep minus l is filtering out
all of this.
Right.
Another use is
"ps aux
| grep
http".
The use case?
I wanna know if my HTTP server is up and running,
so I'm using "ps aux", and what do we get?
Well, we get a result.
And the fact that we get a result
is maybe not what you want to occur.
You can see in the result "grep --color=auto http".
This is my grep command.
In case you are wondering
where does this "--color" come from?
Well, that's a shell setting that automatically
showing you colors in grep to make it more usable.
We'll talk about that later.
But the thing is, if I am checking
if HTTP is up or running, or not,
then I don't wanna see the grep command in the result.
I just wanna see whether or not HTTP is running.
So I want to exclude lines
that contain the text "grep",
"grep -v
grep".
And in case you are thinking,
"Hey Sanor, can't you read that for yourself,
that this is wrong?"
Of course I can read it,
but if you use it like this, then we can automate it.
I mean, you can write a shell script
and in that shell script, you can do something
if HTTP is not running.
And then really, you need the grep command
to produce the proper result.
Right, so more, "grep -A5
linda /etc/passwd",
which is showing us the five lines after.
Apparently there are no five lines after.
How about minus B5?.
There it is working, the five lines before.
So I guess we should be using "grep -A3"
on "tcpdump" in "/etc/passwd".
So "tcpdump", so that at least you can see,
that the lines after is working as well.
And how about we make that "C3"?
Then we see the lines before and the lines after.
Is that useful?
Well, not so very much in this case,
but if you are going to look for configuration files,
and in the configuration files
you want to see
the lines just before the line that you are looking for,
this can be really convenient.
For now, that's all I want to show you about grep.
But we will be grepping some more in the next lesson.