1
00:00:06,690 --> 00:00:07,679
Let's talk
about grep.

2
00:00:08,550 --> 00:00:11,320
Grep is one of the
most important utilities on Linux.

3
00:00:11,919 --> 00:00:14,269
We have already seen it.
That's how important it is.

4
00:00:15,330 --> 00:00:18,905
So grep is used to
search text strings or regular

5
00:00:18,905 --> 00:00:22,480
expressions in files, or using
a pipe in command output.

6
00:00:23,219 --> 00:00:28,510
A regular expression is an advanced text pattern. We
will talk about it in more detail in lesson seven.

7
00:00:29,750 --> 00:00:34,140
So grab is one of the most
important tools on Linux because it's always about

8
00:00:34,140 --> 00:00:38,530
vals, it's always about output and how
to get the relevant information out of it.

9
00:00:39,710 --> 00:00:44,836
Couple of examples. Grab lynda searches
attacks Linda in all files in

10
00:00:44,836 --> 00:00:49,963
the current Directory or PSAUX Pipe
GrabHDP uses a pipe to show

11
00:00:49,963 --> 00:00:55,090
all lines that contain the text
HTTP in the output of ps.

12
00:00:56,560 --> 00:01:00,230
Very important for advanced use
of grep. Regular expresses can be

13
00:01:00,230 --> 00:01:03,900
used to match file patterns.
More about that in lesson seven.

14
00:01:04,840 --> 00:01:10,060
Now grep has a couple of options
that can be useful, like I for ignore

15
00:01:10,060 --> 00:01:15,280
case V which will show all lines
that do not contain the selected pattern.

16
00:01:16,239 --> 00:01:19,819
L to list file names that
contain the pattern without showing matching lines.

17
00:01:20,560 --> 00:01:26,159
A5, which shows lines that match
the pattern pattern as well as five

18
00:01:26,159 --> 00:01:31,759
lines after and B5, which shows
lines that matches the pattern as well

19
00:01:31,759 --> 00:01:37,359
as five lines before. You can
also combine A and B by using

20
00:01:37,359 --> 00:01:42,959
C or R, which is useful
to recursively search for a specific pattern.

21
00:01:43,959 --> 00:01:45,439
Let's do a
quick grep demo.

22
00:01:46,799 --> 00:01:53,659
So I'm starting with psaox. Psaox is showing
all the processes currently running and I want

23
00:01:53,659 --> 00:02:00,519
to know if SSH is running. So I'm
using grep ssh and what do we see?

24
00:02:01,340 --> 00:02:06,504
We see information about the SSHD process, which
happens to be what I was looking for.

25
00:02:06,504 --> 00:02:11,669
We also see stuff that I don't want
to see, and that's the grep command itself.

26
00:02:12,349 --> 00:02:17,213
And you know, if I'm going
to add grep v grep that

27
00:02:17,213 --> 00:02:22,076
means show me the output. But
from the output, remove all lines

28
00:02:22,076 --> 00:02:26,939
that have the text grep and
that is showing the clean result.

29
00:02:27,979 --> 00:02:34,006
Next let's do a grab lynda on etc
which is showing all files in etc containing

30
00:02:34,006 --> 00:02:40,033
the text Linda, maybe we want to do
that with a sudo in front of it

31
00:02:40,033 --> 00:02:46,060
so that we can see all files
and oh boy, a lot of directories.

32
00:02:47,020 --> 00:02:51,805
Well, if we don't want to struggle
with the directories R so that we

33
00:02:51,805 --> 00:02:56,590
can see files in etc and its
subdirectories. Now we have a clean result.

34
00:02:57,409 --> 00:03:01,105
As you can see, in the
result we have the name of the

35
00:03:01,105 --> 00:03:04,800
file followed by the line that
contains the text. We were looking for.

36
00:03:05,460 --> 00:03:12,409
If that's not what you want, use l
with the command that only will list the files.

37
00:03:13,430 --> 00:03:17,245
Now next, you might
be interested in the after

38
00:03:17,245 --> 00:03:21,060
and before options. Let
me show you psfaux.

39
00:03:21,560 --> 00:03:25,219
Psfaux is showing
a process forest.

40
00:03:25,840 --> 00:03:31,949
So let's say that in P.S.
faux I'm interested in the line

41
00:03:31,949 --> 00:03:38,060
faux, but I want to see
that including the five lines before.

42
00:03:38,939 --> 00:03:45,240
And that's because in process management
there is a hierarchy of processes.

43
00:03:46,159 --> 00:03:51,120
A command is always a child of some other
process which is a child of some other process.

44
00:03:51,740 --> 00:03:57,030
By using this b we can see
the lines before and the lines before

45
00:03:57,030 --> 00:04:02,319
make sense in hierarchical tree output.
You can also use a for after.

46
00:04:03,020 --> 00:04:06,740
That's more common if
you are looking for headerfiles.

47
00:04:07,819 --> 00:04:12,639
So let me do a sudo. I'm not sure
if it needs sudo, but let's do it anyway.

48
00:04:13,479 --> 00:04:18,764
Sudo grep a5
root on etc sshdconfig

49
00:04:18,764 --> 00:04:24,050
and there we
can see. Aha.

50
00:04:24,730 --> 00:04:30,149
We have the root directory and that's
a five lines following. That's a little

51
00:04:30,149 --> 00:04:35,569
bit disappointing, but maybe that's because I
haven't included minus I for case insensitive.

52
00:04:36,089 --> 00:04:37,990
Yeah, this is what
I was more expecting.

53
00:04:38,610 --> 00:04:43,833
Permit root login with an uppercase r.
If you don't want to have any

54
00:04:43,833 --> 00:04:49,056
surprises because of case, use grep I
to make it case insensitive so that

55
00:04:49,056 --> 00:04:54,279
it matches lower as well as
uppercase and any mix of that.

56
00:04:54,819 --> 00:04:58,699
That's all for now. We'll get back
to grep when we talk about regular expressions.
