1
00:00:00,360 --> 00:00:01,410
In this lesson,

2
00:00:01,410 --> 00:00:03,450
we're going to discuss the tools that you can use

3
00:00:03,450 --> 00:00:05,670
to search for files or applications

4
00:00:05,670 --> 00:00:09,180
when you don't know their exact location on a Linux system.

5
00:00:09,180 --> 00:00:11,520
The first one is the locate command.

6
00:00:11,520 --> 00:00:13,680
The locate command performs a quick search

7
00:00:13,680 --> 00:00:16,680
for any specified string, inside file names,

8
00:00:16,680 --> 00:00:20,400
and path stored inside the mlocate database.

9
00:00:20,400 --> 00:00:23,250
Now this database must be updated regularly

10
00:00:23,250 --> 00:00:25,290
for the search to be really effective.

11
00:00:25,290 --> 00:00:27,720
The results displayed may be restricted to files

12
00:00:27,720 --> 00:00:30,660
that the user has permissions to access or execute, though,

13
00:00:30,660 --> 00:00:32,310
so keep that in mind.

14
00:00:32,310 --> 00:00:34,500
To use this, just type in locate,

15
00:00:34,500 --> 00:00:37,110
the options, and the string you want to search for.

16
00:00:37,110 --> 00:00:39,420
The locate command supports different options

17
00:00:39,420 --> 00:00:42,090
that enable you to make your searches more effective.

18
00:00:42,090 --> 00:00:45,270
For example, you can use the -r option to search

19
00:00:45,270 --> 00:00:47,940
for file names using regular expressions.

20
00:00:47,940 --> 00:00:49,800
If you use the -c option,

21
00:00:49,800 --> 00:00:52,620
you can display only the number of matching entries found

22
00:00:52,620 --> 00:00:54,810
instead of showing the actual file names.

23
00:00:54,810 --> 00:00:57,240
So for example, if I wanted to find all the files

24
00:00:57,240 --> 00:00:59,160
that had the word JSON in them,

25
00:00:59,160 --> 00:01:03,600
I can use -c and get a result that says there are 543 files

26
00:01:03,600 --> 00:01:05,610
with the name JSON in them.

27
00:01:05,610 --> 00:01:08,220
Also, you can use -e.

28
00:01:08,220 --> 00:01:10,350
This is going to return only files that exist

29
00:01:10,350 --> 00:01:11,850
at the time of the search

30
00:01:11,850 --> 00:01:14,280
and not things that were previously deleted.

31
00:01:14,280 --> 00:01:15,930
If you use -i,

32
00:01:15,930 --> 00:01:17,610
it's going to be used to ignore the casing

33
00:01:17,610 --> 00:01:19,590
in a file name or path.

34
00:01:19,590 --> 00:01:22,680
And if you use --n and the number of entries,

35
00:01:22,680 --> 00:01:25,350
it's going to be used to return only the first few matches

36
00:01:25,350 --> 00:01:27,030
up to the specified number.

37
00:01:27,030 --> 00:01:28,260
So for the example of JSON,

38
00:01:28,260 --> 00:01:30,330
I said there was 500 and something files,

39
00:01:30,330 --> 00:01:31,980
but I only want to show the first three,

40
00:01:31,980 --> 00:01:34,500
I could use --n 3

41
00:01:34,500 --> 00:01:36,960
and only see the first three that are found.

42
00:01:36,960 --> 00:01:38,970
If you want to update that database,

43
00:01:38,970 --> 00:01:42,120
you do this by using the updatedb command.

44
00:01:42,120 --> 00:01:44,760
The updatedb command is used to build a database

45
00:01:44,760 --> 00:01:47,907
of files based on the /etc/updatedb.conf file.

46
00:01:50,640 --> 00:01:51,960
This command is used to update

47
00:01:51,960 --> 00:01:56,960
the /var/lib/mlocate/mlocate.db database file.

48
00:01:59,070 --> 00:02:02,580
The /etc/update database config file consists

49
00:02:02,580 --> 00:02:04,230
of the path that should be excluded

50
00:02:04,230 --> 00:02:06,270
when building up your database.

51
00:02:06,270 --> 00:02:08,039
To add a path that needs to be excluded

52
00:02:08,039 --> 00:02:09,419
when building the database,

53
00:02:09,419 --> 00:02:14,130
simply open up the /etc/updatedb.conf file,

54
00:02:14,130 --> 00:02:16,560
and in the PRUNEPATH variable,

