1 00:00:00,090 --> 00:00:01,380 Hello, the beautiful people. 2 00:00:01,380 --> 00:00:02,260 And welcome back. 3 00:00:02,280 --> 00:00:07,560 Now in this video, we're going to be discussing one of the most powerful features of the find command 4 00:00:07,560 --> 00:00:09,900 and to be honest, of the command line in general. 5 00:00:09,900 --> 00:00:15,930 And that is how you can once you have gotten your big list of results from the fine command, how you 6 00:00:15,930 --> 00:00:20,430 can take those results and perform another command on each of those. 7 00:00:20,430 --> 00:00:22,080 This is an incredibly powerful ability. 8 00:00:22,140 --> 00:00:24,840 So I hope you're very, very, very excited. 9 00:00:24,840 --> 00:00:30,720 So you can see here that I'm on the desktop using the command and we're going to make a directory called 10 00:00:30,720 --> 00:00:32,910 copy copy here. 11 00:00:32,920 --> 00:00:33,490 Okay. 12 00:00:33,780 --> 00:00:38,580 Noticing that I'm having an underscore to represent the space, that's good practice, as I've mentioned 13 00:00:38,580 --> 00:00:41,070 to you before, to stop at command line messing up. 14 00:00:41,070 --> 00:00:41,490 Okay. 15 00:00:41,490 --> 00:00:49,560 So what we're going to do, okay, let's say that we want to search the entire file system for every 16 00:00:49,560 --> 00:00:56,940 file that is bigger than 100 kilobytes and less than five megabytes. 17 00:00:57,480 --> 00:01:02,850 So what I want you to do is I want you to just have a go at writing out that command. 18 00:01:02,850 --> 00:01:11,010 So how would you starting from the very base directory, find every single file on the system and that 19 00:01:11,010 --> 00:01:16,950 is greater than 100 megabytes and less than five megabytes. 20 00:01:17,010 --> 00:01:21,750 Okay, so pause the video now go have a go at it and then come back to see how you did. 21 00:01:27,180 --> 00:01:27,730 How'd you get on? 22 00:01:27,750 --> 00:01:28,530 Did you manage it? 23 00:01:28,560 --> 00:01:30,770 If you did, I'm so proud of you. 24 00:01:30,780 --> 00:01:35,010 That's like a technique that you're going to use so often when you work with the command line. 25 00:01:35,010 --> 00:01:38,270 I'm really, really proud that you've got that skill and if you haven't, practice makes perfect. 26 00:01:38,280 --> 00:01:42,990 Don't worry, you get plenty of practice as we as you go with your assignment later on. 27 00:01:42,990 --> 00:01:43,230 Okay. 28 00:01:43,230 --> 00:01:44,490 But this is how I'd go about it. 29 00:01:44,500 --> 00:01:44,860 Okay. 30 00:01:45,000 --> 00:01:48,570 I'd use the find command and we're starting at the base directory. 31 00:01:48,570 --> 00:01:53,940 So we're going to press, we're going to type a slash, we're going to specify that we want only files 32 00:01:54,240 --> 00:01:55,770 by using the type option. 33 00:01:55,800 --> 00:02:01,440 Then we're going to say the size has to be greater than than 100 kilobytes and the size has to be less 34 00:02:01,440 --> 00:02:02,880 than five megabytes. 35 00:02:02,910 --> 00:02:08,070 Now, because we're starting at the base directory here to make sure that our find command has the ability 36 00:02:08,070 --> 00:02:14,700 to go into every folder, we're going to prefix this entire command with a pseudo because otherwise 37 00:02:14,700 --> 00:02:17,790 we get permission, access errors and we don't want those. 38 00:02:18,090 --> 00:02:21,750 So let's just run the whole command as the administrator. 39 00:02:22,050 --> 00:02:22,650 Okay. 40 00:02:22,650 --> 00:02:32,010 So if we if we run that and we're going to find a big list of results, when I give in my my password, 41 00:02:32,220 --> 00:02:35,640 I'm going to find a massive list of results comes out. 42 00:02:35,670 --> 00:02:36,140 Okay. 43 00:02:36,150 --> 00:02:40,650 And by the way, when these things are running, you can actually cancel it from running any command 44 00:02:40,650 --> 00:02:45,240 on the command line by holding the control key and pressing C. 45 00:02:45,240 --> 00:02:45,510 Okay. 46 00:02:45,600 --> 00:02:48,840 And that will, that will stop the command running. 47 00:02:48,900 --> 00:02:49,350 Okay. 48 00:02:49,870 --> 00:02:53,790 If you know it's taken too long or you type something wrong and you realize it and you don't want to 49 00:02:53,790 --> 00:02:55,440 have to wait until the thing finishes. 50 00:02:55,650 --> 00:03:01,590 But we're getting a whole bunch of results and actually we can tell you how many there are when it let's 51 00:03:01,590 --> 00:03:03,750 see, see by piping it into the word count command. 52 00:03:03,750 --> 00:03:06,240 And you can see we've got 3031 results. 53 00:03:06,240 --> 00:03:07,530 So that's quite a lot of results. 54 00:03:07,530 --> 00:03:07,920 Okay. 55 00:03:08,070 --> 00:03:16,290 Now, let's say that we wanted to copy every single one of those files, every single one of those 3031 56 00:03:16,290 --> 00:03:21,120 results that matched our find commands criteria to this copy here folder. 57 00:03:21,450 --> 00:03:23,280 How would we do that? 58 00:03:23,280 --> 00:03:26,970 Well, the way that we would do that is using another option. 59 00:03:26,970 --> 00:03:34,050 If I clear the screen, using another option for the find command called the exec option. 60 00:03:34,050 --> 00:03:36,210 Now the exec option stands for execute. 61 00:03:36,210 --> 00:03:40,260 And what I'm going to do is I'm actually going to maximize the screen to make this a bit easier to see. 62 00:03:40,260 --> 00:03:47,190 So we're saying, okay, once you've done this search here on each result, execute something. 63 00:03:48,050 --> 00:03:51,710 Now here you just type the command as you normally would. 64 00:03:51,710 --> 00:03:58,010 So we'd we'd say copy because we're going to use the CP command to copy, which is, you know, like 65 00:03:58,010 --> 00:03:58,730 we normally would. 66 00:03:58,760 --> 00:04:04,700 Now if you remember how the copy command works first, what you do is you say what you want to copy 67 00:04:04,700 --> 00:04:06,230 and then you say Where. 68 00:04:06,290 --> 00:04:06,800 Okay. 69 00:04:06,800 --> 00:04:09,800 So you say what you want to copy and then you say where. 70 00:04:09,830 --> 00:04:16,940 Well, we know where we want to copy it, which is going to be on our desktop and it's going to be in 71 00:04:16,940 --> 00:04:18,740 the copy here folder. 72 00:04:18,740 --> 00:04:24,410 So that's our path to the copy here folder, slash home, slash, z ad slash desktop slash copy here. 73 00:04:24,410 --> 00:04:27,620 We can write that a bit shorter by just putting the tilde there as well. 74 00:04:27,620 --> 00:04:28,480 That would be fine. 75 00:04:28,550 --> 00:04:29,040 Okay. 76 00:04:29,510 --> 00:04:31,290 But what do we want to copy? 77 00:04:31,310 --> 00:04:34,280 Well, we want to copy every single one of those files. 78 00:04:34,280 --> 00:04:39,740 And the way that you represent that is by using a pair of square squiggly brackets. 79 00:04:40,160 --> 00:04:49,910 So you type some squiggly brackets and that what fills that space is every single one of those files. 80 00:04:50,120 --> 00:04:55,520 So in turn it'll say Copy whatever file one was to this directory. 81 00:04:55,520 --> 00:04:59,810 Then it will copy file two to this directory and copy file three to this directory. 82 00:04:59,810 --> 00:05:02,780 And so we've got all 3000 results. 83 00:05:02,810 --> 00:05:07,820 Now, when you build out these these expressions, you can have multiple of these if you needed to in 84 00:05:07,820 --> 00:05:09,560 your command for whatever reason. 85 00:05:09,560 --> 00:05:15,650 But once you have created it like we have, so we're saying, okay, find every file that's greater 86 00:05:15,650 --> 00:05:18,140 than 100 kilobytes and less than five megabytes. 87 00:05:18,140 --> 00:05:24,110 And once you found all of those, execute the copy command on it and copy it to our copy here directory 88 00:05:24,110 --> 00:05:25,610 on our desktop. 89 00:05:25,610 --> 00:05:30,770 And then once you've finished all of that to end the command, you don't just press enter, you've got 90 00:05:30,770 --> 00:05:34,220 to tell it when you finished your execute option. 91 00:05:34,220 --> 00:05:38,930 And the way you do that is by pressing a backslash and then a semicolon. 92 00:05:38,930 --> 00:05:44,870 Okay, so you see the backslash and the semicolon and that is the command completely finished. 93 00:05:45,380 --> 00:05:55,130 So if I go ahead and run that and we wait just a little while until our shell prompt comes back, what 94 00:05:55,130 --> 00:06:03,120 it is doing is it is copying and pasting every single one of those options into our copy here folder. 95 00:06:03,650 --> 00:06:11,120 So I guess if I stop that for now and then close this and go to the copy here folder. 96 00:06:12,670 --> 00:06:13,540 There we go. 97 00:06:13,570 --> 00:06:21,550 You'll see that we actually have every single one of those files copied and pasted into our copy here 98 00:06:21,550 --> 00:06:24,640 directory, or at least as many as it did before I stopped running the command. 99 00:06:24,640 --> 00:06:29,500 So it's not awesome, but we've managed to just use the find command to not only find what we want on 100 00:06:29,500 --> 00:06:34,240 the whole system, but also execute a command on each of the results. 101 00:06:34,780 --> 00:06:40,030 So just as an aside, depending upon what type of hardware that you're running, running this command 102 00:06:40,030 --> 00:06:43,570 in its entirety might give your computer a bit of trouble. 103 00:06:43,780 --> 00:06:48,640 So if you're finding that your virtual machine crashes, just try and run the whole command to just 104 00:06:48,640 --> 00:06:53,290 a maximum depth of four to save it, you know, finding every file on your file system, this should 105 00:06:53,290 --> 00:06:59,230 reduce it quite significantly the load on your computer anyway or try and maximum depth of three and 106 00:06:59,230 --> 00:07:03,610 that should that should you know stop any problems if you find your virtual machine is crashing. 107 00:07:04,000 --> 00:07:08,560 But first of all, what I want you to do at the minute is just take a look at this command. 108 00:07:09,400 --> 00:07:10,720 Isn't that awesome? 109 00:07:10,720 --> 00:07:15,640 I mean, considering that you haven't you maybe didn't know anything about the Linux command line just 110 00:07:15,640 --> 00:07:16,960 a few videos ago. 111 00:07:16,990 --> 00:07:22,270 Now you're looking at this command, you're probably thinking, wow, if I actually come that far and 112 00:07:22,270 --> 00:07:26,560 this is the thing, the Linux command line isn't like, you know, unbelievably complicated. 113 00:07:26,560 --> 00:07:31,990 It's just a few concepts that if they're explained to you badly or you don't quite understand what they 114 00:07:31,990 --> 00:07:34,300 mean, it can be an absolute nightmare. 115 00:07:34,300 --> 00:07:39,160 But once you understand them, it's very easy to take, you know, just a few building blocks here and 116 00:07:39,160 --> 00:07:41,380 build something incredibly powerful. 117 00:07:41,530 --> 00:07:47,140 So here you found a way to search for every file in your system in between 100 kilobytes and five megabytes 118 00:07:47,140 --> 00:07:51,520 in size, and copy them all to a specific folder in just a command. 119 00:07:51,520 --> 00:07:52,900 And yet now you know how to do that. 120 00:07:52,900 --> 00:07:54,010 So I think that's amazing. 121 00:07:54,010 --> 00:07:58,540 And I really just wanted to pause in this video just to say I'm very, very proud of you for making 122 00:07:58,540 --> 00:07:59,950 it this far. 123 00:07:59,950 --> 00:08:05,410 And you're going to get many more congratulations on this in the conclusion section four for this section 124 00:08:05,410 --> 00:08:06,130 of the course. 125 00:08:06,130 --> 00:08:10,360 Now, what I want to do is I want to move on just a little bit to talk about this exec option here. 126 00:08:11,440 --> 00:08:13,540 This this exec option is awesome. 127 00:08:13,780 --> 00:08:17,410 It allows us to execute a command, but it's not the safest of all things. 128 00:08:17,680 --> 00:08:27,880 So if what you can do if is instead of using the exact option, you can actually type the OC option. 129 00:08:28,360 --> 00:08:31,090 And what this will do is it'll do exactly the same. 130 00:08:31,090 --> 00:08:32,169 It works exactly the same. 131 00:08:32,169 --> 00:08:33,490 You don't have to change anything. 132 00:08:33,490 --> 00:08:36,909 But instead of just doing the action directly. 133 00:08:36,909 --> 00:08:44,380 So if I, if I try this, it will now ask me every time that it tries to copy something, whether it's 134 00:08:44,380 --> 00:08:45,130 OC or not. 135 00:08:45,130 --> 00:08:47,290 So if I type Y, it'll continue. 136 00:08:47,290 --> 00:08:48,730 And if I type Y, it'll continue. 137 00:08:48,730 --> 00:08:50,950 If I type, no, it won't, and so on. 138 00:08:50,950 --> 00:08:54,340 So I have a choice here about what it does for each file. 139 00:08:54,340 --> 00:09:00,160 Now, this would clearly be a massive pain if you had thousands of files, but it's a good thing to 140 00:09:00,160 --> 00:09:05,650 bear in mind if you are doing something for a few files and you're doing something destructive, like 141 00:09:05,650 --> 00:09:10,940 deleting or moving files instead of just copying, like in this case, and you want to make sure, sure 142 00:09:10,990 --> 00:09:17,530 that nothing goes wrong, you can use the OC option instead of the exact option in order to do exactly 143 00:09:17,530 --> 00:09:18,010 the same stuff. 144 00:09:18,010 --> 00:09:20,590 So the OC option is exactly the same as the execute option. 145 00:09:20,590 --> 00:09:23,530 You don't have to change anything else from, from here on. 146 00:09:23,530 --> 00:09:29,590 Just, just bear in mind that, you know, it's, it's, it's a bit safer than using the exact option, 147 00:09:29,590 --> 00:09:34,240 but it, you know, it's a massive pain if you've got thousands of files to do so. 148 00:09:34,240 --> 00:09:39,010 As I was saying, I'm really, really proud of you for getting as far as you have in this section, 149 00:09:39,010 --> 00:09:39,910 in this video. 150 00:09:40,150 --> 00:09:45,310 You could, by the way, you can cancel your way out of the OC option by pressing control and c like 151 00:09:45,310 --> 00:09:51,340 you see I've just done here and of course you can clear the screen by pressing control and l but what 152 00:09:51,340 --> 00:09:54,860 I want to do in the next video is we're going to take this to the extreme, okay? 153 00:09:54,910 --> 00:10:00,760 We're going to really, really show you how powerful the fine command can be by doing a little finding 154 00:10:00,760 --> 00:10:03,070 the needle in the haystack kind of activity. 155 00:10:03,070 --> 00:10:09,190 In the next video, we're going to create a folder called Haystack that contains 500 other folders. 156 00:10:09,190 --> 00:10:12,970 And in each of those folders, we're going to make another 100 files. 157 00:10:12,970 --> 00:10:17,710 And this will be very easy using our knowledge of and knowledge of a brace expansion. 158 00:10:18,370 --> 00:10:25,420 But inside one of those folders at random, we're going to create a file called Needle Dot txt, and 159 00:10:25,420 --> 00:10:30,250 then we're going to use the find command to find the needle in the haystack and move it into our home 160 00:10:30,250 --> 00:10:30,850 directory. 161 00:10:30,850 --> 00:10:31,220 Okay. 162 00:10:31,240 --> 00:10:37,510 So out of about 50,000 possible files, we're going to find that one and copy it exactly where we need. 163 00:10:37,510 --> 00:10:39,190 And it's going to be so easy to do. 164 00:10:39,220 --> 00:10:40,060 You won't believe it. 165 00:10:40,090 --> 00:10:40,270 Okay. 166 00:10:40,330 --> 00:10:44,230 So what we're going to do is we're going to shift over into the next video and I'll see you there to 167 00:10:44,230 --> 00:10:45,970 find the needle in the haystack.