1 00:00:06,520 --> 00:00:07,590 - In the previous video, 2 00:00:07,590 --> 00:00:09,720 you have learned how to work with find, 3 00:00:09,720 --> 00:00:13,530 and you have seen that find is pretty powerful. 4 00:00:13,530 --> 00:00:17,070 If you think that's all, well, I have a few more commands 5 00:00:17,070 --> 00:00:19,170 that I would like to discuss. 6 00:00:19,170 --> 00:00:21,870 For your convenience, I put them on the slide right here, 7 00:00:21,870 --> 00:00:23,700 and I'm going to run them for you. 8 00:00:23,700 --> 00:00:25,260 Oh, and just a tip, 9 00:00:25,260 --> 00:00:27,960 you better run them with sudo privileges, 10 00:00:27,960 --> 00:00:30,480 at least most of them. 11 00:00:30,480 --> 00:00:31,313 Let's go. 12 00:00:33,720 --> 00:00:38,203 So let me start with sudo find / 13 00:00:40,320 --> 00:00:41,550 minus type f. 14 00:00:41,550 --> 00:00:46,140 Minus type f is looking for items that are of the type f, 15 00:00:46,140 --> 00:00:48,660 which is ordinary files. 16 00:00:48,660 --> 00:00:53,660 Minus size, +100M, which is showing files with a size 17 00:00:53,790 --> 00:00:55,860 bigger than 100M. 18 00:00:55,860 --> 00:00:57,150 Only files. 19 00:00:57,150 --> 00:01:00,900 The thing here is that if in find you are going to look 20 00:01:00,900 --> 00:01:04,650 for specific items, you don't want to see any directories 21 00:01:04,650 --> 00:01:08,220 or whatever, so this is looking for files only. 22 00:01:08,220 --> 00:01:10,360 Second example is sudo find 23 00:01:14,146 --> 00:01:17,292 /etc -exec 24 00:01:17,292 --> 00:01:20,193 grep -l student. 25 00:01:22,650 --> 00:01:24,840 Based on what we have done before, you should be able 26 00:01:24,840 --> 00:01:28,323 to identify what this is doing. 27 00:01:29,550 --> 00:01:32,790 This is looking for files in /etc, and in these files, 28 00:01:32,790 --> 00:01:35,220 it will use grep -l student, 29 00:01:35,220 --> 00:01:37,590 which means that it is looking in the file contents 30 00:01:37,590 --> 00:01:40,020 to see if the text student occurs 31 00:01:40,020 --> 00:01:41,790 somewhere in the file contents. 32 00:01:41,790 --> 00:01:44,550 And next I want to run a second -exec, 33 00:01:44,550 --> 00:01:46,890 and yes, that is something that you can do. 34 00:01:46,890 --> 00:01:51,540 Second -exec cp, we are going to copy these files. 35 00:01:51,540 --> 00:01:56,540 Curly braces to, well, let's copy it to /root/linda 36 00:01:56,850 --> 00:02:01,050 because that's an existing directory, backslash semicolon. 37 00:02:01,050 --> 00:02:04,200 The point here is that I wanna show you 38 00:02:04,200 --> 00:02:06,915 that you can use multiple exec statements 39 00:02:06,915 --> 00:02:08,793 on one find command. 40 00:02:10,860 --> 00:02:13,320 And this is how you do it. 41 00:02:13,320 --> 00:02:17,730 Now the third one is find /etc 42 00:02:17,730 --> 00:02:19,980 minus name, star. 43 00:02:19,980 --> 00:02:23,850 I wanna find all files, -type f. 44 00:02:23,850 --> 00:02:26,550 Notice that you might as well drop -name star 45 00:02:26,550 --> 00:02:30,180 because if you dropped it, then it'll find all files also. 46 00:02:30,180 --> 00:02:32,401 Then I'm going to use xargs. 47 00:02:32,401 --> 00:02:35,460 Xargs is an alternative way 48 00:02:35,460 --> 00:02:38,460 for sending the output of the find command 49 00:02:38,460 --> 00:02:39,840 to another command. 50 00:02:39,840 --> 00:02:42,120 And the xargs this time is going to look 51 00:02:42,120 --> 00:02:47,120 for the occurrence of the text grep 127.0.0.1. 52 00:02:47,497 --> 00:02:49,200 And there we go. 53 00:02:49,200 --> 00:02:51,900 And oh, too many error messages. 54 00:02:51,900 --> 00:02:56,340 Let me use 2>/dev/null again so that we don't have to look 55 00:02:56,340 --> 00:02:57,900 at the error messages, 56 00:02:57,900 --> 00:03:02,900 and we only see what is being produced. 57 00:03:03,450 --> 00:03:04,770 Now hey, this is interesting. 58 00:03:04,770 --> 00:03:06,330 Do you see what's happening here? 59 00:03:06,330 --> 00:03:08,523 I still see error messages. 60 00:03:09,510 --> 00:03:11,130 And you know why? 61 00:03:11,130 --> 00:03:13,230 Well, that's because of the pipe. 62 00:03:13,230 --> 00:03:14,970 I'm using the pipe here. 63 00:03:14,970 --> 00:03:16,740 Before the pipe, we have the find command. 64 00:03:16,740 --> 00:03:19,110 After the pipe, we have the xargs command. 65 00:03:19,110 --> 00:03:22,440 It is not xargs that is generating all of these errors. 66 00:03:22,440 --> 00:03:24,900 It's find that is generating all of these errors, 67 00:03:24,900 --> 00:03:28,440 or maybe it's even worse, and maybe it's both of them. 68 00:03:28,440 --> 00:03:31,470 And if I don't want to see any errors anymore, 69 00:03:31,470 --> 00:03:35,490 then I also need 2> to /dev/null before the pipe. 70 00:03:35,490 --> 00:03:37,620 And now we can only see the result. 71 00:03:37,620 --> 00:03:40,020 And the result is a list of files 72 00:03:40,020 --> 00:03:43,773 that contain the text 127.0.0.1. 73 00:03:44,820 --> 00:03:48,720 Now you can also do some formatting in find. 74 00:03:48,720 --> 00:03:51,640 Now let me do find /etc 75 00:03:52,680 --> 00:03:53,733 minus name, 76 00:03:55,722 --> 00:03:56,555 *conf. 77 00:03:59,580 --> 00:04:00,420 And there we go. 78 00:04:00,420 --> 00:04:02,103 We have configuration files. 79 00:04:03,330 --> 00:04:06,150 Now this is what we see by default. 80 00:04:06,150 --> 00:04:08,430 And the nice thing is that find allows you 81 00:04:08,430 --> 00:04:10,200 to do some formatting on that. 82 00:04:10,200 --> 00:04:11,793 So if I add -printf, 83 00:04:13,470 --> 00:04:16,980 the -printf allows me to do formatting. 84 00:04:16,980 --> 00:04:21,870 And in this formatting, I am going to specify how I want it 85 00:04:21,870 --> 00:04:25,630 to be printed, %s, comma, 86 00:04:25,630 --> 00:04:29,940 %p\n, closing quote, 87 00:04:29,940 --> 00:04:31,140 which is doing what? 88 00:04:31,140 --> 00:04:33,420 This is giving me additional information. 89 00:04:33,420 --> 00:04:36,210 And as you can see, I get information 90 00:04:36,210 --> 00:04:38,850 about the size of the file in the first column, 91 00:04:38,850 --> 00:04:41,070 and then I have the name of the file. 92 00:04:41,070 --> 00:04:45,390 And it's separated with a comma and a space. 93 00:04:45,390 --> 00:04:48,750 And if you want to, you can even sort that. 94 00:04:48,750 --> 00:04:52,020 That's a new utility that we will discuss 95 00:04:52,020 --> 00:04:53,340 in more detail later. 96 00:04:53,340 --> 00:04:58,340 Sort -rn, which is doing a reverse numeric sort, 97 00:04:58,380 --> 00:05:00,450 so that you have the biggest file on the top 98 00:05:00,450 --> 00:05:03,120 and the smallest file on the bottom. 99 00:05:03,120 --> 00:05:07,263 And that is how you can use find in a more advanced way.