55
00:02:16,560 --> 00:02:19,680
specify which paths need to not be included

56
00:02:19,680 --> 00:02:21,750
when you're building this database.

57
00:02:21,750 --> 00:02:26,750
For example, if I use PRUNEPATH="/etc",

58
00:02:27,180 --> 00:02:29,610
this would exclude the etc directory

59
00:02:29,610 --> 00:02:31,650
when building my database.

60
00:02:31,650 --> 00:02:33,960
Another command you can use to search is known

61
00:02:33,960 --> 00:02:35,490
as the find command.

62
00:02:35,490 --> 00:02:38,490
The find command enables you to search a specific location

63
00:02:38,490 --> 00:02:40,290
for files and directories that adhere

64
00:02:40,290 --> 00:02:42,690
to some specific search criteria.

65
00:02:42,690 --> 00:02:44,730
It's going to be used recursively to do searching

66
00:02:44,730 --> 00:02:46,350
throughout the directory structure,

67
00:02:46,350 --> 00:02:49,050
including any subdirectories and their contents,

68
00:02:49,050 --> 00:02:51,810
beginning with the search location that you enter.

69
00:02:51,810 --> 00:02:53,580
You can perform one or more actions

70
00:02:53,580 --> 00:02:55,470
on the files that are found.

71
00:02:55,470 --> 00:02:57,660
If you use the -type option,

72
00:02:57,660 --> 00:02:58,920
this enables you to specify

73
00:02:58,920 --> 00:03:00,780
the type of object you're looking for,

74
00:03:00,780 --> 00:03:04,050
such as d for directory or f for file.

75
00:03:04,050 --> 00:03:07,200
If you use the -name option, this is where you specify

76
00:03:07,200 --> 00:03:09,450
the name of the object you're looking for.

77
00:03:09,450 --> 00:03:13,110
So you might enter something like find, the options,

78
00:03:13,110 --> 00:03:15,390
the search locations, the search criteria,

79
00:03:15,390 --> 00:03:18,360
and the actions to be able to run this command.

80
00:03:18,360 --> 00:03:20,460
For example, let's say you want to search

81
00:03:20,460 --> 00:03:21,990
for a user's home directory

82
00:03:21,990 --> 00:03:25,980
for all the files named 2019_report.

83
00:03:25,980 --> 00:03:27,300
I can do that by typing

84
00:03:27,300 --> 00:03:32,300
in find /home/user -type f -name 2019_report.

85
00:03:36,060 --> 00:03:38,700
Now I'm going to be able to find all those files

86
00:03:38,700 --> 00:03:40,590
that are called 2019 report

87
00:03:40,590 --> 00:03:43,260
underneath that home user's directory.

88
00:03:43,260 --> 00:03:45,330
Now you may be wondering what's the difference

89
00:03:45,330 --> 00:03:48,510
between using the locate command and the find command?

90
00:03:48,510 --> 00:03:49,530
Well, in general,

91
00:03:49,530 --> 00:03:52,350
the locate command will give you an answer much more quickly

92
00:03:52,350 --> 00:03:54,240
because it's searching a database,

93
00:03:54,240 --> 00:03:56,430
but it's only going to retrieve information

94
00:03:56,430 --> 00:04:00,450
on files that are present on your system in that database.

95
00:04:00,450 --> 00:04:02,490
Now, if you don't keep that database updated,

96
00:04:02,490 --> 00:04:04,050
you're going to get outdated results,

97
00:04:04,050 --> 00:04:06,360
and those files may no longer exist.

98
00:04:06,360 --> 00:04:07,350
On the other hand,

99
00:04:07,350 --> 00:04:10,050
the find command is going to perform a live search

100
00:04:10,050 --> 00:04:11,340
of your file system,

101
00:04:11,340 --> 00:04:14,370
and it can be concentrated on a specific location.

102
00:04:14,370 --> 00:04:17,760
So the find command may take more time to complete a search

103
00:04:17,760 --> 00:04:19,290
than your locate command.

104
00:04:19,290 --> 00:04:21,209
But when the system finds a listing

105
00:04:21,209 --> 00:04:22,650
that meets your criteria,

106
00:04:22,650 --> 00:04:24,660
those things are really on your system

107
00:04:24,660 --> 00:04:26,370
if you're using the find command,

108
00:04:26,370 --> 00:04:29,550
and then you can take actions on those results.

109
00:04:29,550 --> 00:04:32,520
For example, I might use -print.

110
00:04:32,520 --> 00:04:33,870
This would display the location

