1 00:00:00,720 --> 00:00:02,130 Hello, the beautiful people. 2 00:00:02,130 --> 00:00:05,160 So in the last video you saw how to redirect standard output. 3 00:00:05,160 --> 00:00:09,600 And in this video, we're going to be picking that up and continuing by learning how to redirect standard 4 00:00:09,600 --> 00:00:11,280 error and standard input. 5 00:00:11,280 --> 00:00:12,690 So let's start with standard error. 6 00:00:12,690 --> 00:00:14,190 How do you go about redirecting that? 7 00:00:14,190 --> 00:00:17,400 Well, standard error works in very much the same way as standard output. 8 00:00:17,400 --> 00:00:20,970 Now, the number for the standard error data stream is the number two. 9 00:00:20,970 --> 00:00:26,550 So to redirect standard error, you would type cat and then instead of one and then a greater than sign, 10 00:00:26,550 --> 00:00:28,110 which is what we use for standard output. 11 00:00:28,140 --> 00:00:32,009 You just have a two because the data stream number four, standard error is the number two. 12 00:00:32,009 --> 00:00:39,000 And this would redirect the standard error from the cat command to error text and to append the standard 13 00:00:39,000 --> 00:00:43,770 error to whatever is already in error text, you would do the two arrows. 14 00:00:43,770 --> 00:00:49,530 So remember the difference between the one arrow and the two arrows is the one arrow will delete everything 15 00:00:49,530 --> 00:00:51,960 that's in the file before append before writing to it. 16 00:00:51,960 --> 00:00:57,930 So it will truncate the file and two arrows will append or add to what's already there. 17 00:00:58,200 --> 00:00:59,370 So let's demonstrate this. 18 00:01:00,150 --> 00:01:03,600 The cat command doesn't have the k option. 19 00:01:03,990 --> 00:01:06,810 So if I do cat give it the k option and just type blah. 20 00:01:06,840 --> 00:01:07,270 Right. 21 00:01:07,290 --> 00:01:13,110 We're going to get an error and it's going to say invalid option k, try cat help for more information. 22 00:01:13,740 --> 00:01:15,600 So this is an error message. 23 00:01:15,600 --> 00:01:19,170 So therefore it was output on the standard error data stream. 24 00:01:19,170 --> 00:01:24,720 And the standard error data stream is by default connected to our terminal and therefore we see it in 25 00:01:24,720 --> 00:01:25,290 our terminal. 26 00:01:25,290 --> 00:01:30,690 But let's redirect it to error text so I can press the up arrow key to save myself some typing and I'm 27 00:01:30,690 --> 00:01:37,260 going to type the number two and then a greater than sine and say redirect that to error text. 28 00:01:37,770 --> 00:01:43,770 So I'm saying run this and redirect the, the, the standard error to text. 29 00:01:43,800 --> 00:01:47,760 So when I press enter, we first of all, we noticed that nothing popped out on our terminal, which 30 00:01:47,760 --> 00:01:48,570 is a good sign. 31 00:01:48,570 --> 00:01:52,740 And when we open up our files, we can now see there's a file called Error Text. 32 00:01:52,740 --> 00:01:59,160 And when I open it, we can see that indeed the error message has been sent to that location. 33 00:01:59,580 --> 00:02:04,350 A common use for this kind, for redirecting standard error is to keep track of log messages coming 34 00:02:04,350 --> 00:02:06,450 from web servers and things like that. 35 00:02:06,450 --> 00:02:13,110 But a common mistake is to use just one arrow like we have here, because if I keep doing that, what 36 00:02:13,110 --> 00:02:19,440 that means is it will delete what's in the file before before it sends standard error again. 37 00:02:19,440 --> 00:02:24,690 So if I keep doing that, okay, imagine a new error happens every time. 38 00:02:25,140 --> 00:02:27,510 If I keep doing that and open error text. 39 00:02:27,540 --> 00:02:29,850 Only the most recent error has been saved. 40 00:02:30,000 --> 00:02:33,120 But if I instead of having one arrow, I have two arrows. 41 00:02:33,120 --> 00:02:36,510 Now I'm going to append each error on top of each other. 42 00:02:36,630 --> 00:02:37,090 Okay. 43 00:02:37,380 --> 00:02:40,980 Which is exactly what you want with a log file, because you want to be able to scroll back and see 44 00:02:40,980 --> 00:02:41,940 what's happened in the past. 45 00:02:42,120 --> 00:02:43,080 So it's open it again. 46 00:02:43,830 --> 00:02:47,040 Now we can see that the errors are being added on top of each other. 47 00:02:47,040 --> 00:02:49,530 So that's the difference between having the one arrow and the two arrows. 48 00:02:49,530 --> 00:02:56,580 The two arrows appends onto what was already there, and that's really what you need for standard error. 49 00:02:57,550 --> 00:03:02,020 Now you can, of course, redirect standard error and standard output at the same time. 50 00:03:02,020 --> 00:03:08,530 So for example, we could type the command and redirect our output to output text. 51 00:03:08,560 --> 00:03:11,410 Remember that standard output is data stream number one. 52 00:03:11,410 --> 00:03:17,080 And we could also redirect standard error to another place called error text. 53 00:03:17,110 --> 00:03:21,160 And we could make sure that we've, we append both of them by making sure there's two arrows at both 54 00:03:21,160 --> 00:03:21,790 places. 55 00:03:21,790 --> 00:03:29,080 And then when we type something like Linux is amazing and then we cancel when we open up our file browser 56 00:03:29,080 --> 00:03:33,040 here, we will see that we now have two files and output. 57 00:03:33,170 --> 00:03:37,840 TXT output txt contains Linux as amazing and standard error. 58 00:03:37,840 --> 00:03:42,790 The error text doesn't contain anything because there were actually no errors from this output. 59 00:03:42,820 --> 00:03:47,320 It was a perfectly valid command, there were no errors required, but that is how you would go about 60 00:03:47,320 --> 00:03:47,920 doing it. 61 00:03:47,920 --> 00:03:51,950 So this is how we would be redirecting standard output to output tcd. 62 00:03:52,000 --> 00:03:57,280 We can tell that because the number one is here and standard error to it file called error dot txt. 63 00:03:57,310 --> 00:04:02,800 Now we can see what the number and the arrows that standard error would be appended and so would output 64 00:04:02,890 --> 00:04:03,370 txt. 65 00:04:03,400 --> 00:04:07,210 Now we actually remember you actually don't need the one output at txt, so you could actually just 66 00:04:07,210 --> 00:04:10,930 do get rid of that and do the say. 67 00:04:10,930 --> 00:04:19,750 Linux is very amazing and when I look at that, we're going to see output at TXT has had its text appended 68 00:04:19,750 --> 00:04:24,040 and again there will still be no errors in error txt because there weren't any errors. 69 00:04:24,040 --> 00:04:28,570 But you can see here that without using the number one, this will still work, but you do still need 70 00:04:28,570 --> 00:04:32,200 the number two for standard error because that's shortcut isn't available. 71 00:04:32,560 --> 00:04:37,330 So you can only get rid of the number one for the standard output data stream. 72 00:04:38,620 --> 00:04:38,980 Okay. 73 00:04:38,980 --> 00:04:39,370 Brilliant. 74 00:04:39,370 --> 00:04:42,900 So you now understand how to redirect standard output and standard error. 75 00:04:42,910 --> 00:04:44,650 So how about standard input? 76 00:04:44,680 --> 00:04:46,750 Well, actually, that's relatively straightforward. 77 00:04:46,840 --> 00:04:50,550 At the moment, the cat command is looking for standard input from the keyboard. 78 00:04:50,560 --> 00:04:54,070 But what if we told it to get standard input from a file instead? 79 00:04:54,070 --> 00:04:59,670 So let's make a file called input text and type in there the words Hello world. 80 00:04:59,680 --> 00:05:04,150 Okay, so what we're going to do is we're going to use the cat command and we're going to output that 81 00:05:04,150 --> 00:05:07,060 now to a file called input text. 82 00:05:07,390 --> 00:05:07,840 Okay. 83 00:05:07,840 --> 00:05:11,110 So we're going to make a file we're going to put in there Hello World. 84 00:05:11,650 --> 00:05:17,560 Now when I cancel that, we have now effectively created a file called input txt. 85 00:05:17,560 --> 00:05:20,410 And in there we have hello world. 86 00:05:20,560 --> 00:05:24,780 Okay, so you can see how you can use redirection to create files like this as well. 87 00:05:24,790 --> 00:05:28,960 Now let's use now let's try and read from that file using the cat command again. 88 00:05:29,260 --> 00:05:34,180 How can we make the contents of this file, this input text file be read into standard input for the 89 00:05:34,180 --> 00:05:38,410 cat command so that we don't have to type it later on, let's say. 90 00:05:38,410 --> 00:05:41,650 Well, the standard input stream has the number of zero. 91 00:05:41,650 --> 00:05:46,870 So what we can do is we can type cat then the number zero and instead of the greater than sine, which 92 00:05:46,870 --> 00:05:55,210 is like an output, you actually use the less than sine we say then input, text or as a shortcut because 93 00:05:55,420 --> 00:05:58,510 the standard input stream is the only way that you can actually input. 94 00:05:58,510 --> 00:06:02,020 You don't even need the zero, you just put the less than sign right there. 95 00:06:02,170 --> 00:06:06,280 Now when we do this, we see the hello world message pop up on the screen. 96 00:06:06,280 --> 00:06:11,140 Now, this is because Cat has read the file from standard input and outputted it to standard output, 97 00:06:11,140 --> 00:06:14,290 which is by default connected to our terminal. 98 00:06:14,530 --> 00:06:16,210 Now here's a mini challenge. 99 00:06:16,210 --> 00:06:16,540 Okay? 100 00:06:16,540 --> 00:06:26,500 I want you to get CAT to read standard input from input text and redirect that output to hello text 101 00:06:27,430 --> 00:06:29,470 and make it so that that happens. 102 00:06:29,470 --> 00:06:29,850 Okay. 103 00:06:29,890 --> 00:06:31,750 So go ahead and give that a go. 104 00:06:31,780 --> 00:06:39,010 Pause the video now and try to get CAT to read standard input from input text and redirect the output 105 00:06:39,010 --> 00:06:40,900 to hello text. 106 00:06:40,930 --> 00:06:42,790 Then come back to see how you did. 107 00:06:49,700 --> 00:06:50,270 Did you manage? 108 00:06:50,270 --> 00:06:53,080 It was, here's how I would go about it. 109 00:06:53,090 --> 00:06:58,090 I would first redirect cat's standard input so that it comes from input text. 110 00:06:58,130 --> 00:07:00,170 So we're just going to do like that. 111 00:07:00,320 --> 00:07:05,750 And then I would output it to a file called Hello Dot Text. 112 00:07:06,290 --> 00:07:10,250 Now to make it easier to read, I might add the stream numbers. 113 00:07:10,250 --> 00:07:15,230 So I might say, okay, I'm going to redirect into stream zero input text and I'm going to redirect 114 00:07:15,410 --> 00:07:19,460 from stream one, which is standard output to hello text. 115 00:07:19,700 --> 00:07:21,220 It's totally up to you which one you use. 116 00:07:21,230 --> 00:07:22,430 This might be a way you do it. 117 00:07:22,430 --> 00:07:23,300 It might not be. 118 00:07:24,200 --> 00:07:28,670 But just remember that zero is standard input, one is standard output and two is standard error. 119 00:07:28,670 --> 00:07:28,950 Okay. 120 00:07:29,060 --> 00:07:34,040 And here we can see that we're reading input text into standard input. 121 00:07:34,040 --> 00:07:36,500 It's quite easy to see that quite, quite graphically. 122 00:07:36,500 --> 00:07:40,550 And we're writing from standard output into hello text. 123 00:07:40,580 --> 00:07:40,970 Okay. 124 00:07:41,060 --> 00:07:47,540 So if we go ahead and do that now, we should be able to see that in fact, yes, we now have a file 125 00:07:47,540 --> 00:07:50,690 called Hello Text and when we open it we see hello world. 126 00:07:50,690 --> 00:07:52,310 Isn't that amazing? 127 00:07:52,320 --> 00:07:54,920 That's how you can redirect data all around your computer. 128 00:07:54,920 --> 00:07:57,200 Now, here's one more mind blowing thing. 129 00:07:57,200 --> 00:08:03,470 Everything in Linux is actually treated the same as a file, even terminals. 130 00:08:03,470 --> 00:08:05,390 So let me open up another terminal. 131 00:08:05,390 --> 00:08:09,800 I'm going to use the shortcut control Orton T and I'm going to open them right next to one another. 132 00:08:09,800 --> 00:08:13,400 So I'm going to put one over here and I'm going to put one over here. 133 00:08:13,670 --> 00:08:14,300 Okay. 134 00:08:14,390 --> 00:08:20,630 Now in the other terminal, this one over this new one on the right, I'm going to run the t y command. 135 00:08:20,630 --> 00:08:26,090 Now, the t command will actually tell us where this terminal is located, where on the file system 136 00:08:26,090 --> 00:08:26,510 it is. 137 00:08:26,510 --> 00:08:30,920 And we see it comes back with slash dev, slash RTS, slash one. 138 00:08:30,950 --> 00:08:31,340 Okay. 139 00:08:31,430 --> 00:08:37,730 Now what I'm going to do is I'm going to redirect the, the, the content from input text. 140 00:08:37,760 --> 00:08:45,320 So just like this and instead of writing it to a file, I'm going to write it to this location, slash 141 00:08:45,320 --> 00:08:47,270 dev, slash RTS, slash one. 142 00:08:47,270 --> 00:08:51,350 So slash dev, slash RTS, slash one. 143 00:08:51,830 --> 00:08:53,240 And when I press enter. 144 00:08:54,940 --> 00:08:56,190 Oh my goodness. 145 00:08:56,200 --> 00:09:00,400 We can see that the data has been passed from one terminal to another. 146 00:09:00,400 --> 00:09:04,390 It's kind of like a really rubbish instant messaging system right now. 147 00:09:04,390 --> 00:09:06,580 Well, this isn't particularly useful in itself. 148 00:09:06,580 --> 00:09:12,520 It shows how redirection can be used to pass data around your entire computer and even over computer 149 00:09:12,520 --> 00:09:13,000 networks. 150 00:09:13,000 --> 00:09:13,390 I've heard. 151 00:09:13,390 --> 00:09:15,220 So that's pretty amazing. 152 00:09:15,760 --> 00:09:21,040 So how awesome you now know everything you need to know about redirection in Linux and the beauty of 153 00:09:21,040 --> 00:09:23,980 the Linux system should begin to be making itself clear. 154 00:09:23,980 --> 00:09:28,810 Now, as a quick recap, you learn that standard input, standard output and standard error are streams 155 00:09:28,810 --> 00:09:34,150 of data and like normal water streams, their flow comes from somewhere and goes to somewhere. 156 00:09:34,180 --> 00:09:40,000 Redirection is all about controlling that, where using redirection you can control where standard output 157 00:09:40,000 --> 00:09:44,110 and standard error streams go and you can control where standard input comes from. 158 00:09:44,140 --> 00:09:47,470 You also learn that these data streams have numbers associated with them. 159 00:09:47,500 --> 00:09:49,480 Standard input is stream number zero. 160 00:09:49,480 --> 00:09:53,410 Standard output is stream number one and standard error is stream number two. 161 00:09:54,070 --> 00:09:57,070 You then learned about the different symbols that can be used for redirection. 162 00:09:57,070 --> 00:09:59,650 They are basically the greater than and less than signs. 163 00:09:59,680 --> 00:10:03,970 Just remember that if you're redirecting output, one greater than sign means that the file will be 164 00:10:03,970 --> 00:10:06,400 erased or truncated before you write to it. 165 00:10:06,400 --> 00:10:10,330 So if you don't want to lose the data that's in there, make sure to use to greater than science to 166 00:10:10,330 --> 00:10:11,650 append data to the file. 167 00:10:11,650 --> 00:10:14,500 In other words, add data to what's already there. 168 00:10:14,800 --> 00:10:18,400 Now there are a few more cool things that you can do with redirection, but they're less common than 169 00:10:18,400 --> 00:10:20,320 what you've learned in these last few videos. 170 00:10:20,320 --> 00:10:25,120 So if you're curious, I've put a link where you can learn more about redirection in the lecture resources. 171 00:10:25,330 --> 00:10:31,750 But one question that must be burning in your head right now is How can I redirect the output from one 172 00:10:31,750 --> 00:10:33,580 command to the input of another? 173 00:10:33,580 --> 00:10:34,960 Surely there must be a way. 174 00:10:34,960 --> 00:10:40,570 Well, that process is known as piping, and it is, in my opinion, that that's the crown jewel of 175 00:10:40,570 --> 00:10:41,800 the Linux command line. 176 00:10:41,800 --> 00:10:46,060 Piping is probably the most important thing that the command line lets you do because it takes all of 177 00:10:46,060 --> 00:10:51,250 the commands that do one thing very, very, very well and allows you to tie them together seamlessly 178 00:10:51,250 --> 00:10:53,740 to do basically anything that you want. 179 00:10:53,770 --> 00:10:56,470 Mastering piping will definitely give you superpowers. 180 00:10:56,470 --> 00:10:59,320 So to master the power of piping and gain super powers. 181 00:10:59,320 --> 00:11:01,330 I'll see you in the next video.