1 00:00:00,920 --> 00:00:02,270 Hello the beautiful people. 2 00:00:02,270 --> 00:00:03,330 And welcome back. 3 00:00:03,350 --> 00:00:09,710 So in the last video, you learned how to open files and read their contents by using several command 4 00:00:09,710 --> 00:00:10,520 line commands. 5 00:00:10,520 --> 00:00:14,960 So, for example, the cat command will open up a file and allow us to read its contents. 6 00:00:14,960 --> 00:00:19,070 So if we use file one dot txt, we can see that it contains the word hello. 7 00:00:19,070 --> 00:00:24,950 And when we open files like this, what happens is everything is shown all at once. 8 00:00:24,950 --> 00:00:29,570 So no matter how long the file is, it will all be spat out right into our terminal. 9 00:00:30,050 --> 00:00:31,640 And that can be kind of a pain. 10 00:00:31,640 --> 00:00:36,650 So for example, if we try to open up a really long file, such as the file that I showed you in the 11 00:00:36,650 --> 00:00:40,550 last video, which is something to do with printers and stuff on your system. 12 00:00:41,270 --> 00:00:48,740 So if I type that in so cops browsed dot conf and I tried to print that out, you can see that we've 13 00:00:48,740 --> 00:00:54,440 just shut out a whole bunch of information and if we want to read it, we've got to scroll up and down 14 00:00:54,440 --> 00:00:55,850 like this right in our command line. 15 00:00:55,850 --> 00:00:57,860 And it's a bit of a pain, right, like this. 16 00:00:57,890 --> 00:00:59,270 It's not really a nice way. 17 00:00:59,270 --> 00:01:01,310 You want to be able to deal with long files. 18 00:01:01,940 --> 00:01:09,080 So to help with this, there are programs on Linux called pager programs that allow you to page through 19 00:01:09,080 --> 00:01:11,420 large output to make it easier to read. 20 00:01:11,420 --> 00:01:15,830 And the one that we'll be covering in this video is called The Less Command. 21 00:01:15,830 --> 00:01:23,180 So if we just do instead of cat, if we do less and then give it the file that we want to read, you 22 00:01:23,180 --> 00:01:28,220 can see now that what's happening is we are we started, first of all, at the top of the file and by 23 00:01:28,220 --> 00:01:34,970 just using our keyboard keys, our arrow keys, up and down, we can scroll through this content and 24 00:01:34,970 --> 00:01:40,790 read it by scrolling up and down a lot easier than if we'd had it just on the command line. 25 00:01:40,790 --> 00:01:44,480 And if I press Q you can see down the bottom here we got these two columns. 26 00:01:44,480 --> 00:01:50,210 If I type Q, it closes and now we're back on our command line with no mess that we have to scroll through. 27 00:01:50,210 --> 00:01:52,460 So that's a nice way about of doing it. 28 00:01:52,460 --> 00:01:55,070 And actually you can pipe to the less command. 29 00:01:55,070 --> 00:01:59,960 So instead of opening it directly like that, I could use Cat to open the file and then pipe into the 30 00:01:59,960 --> 00:02:02,180 last command and it will give us the same result. 31 00:02:02,180 --> 00:02:08,750 Now this ability to pipe to the less command is incredibly useful because it doesn't just have to be 32 00:02:08,750 --> 00:02:11,990 files that you are that you are dealing with. 33 00:02:12,710 --> 00:02:18,980 For example, if I said, okay, let's go into our into our home directory and let's find all the text 34 00:02:18,980 --> 00:02:19,310 files. 35 00:02:19,310 --> 00:02:19,490 Okay? 36 00:02:19,520 --> 00:02:26,300 So we're going to find by name everything that ends in dot txt and we see that we get a whole bunch 37 00:02:26,300 --> 00:02:27,020 of information here. 38 00:02:27,020 --> 00:02:28,670 Now this could be quite long. 39 00:02:29,140 --> 00:02:31,310 It could, it could be quite a lot of stuff. 40 00:02:31,310 --> 00:02:37,310 But what we're going to do is we're going to instead of that, actually, instead of just a text, let's 41 00:02:37,310 --> 00:02:38,490 just try and find everything, okay? 42 00:02:38,540 --> 00:02:41,240 It's printed out a whole bunch of stuff that we've now got to scroll through. 43 00:02:41,270 --> 00:02:46,730 Instead of that, we can just pipe it into less and now we can scroll through line by line. 44 00:02:46,730 --> 00:02:52,490 So the last command is a really useful one when working with the command line, because it saves you 45 00:02:52,490 --> 00:02:58,820 from having a lot of mess, filling up your screen and piping to it is is really, really, really useful 46 00:02:59,030 --> 00:03:02,240 because now we can press Q And all the mess has gone. 47 00:03:02,810 --> 00:03:08,360 Now sometimes you don't want the whole output or the whole file and you only want to operate on a piece 48 00:03:08,360 --> 00:03:08,690 of it. 49 00:03:08,990 --> 00:03:13,490 So perhaps you want to take a little piece from a file and pass it through your pipeline. 50 00:03:13,490 --> 00:03:16,430 Instead of having the whole file on your screen. 51 00:03:16,700 --> 00:03:19,460 You just want to peek at a little piece before getting on with your work. 52 00:03:19,460 --> 00:03:23,840 So for this purpose we can use the head and the tail commands. 53 00:03:23,870 --> 00:03:29,210 Now the head command will allow you to see a little piece of the file starting at the top or the head 54 00:03:29,210 --> 00:03:30,020 of the file. 55 00:03:30,020 --> 00:03:32,030 And the tail command will kind of do the opposite. 56 00:03:32,030 --> 00:03:36,680 It will allow you to see a little piece of the file starting at the bottom or at the tail of the file. 57 00:03:36,710 --> 00:03:37,020 Okay. 58 00:03:37,130 --> 00:03:41,720 So for example, let's concatenate together the five files again. 59 00:03:41,720 --> 00:03:47,120 So I'm going to go to our desktop and clear the screen and I'm going to cut together files. 60 00:03:47,330 --> 00:03:48,350 Oops, clear the screen. 61 00:03:48,470 --> 00:03:51,110 Cut together files 1 to 5. 62 00:03:51,160 --> 00:03:51,470 Okay. 63 00:03:51,710 --> 00:03:55,100 TXT and we see that we get five rows out. 64 00:03:55,100 --> 00:03:55,500 Okay. 65 00:03:55,580 --> 00:03:59,450 Now if I just pipe this into the head command. 66 00:04:01,030 --> 00:04:02,430 We just get the same output. 67 00:04:02,440 --> 00:04:03,520 So why is that? 68 00:04:03,550 --> 00:04:09,010 Well, that's because the head command will, by default, show you the first ten lines of a file. 69 00:04:09,010 --> 00:04:11,740 And since there are only five, we'll see it all. 70 00:04:11,740 --> 00:04:15,590 But we can edit this behavior by specifying how many lines that we want to see. 71 00:04:15,610 --> 00:04:22,240 So if I just press the opera key to get what we want, I can give the the H, the head command, the 72 00:04:22,240 --> 00:04:25,730 N option and specify how many rows we want to see. 73 00:04:25,750 --> 00:04:29,230 So now we're just going to see the first two lines and you'll see. 74 00:04:29,230 --> 00:04:29,710 There we go. 75 00:04:29,710 --> 00:04:30,550 We just see hello. 76 00:04:30,550 --> 00:04:33,510 And they're the first two lines of the output. 77 00:04:33,520 --> 00:04:36,300 Now the head command is very useful in pipelines. 78 00:04:36,310 --> 00:04:40,660 Again, let's say we were doing the find command and we're back in our home directory and we run the 79 00:04:40,660 --> 00:04:41,170 find command. 80 00:04:41,170 --> 00:04:42,550 We'll get a whole bunch of output. 81 00:04:42,550 --> 00:04:47,380 And let's say you just wanted the first five of these options, you could just pipe it to the head command, 82 00:04:47,380 --> 00:04:51,760 give it the an option and say, Oh, hey, I just want five lines, let me press enter. 83 00:04:51,760 --> 00:04:52,480 And you see, there we go. 84 00:04:52,480 --> 00:04:58,270 We get the first five results and then you could take those and pipe them down, down the the pipeline 85 00:04:58,660 --> 00:04:59,770 for whatever reason you wanted. 86 00:04:59,770 --> 00:05:04,000 So maybe I wanted to, you know, then pass those into the tac command and reverse the order that they're 87 00:05:04,000 --> 00:05:05,220 in or whatever it might be. 88 00:05:05,230 --> 00:05:05,650 Okay. 89 00:05:05,710 --> 00:05:08,710 So that's a really useful application of the head command. 90 00:05:08,710 --> 00:05:12,490 It works the same for the tail command, but the tail command will just show you from the bottom rather 91 00:05:12,490 --> 00:05:13,810 than from the top. 92 00:05:14,050 --> 00:05:17,770 But I want to show you that actually you don't need to just pipe to the head command. 93 00:05:17,770 --> 00:05:20,680 You can operate directly on files. 94 00:05:20,680 --> 00:05:23,800 So we had that really long file, didn't we? 95 00:05:23,800 --> 00:05:30,250 We had what was it slash Etsy cops cops browsed dot com. 96 00:05:30,250 --> 00:05:35,260 And you see we've got this really really long file now if we can actually see how long that is by piping 97 00:05:35,260 --> 00:05:39,690 that to the word count command and giving it the URL option like you've seen a bit earlier in the course, 98 00:05:39,730 --> 00:05:42,130 I can see we've got 673 lines. 99 00:05:42,130 --> 00:05:43,450 That's quite a few lines. 100 00:05:43,450 --> 00:05:45,670 But what if we just want the first 20 lines? 101 00:05:45,670 --> 00:05:48,220 Well, instead of piping it, we don't have to do that. 102 00:05:48,220 --> 00:05:53,740 What we can do is we can just say head, give it the an option of the first 20 and then give it the 103 00:05:53,740 --> 00:05:55,120 file and just operate directly on it. 104 00:05:55,120 --> 00:06:01,690 And we will get just the first 20 lines and we can confirm that we do indeed have 20 lines by just pressing 105 00:06:01,690 --> 00:06:06,190 the up arrow key here and piping that into the word count command with the option to count the amount 106 00:06:06,190 --> 00:06:06,610 of lines. 107 00:06:06,610 --> 00:06:07,240 And we'll see that. 108 00:06:07,240 --> 00:06:11,980 Yep, we do now only have 20 lines of output, so that's perfect. 109 00:06:11,980 --> 00:06:17,110 Now the tail command works basically the same way as the head command, except it starts from the bottom 110 00:06:17,110 --> 00:06:18,160 of a file. 111 00:06:18,160 --> 00:06:30,550 So if we do our cutting again, we go back to the desktop and we cut out file 1 to 5 dot txt and we'll 112 00:06:30,550 --> 00:06:30,880 see that. 113 00:06:30,880 --> 00:06:31,060 Yep. 114 00:06:31,060 --> 00:06:32,530 We get hello there you beautiful people. 115 00:06:32,530 --> 00:06:37,030 Now if we pipe that into the tail command and say we just want the bottom two lines, then we'll just 116 00:06:37,030 --> 00:06:37,990 get beautiful people. 117 00:06:37,990 --> 00:06:44,770 So the bottom two lines and if we again operated on the really, really long file, so instead of having 118 00:06:45,010 --> 00:06:47,470 cat instead of having head, let's have tail. 119 00:06:47,830 --> 00:06:49,840 Get rid of this word count at the end. 120 00:06:50,800 --> 00:06:56,050 Now if we if we press enter, you can see that we just get the bottom 20 lines and we can again pipe 121 00:06:56,050 --> 00:06:57,190 that into word count and see that. 122 00:06:57,190 --> 00:06:59,590 Yes, we do indeed have 20 lines. 123 00:06:59,590 --> 00:07:06,160 So head and tail allow you to cut either the top of a file or the bottom of a file, respectively, 124 00:07:06,880 --> 00:07:08,950 to control exactly what you want. 125 00:07:08,950 --> 00:07:13,780 And you can use these together to kind of filter out specific lines. 126 00:07:14,230 --> 00:07:22,900 So let's say we did let's go back to our head example and let's say that we cut out the first 20 lines. 127 00:07:23,110 --> 00:07:25,900 So these are the first 20 lines of that big long file. 128 00:07:25,960 --> 00:07:35,260 And then if I pipe that into the tail command and said, okay, give me just one line from the bottom. 129 00:07:35,260 --> 00:07:39,880 Okay, then this blank bit here is going to be the 20th line of the file. 130 00:07:39,880 --> 00:07:41,020 Maybe that's not that exciting. 131 00:07:41,020 --> 00:07:49,150 Let's say if I get say three, okay, then that's going to give me lines 18, 19 and 20 of the file. 132 00:07:49,150 --> 00:07:53,890 And you can use the commands in this way to cut out specific lines. 133 00:07:54,160 --> 00:07:57,400 So that's that's quite a useful combination right there. 134 00:07:57,400 --> 00:08:03,460 But really where these things come into come into their real use is when you are building your pipeline. 135 00:08:03,460 --> 00:08:07,900 So the example I've been using and leaning on is the find command because it gives us a lot of output. 136 00:08:07,900 --> 00:08:14,860 But if you wanted to just get a specific set of lines, so let's say I wanted the last three lines, 137 00:08:14,860 --> 00:08:18,640 I could just pipe those into the tail command and just say, okay, just give me the last three lines 138 00:08:18,640 --> 00:08:27,910 and then continue to pipe that down or even export it, let's say, into export text on our on our desktop. 139 00:08:28,270 --> 00:08:33,760 And then you can save that and keep just the lines that you want rather than having to have a whole 140 00:08:33,760 --> 00:08:35,650 bunch of output that you don't need. 141 00:08:35,860 --> 00:08:36,970 So it's like a filter. 142 00:08:36,970 --> 00:08:42,309 The head and tail commands are best seen as filters that allow your pipelines to get you just what you 143 00:08:42,309 --> 00:08:43,419 want to work with. 144 00:08:44,950 --> 00:08:48,040 Q So we've covered quite a lot of stuff in the past few videos, right? 145 00:08:48,040 --> 00:08:53,050 But fortunately none of it is particularly complicated, but it's all very useful. 146 00:08:53,050 --> 00:08:59,350 So as a quick recap, you've seen how to join files together or concatenate files using the cat command, 147 00:08:59,350 --> 00:09:04,480 which is a great tool for not only joining files together, but also allowing you to take a files content 148 00:09:04,480 --> 00:09:08,680 and putting it right into standard output so that it can be used in your pipelines. 149 00:09:08,710 --> 00:09:14,710 You've also seen how you can reverse files vertically, so using the tac command and you've also seen 150 00:09:14,710 --> 00:09:17,410 how to reverse files horizontally across their lines. 151 00:09:17,410 --> 00:09:22,510 Using the rev command and using both of those together is a real good way to jumble up your files. 152 00:09:22,540 --> 00:09:27,520 Now, some files that you want to read are very, very long, and just viewing their contents with the 153 00:09:27,520 --> 00:09:31,510 cat command, for example, is a bit of a mess and it gives you a lot of stuff you've got to scroll 154 00:09:31,510 --> 00:09:31,980 through. 155 00:09:31,990 --> 00:09:37,660 So you saw how to use the less command to page through massive amounts of output or large files from 156 00:09:37,660 --> 00:09:43,690 the command line without needing to make unnecessary files just to view the output and piping from commands 157 00:09:43,690 --> 00:09:48,340 to the less command is a great way to keep your terminal tidy and make your workflow more effective 158 00:09:48,340 --> 00:09:51,710 and just make dealing with large amounts of data a lot easier to handle. 159 00:09:51,730 --> 00:09:57,190 And finally, you saw how to use the head and tail commands to cut out just what you want from a file, 160 00:09:57,460 --> 00:10:01,060 and if you would like, then pass it straight on down your pipeline. 161 00:10:01,060 --> 00:10:06,760 So these are really, really useful tools that will come in handy quite often when working with file 162 00:10:06,760 --> 00:10:07,240 content. 163 00:10:07,240 --> 00:10:12,580 So now you not only know how to manipulate files as a whole, so copy a whole file, move a whole file, 164 00:10:12,580 --> 00:10:13,300 delete a whole file. 165 00:10:13,300 --> 00:10:16,630 But now you know how to operate a bit more on their contents. 166 00:10:16,630 --> 00:10:21,220 But there's so much more that you can do with file contents and simply just looking at them or sticking 167 00:10:21,220 --> 00:10:21,850 them together. 168 00:10:21,850 --> 00:10:27,070 So in the next video, you're going to learn how you can sort a command, a files, contents, either 169 00:10:27,070 --> 00:10:28,840 alphabetically or numerically. 170 00:10:28,840 --> 00:10:31,480 And being able to sort contents is super useful. 171 00:10:31,480 --> 00:10:35,830 It makes finding what you're looking for easier and works perfectly alongside commands such as the head 172 00:10:35,830 --> 00:10:41,020 and tail commands that we've seen in this video to make really powerful combinations for getting just 173 00:10:41,020 --> 00:10:42,190 what you want out of a file. 174 00:10:42,190 --> 00:10:43,240 So this is going to be epic. 175 00:10:43,240 --> 00:10:46,180 Sorting files is just a really, really useful ability. 176 00:10:46,180 --> 00:10:49,060 So for all that goodness, I'll see you in the next video.