111
00:04:33,870 --> 00:04:35,670
of those files that were found.

112
00:04:35,670 --> 00:04:38,970
I can type -exec which will execute the command

113
00:04:38,970 --> 00:04:42,000
that follows that prompt on those files.

114
00:04:42,000 --> 00:04:44,760
I can type -ok which executes the command

115
00:04:44,760 --> 00:04:46,650
that follows interactively.

116
00:04:46,650 --> 00:04:48,690
I might use something like -delete

117
00:04:48,690 --> 00:04:50,880
which will delete those files that I found.

118
00:04:50,880 --> 00:04:53,880
Or I can use the option -fprint

119
00:04:53,880 --> 00:04:56,820
which stores the results in a target file.

120
00:04:56,820 --> 00:04:59,370
Now, in some cases, you may need help to locate

121
00:04:59,370 --> 00:05:02,040
where a program has already been installed on your system,

122
00:05:02,040 --> 00:05:03,930
for example, if you want to modify it

123
00:05:03,930 --> 00:05:06,150
or identify which version of a command you're using

124
00:05:06,150 --> 00:05:08,880
if there's multiple binaries of that command stored

125
00:05:08,880 --> 00:05:10,830
in different locations on your system.

126
00:05:10,830 --> 00:05:13,920
And to do that, you can use the which command.

127
00:05:13,920 --> 00:05:16,650
The which command is going to display the complete path

128
00:05:16,650 --> 00:05:18,090
of a specified command

129
00:05:18,090 --> 00:05:21,450
by searching the directories assigned to the PATH variable.

130
00:05:21,450 --> 00:05:24,510
For example, if I entered which cat,

131
00:05:24,510 --> 00:05:28,320
I'm going to get the output that says /bin/cat.

132
00:05:28,320 --> 00:05:32,130
This way, I know where the cat command is actually located,

133
00:05:32,130 --> 00:05:34,740
in this case, the /bin directory.

134
00:05:34,740 --> 00:05:37,560
If you want to use this, just type in the word which,

135
00:05:37,560 --> 00:05:38,790
the options you want,

136
00:05:38,790 --> 00:05:41,220
and the program name you want to search for.

137
00:05:41,220 --> 00:05:44,250
Now another command you can use is known as whereis.

138
00:05:44,250 --> 00:05:45,660
The whereis command is used

139
00:05:45,660 --> 00:05:48,930
to display various details associated with the command.

140
00:05:48,930 --> 00:05:53,190
For example, if I entered whereis ls and I hit Enter,

141
00:05:53,190 --> 00:05:55,110
I'm going to get something that looks like this.

142
00:05:55,110 --> 00:05:59,316
It says ls: /bin/ls

143
00:05:59,316 --> 00:06:04,316
/usr/share/man/man1/ls.1.gz

144
00:06:05,503 --> 00:06:10,343
/usr/share/man/man1p/ls.1p.gz,

145
00:06:12,270 --> 00:06:16,860
where /bin/ls indicates the location of the ls command,

146
00:06:16,860 --> 00:06:19,230
in this case, bin/ls.

147
00:06:19,230 --> 00:06:24,187
The /usr/share/man/man1/ls.1.gz

148
00:06:25,260 --> 00:06:27,920
and /usr/share/man

149
00:06:27,920 --> 00:06:32,920
/man1p/ls.1p.gz indicates the location

150
00:06:33,690 --> 00:06:36,570
of the man pages for the ls command.

151
00:06:36,570 --> 00:06:37,710
Now the proper way to run

152
00:06:37,710 --> 00:06:40,200
the whereis command is to type whereis,

153
00:06:40,200 --> 00:06:43,320
the options, and the directory name, and the file name.

154
00:06:43,320 --> 00:06:44,610
Now the whereis command has

155
00:06:44,610 --> 00:06:46,800
several different options you can choose from.

156
00:06:46,800 --> 00:06:50,970
For example, you can use -b to search only for binaries,

157
00:06:50,970 --> 00:06:53,700
m to search only for manual sections,

158
00:06:53,700 --> 00:06:55,710
s to search only for sources,

159
00:06:55,710 --> 00:06:58,680
and -o to search for unusual entries.

160
00:06:58,680 --> 00:07:00,900
So as you can see, there are many different ways

161
00:07:00,900 --> 00:07:03,300
to search for information on your systems,

162
00:07:03,300 --> 00:07:06,300
including the locate command, the find command,

163
00:07:06,300 --> 00:07:08,643
the which command, and the whereis command.

