1 00:00:00,090 --> 00:00:01,430 Hello, the beautiful people. 2 00:00:01,440 --> 00:00:06,120 So in the last few videos you've been learning how to create and modify files and directories in all 3 00:00:06,120 --> 00:00:07,290 kinds of different ways. 4 00:00:07,290 --> 00:00:11,910 And when you're working with files in your computer, it's really easy to misplace one or two and wonder 5 00:00:11,910 --> 00:00:15,810 where an important file that's new to you such a configuration file might be hiding. 6 00:00:15,810 --> 00:00:20,640 So in this video, I'm going to show you how you can use the locate command to actually search for files 7 00:00:20,640 --> 00:00:25,890 by name and find them no matter where they might be hiding on your computer or with the command line. 8 00:00:25,890 --> 00:00:26,460 Pretty cool. 9 00:00:26,460 --> 00:00:26,730 Right? 10 00:00:26,730 --> 00:00:29,460 So we're going to break this up into two parts in this video. 11 00:00:29,460 --> 00:00:32,430 You're going to learn how to use the locate command and some of its options. 12 00:00:32,430 --> 00:00:38,640 And you're also going to appreciate some of the limitations that the locate command can have. 13 00:00:38,640 --> 00:00:42,870 And then in the next video, the one after this one, you're going to learn how to overcome those limitations 14 00:00:42,870 --> 00:00:49,680 and also how to access administrator privileges on the system in order to update a database. 15 00:00:49,680 --> 00:00:52,500 So this is all going to be very cool stuff, very, very exciting. 16 00:00:52,500 --> 00:00:54,060 It's going to give you super powers, basically. 17 00:00:54,060 --> 00:00:56,460 So let's just go ahead and get right into it. 18 00:00:58,700 --> 00:00:59,040 Okay. 19 00:00:59,060 --> 00:01:04,340 So the locate command is actually really simple to use, but it's very, very useful. 20 00:01:04,370 --> 00:01:08,420 Now, the first thing that you need to know about the locate command is that it works by searching a 21 00:01:08,420 --> 00:01:10,940 database file that's on your system. 22 00:01:11,120 --> 00:01:16,040 Now this database holds location information about every file that's on your system. 23 00:01:16,040 --> 00:01:20,570 And the way that the locate command works is that you will give it some pattern to look for and then 24 00:01:20,570 --> 00:01:26,450 locate will search the database for every path that matches that pattern and give it back to you on 25 00:01:26,450 --> 00:01:27,350 standard output. 26 00:01:27,380 --> 00:01:31,550 Now this is actually really easy, so let's just jump right in and get a feel for how this works. 27 00:01:31,580 --> 00:01:31,970 Okay. 28 00:01:31,970 --> 00:01:39,230 So for example, let's try and find every file in our system that ends in dot conf. 29 00:01:39,450 --> 00:01:39,630 Okay. 30 00:01:39,740 --> 00:01:46,670 Now dot conf is a file extension that is common on Linux to indicate that this file is an editable configuration 31 00:01:46,670 --> 00:01:47,090 file. 32 00:01:47,550 --> 00:01:52,790 And because file extensions don't really matter to Linux, Linux users and developers usually leave 33 00:01:52,790 --> 00:01:57,050 meaningful file extensions like dot com so that you can understand what the file might be about just 34 00:01:57,050 --> 00:01:58,610 from reading the file extension. 35 00:01:58,970 --> 00:02:04,010 Dot log is another common example and it's used for log files that store error messages from programs 36 00:02:04,010 --> 00:02:05,240 and server applications. 37 00:02:05,240 --> 00:02:10,100 So you can look at these log files later on in case anything breaks and see where stuff went wrong. 38 00:02:10,490 --> 00:02:16,700 But we're going to look at files that end in dot conf now to search for all the dot com files on our 39 00:02:16,700 --> 00:02:17,120 system. 40 00:02:17,120 --> 00:02:21,530 All we need to do is type the locate command and then give it the pattern we want to search for. 41 00:02:21,680 --> 00:02:25,550 Now you should start remembering what we did about regular expressions and wild cards. 42 00:02:25,550 --> 00:02:26,830 So just think for a minute. 43 00:02:26,840 --> 00:02:31,370 How would you make a pattern that matched any file that ended in dotcom? 44 00:02:31,430 --> 00:02:32,810 Just think about it for a second. 45 00:02:35,000 --> 00:02:40,210 And you might remember that perhaps you need to have the star wildcard and then com. 46 00:02:40,460 --> 00:02:45,500 This says anything that matches this pattern here and so that means it can start with anything and then 47 00:02:45,500 --> 00:02:46,340 end in dotcom. 48 00:02:46,940 --> 00:02:53,660 So this pattern will check for files that end in dot conf and that's actually all we need to do. 49 00:02:53,660 --> 00:02:53,900 Right. 50 00:02:53,900 --> 00:02:57,170 So if we do that and press enter. 51 00:02:58,360 --> 00:02:58,950 Whoa. 52 00:02:58,960 --> 00:02:59,170 Right. 53 00:02:59,170 --> 00:03:05,890 You'll see, like a whole bunch of different file paths come out, but you'll see that every single 54 00:03:05,890 --> 00:03:10,360 one of these files ends in dotcom. 55 00:03:11,230 --> 00:03:18,130 Now, it's worth noting that by default, Linux is case sensitive, but you can make searches using 56 00:03:18,130 --> 00:03:23,370 the locate command case insensitive by giving the locate command the AI option. 57 00:03:23,380 --> 00:03:33,370 So, for example, if we just clear the screen and we do locate and just say dot com, but spell everything 58 00:03:33,370 --> 00:03:40,750 in uppercase, now you'll see that we actually get no results because Linux searched case sensitively. 59 00:03:40,750 --> 00:03:49,630 It had to be exactly everything in uppercase but to make locate search in a case insensitive way, we 60 00:03:49,630 --> 00:03:52,570 could give the locate command the AI option. 61 00:03:52,570 --> 00:03:54,580 And what this does is say, hey, don't worry about the case. 62 00:03:54,580 --> 00:03:55,930 I'm just looking for these letters. 63 00:03:55,930 --> 00:04:01,420 And when I do that we get again files that end in dot conf. 64 00:04:01,750 --> 00:04:04,390 So that's something to bear in mind there. 65 00:04:04,390 --> 00:04:06,940 And you can also limit the amount of results that you get. 66 00:04:06,940 --> 00:04:09,190 So we get hundreds of results coming out of this one. 67 00:04:09,190 --> 00:04:09,640 Look how many. 68 00:04:09,640 --> 00:04:15,670 There are a lot of configuration files, but if we clear the screen, we could say, okay, let's search 69 00:04:15,670 --> 00:04:22,690 in a case insensitive way, but let's limit it, limit the output to just three results. 70 00:04:22,690 --> 00:04:23,020 Okay. 71 00:04:23,170 --> 00:04:28,060 And you can see here that I am using the long form limit option. 72 00:04:28,070 --> 00:04:31,900 So we've got the two dashes, then the word limit, and then we give it its command line argument, 73 00:04:31,900 --> 00:04:32,890 which is three. 74 00:04:32,890 --> 00:04:36,820 So this limit option has an input of three. 75 00:04:37,150 --> 00:04:42,670 So when I do that, you can see that we are just limiting it to three results. 76 00:04:42,670 --> 00:04:49,390 If I change that to five, we'll get five results for change that to ten, we will get ten results and 77 00:04:49,390 --> 00:04:50,110 so on and so on. 78 00:04:50,110 --> 00:04:55,870 So this is useful to if you just want to get the first few results or you only want to find a few results, 79 00:04:55,870 --> 00:04:57,670 you can limit it like that as well. 80 00:04:57,970 --> 00:05:03,850 Now the key consideration to have when using the locate command is to understand that it uses a database. 81 00:05:03,850 --> 00:05:08,800 In fact, you can actually find out information about the database using the capital s option. 82 00:05:08,800 --> 00:05:15,940 So if I click the screen and we use locate and give it the capital s the upper case option, I can press 83 00:05:15,940 --> 00:05:20,770 enter and you'll see that we get some information about the database itself. 84 00:05:20,770 --> 00:05:29,470 So it tells us the path to the database file as well as some size about the general, some information 85 00:05:29,470 --> 00:05:31,990 about the general size and complexity of the database. 86 00:05:32,440 --> 00:05:34,990 So I'm actually going to save this information using a redirect. 87 00:05:34,990 --> 00:05:42,310 So if I use locate s and I'm going to save that into something on our desktop, let's call it database 88 00:05:42,760 --> 00:05:44,440 before txt. 89 00:05:44,470 --> 00:05:50,590 So well we've got a file called database before that txt on our desktop with all that information saved 90 00:05:50,590 --> 00:05:51,100 in it. 91 00:05:51,970 --> 00:05:56,140 So you can see redirection is actually a very useful skill and you'll just use it all the time and you 92 00:05:56,140 --> 00:05:56,680 should be proud. 93 00:05:56,680 --> 00:05:59,590 Now you understand how to use it as if it's nothing, right? 94 00:06:00,280 --> 00:06:07,360 So this database, just like any of the database, is only useful if the information in it is up to 95 00:06:07,360 --> 00:06:07,870 date. 96 00:06:07,990 --> 00:06:08,370 Okay. 97 00:06:08,470 --> 00:06:12,520 Now the database by default is updated just once a day. 98 00:06:12,520 --> 00:06:16,810 So this means that anything that happens in the meantime is not logged in. 99 00:06:16,810 --> 00:06:23,200 The database files could have been created, moved, deleted, and unless the database is updated, 100 00:06:23,200 --> 00:06:27,100 then the locate command will start to give incorrect results. 101 00:06:27,400 --> 00:06:31,240 Now there are a few options that come with the locate command to help minimize this issue. 102 00:06:31,420 --> 00:06:36,130 They can't make the database list files that haven't been added to it yet because that's impossible. 103 00:06:36,130 --> 00:06:40,780 But they can prevent the locate command from showing files that have been deleted in the meantime or 104 00:06:40,780 --> 00:06:42,280 links that no longer work. 105 00:06:42,280 --> 00:06:48,340 For example, there is the option that requires the locate command to check that the file is existing 106 00:06:48,340 --> 00:06:49,810 before it reports it back. 107 00:06:49,810 --> 00:06:57,010 So for example, if we did locate with the e option of everything that ends in conf, then this would 108 00:06:57,010 --> 00:07:02,980 check the database for all files that ended in dotcom, but also do a check to make sure that those 109 00:07:02,980 --> 00:07:09,580 files actually existed before reporting them rather than just taking it from the database directly. 110 00:07:09,580 --> 00:07:13,360 It will take them from the database, but also do a check to make sure that they are there. 111 00:07:13,360 --> 00:07:15,760 So it gives you that extra bit of assurance. 112 00:07:15,760 --> 00:07:18,180 There is a long form option for the options. 113 00:07:18,180 --> 00:07:24,730 So instead of typing dash E, you could type double dash existing and that does exactly the same thing. 114 00:07:24,730 --> 00:07:26,170 So that's very, very useful stuff. 115 00:07:26,170 --> 00:07:32,770 And sometimes there are shortcuts and links on your system called symbolic links that might be broken 116 00:07:32,890 --> 00:07:38,140 if files get moved around now to check whether these shortcuts and links are still valid before the 117 00:07:38,140 --> 00:07:39,850 locate command reports them back. 118 00:07:39,850 --> 00:07:44,500 You can use the capital L or the follow option like this. 119 00:07:44,500 --> 00:07:48,550 So locate follow everything that ends in dot com for this. 120 00:07:48,550 --> 00:07:53,740 So if there are any symbolic links or if there are any links to files, it will check that they aren't 121 00:07:53,740 --> 00:07:54,460 just dangling. 122 00:07:54,460 --> 00:07:57,460 They actually arrive somewhere, arrive to the right place. 123 00:07:57,660 --> 00:07:59,050 Before reporting it back. 124 00:07:59,060 --> 00:08:00,560 So that's another bit of assurance. 125 00:08:00,560 --> 00:08:03,710 And of course you can do both at the same time. 126 00:08:03,710 --> 00:08:08,720 So existing and follow gives you basically the maximum assurance you can get there. 127 00:08:08,720 --> 00:08:14,300 And again, you can combine it with case in sensitivity and limit it to just show five. 128 00:08:14,300 --> 00:08:19,310 And you can combine all these options showing you here just how powerful the Linux command line can 129 00:08:19,310 --> 00:08:22,970 be to have multiple different functions going at the same time. 130 00:08:23,450 --> 00:08:28,100 Now, these safety features are all well and good, but what we really need to solve our problem is 131 00:08:28,100 --> 00:08:31,490 to have a way to update the database whenever we like. 132 00:08:31,880 --> 00:08:35,120 And there is it's called the update DB command. 133 00:08:35,120 --> 00:08:39,740 And for the purposes of time, we're going to cut the video here and pick up learning how to update 134 00:08:39,740 --> 00:08:41,360 the database in the next video. 135 00:08:41,360 --> 00:08:42,440 So I'll see you there.