1 00:00:00,150 --> 00:00:00,750 Hello. 2 00:00:00,750 --> 00:00:04,950 The beautiful people know in the last video you learned about the different ways that commands can handle 3 00:00:04,950 --> 00:00:05,700 input and output. 4 00:00:05,700 --> 00:00:10,230 And I see that in your mind the concepts behind the most powerful abilities that the Linux operating 5 00:00:10,230 --> 00:00:11,240 system can give us. 6 00:00:11,250 --> 00:00:13,140 Piping and redirection. 7 00:00:13,140 --> 00:00:18,120 Now, piping and redirection allow us to create sophisticated command pipelines and move data around 8 00:00:18,120 --> 00:00:23,220 our computer system with such unbelievable ease and flexibility that it's basically a super power. 9 00:00:23,220 --> 00:00:27,810 So in this video, you're going to begin gaming superpowers by learning how to perform redirection in 10 00:00:27,810 --> 00:00:31,080 Linux and later on in the next few videos will be covering piping. 11 00:00:31,080 --> 00:00:35,190 But by the end of this video, you'll be able to redirect standard input, standard output and standard 12 00:00:35,190 --> 00:00:39,270 error to your heart's content, and you'll feel much more like a computer genius already. 13 00:00:39,270 --> 00:00:41,430 So let's go ahead and get right into it. 14 00:00:43,350 --> 00:00:43,680 Okay. 15 00:00:43,680 --> 00:00:48,360 So our main friend for this video is going to be the cat command that we saw a bit earlier. 16 00:00:48,390 --> 00:00:53,190 Now, the cat command is used to concatenate or stick together multiple different files. 17 00:00:53,190 --> 00:00:56,130 You'll see how it works in more detail in a later video in the course. 18 00:00:56,130 --> 00:01:00,870 But the reason I'm using the cat command in this video is because it's a simple command that reads from 19 00:01:00,870 --> 00:01:02,970 standard input and writes to standard output. 20 00:01:02,970 --> 00:01:06,120 So it fits our purposes of learning redirection perfectly. 21 00:01:06,120 --> 00:01:09,690 So let's watch cat receive data from standard input. 22 00:01:09,690 --> 00:01:14,940 So if I just type cat here, the cat command waits to receive some data on standard input. 23 00:01:14,940 --> 00:01:19,560 Now cat needs standard input in order to run, but because standard input is by default connected to 24 00:01:19,560 --> 00:01:23,460 the keyboard, cat just sits there and waits for us to enter something on the keyboard. 25 00:01:23,460 --> 00:01:31,560 So if I type meow like that now once we do it will output the same input into standard output. 26 00:01:31,560 --> 00:01:37,200 And because standard output is by default connected to our terminal, it it outputs that the same word 27 00:01:37,200 --> 00:01:40,020 that we entered which was meow, which is appropriate for the cat command. 28 00:01:40,050 --> 00:01:42,780 Of course it outputs that to the terminal. 29 00:01:42,780 --> 00:01:47,400 Now in order to tell the cat command that standard input has finished, we need to press control and 30 00:01:47,400 --> 00:01:53,280 see and cat will stop listening and we will have back control of our terminal, of our shell. 31 00:01:53,280 --> 00:01:54,690 Okay, so that's awesome. 32 00:01:54,690 --> 00:02:00,420 So what if I wanted to change where the standard output goes instead of having it come to the terminal? 33 00:02:00,450 --> 00:02:03,420 In other words, how can I redirect standard output? 34 00:02:03,420 --> 00:02:09,150 Well, let's say that I wanted to redirect standard output to a file called output text. 35 00:02:09,180 --> 00:02:11,220 To do that, I would use a special symbol. 36 00:02:11,220 --> 00:02:13,230 I would write cat just like normal. 37 00:02:13,290 --> 00:02:20,010 Then I would type the number one and the greater than sine and then I type output text. 38 00:02:20,520 --> 00:02:22,230 So let me explain to you what this means. 39 00:02:22,530 --> 00:02:26,220 I want you to remember that standard output is the name of the stream. 40 00:02:26,220 --> 00:02:32,070 The output goes down, standard output isn't the destination, but it's the name of the stream or the 41 00:02:32,070 --> 00:02:35,940 pipe or the yeah, or the stream that the data goes down. 42 00:02:35,940 --> 00:02:40,530 Now every data stream not only has a name like standard output, standard input or standard error, 43 00:02:40,530 --> 00:02:43,230 but it also has a number associated with it. 44 00:02:43,230 --> 00:02:45,480 Standard input is number zero. 45 00:02:45,840 --> 00:02:50,610 Standard output is number one and standard error is number two. 46 00:02:50,700 --> 00:02:51,040 Okay. 47 00:02:51,120 --> 00:02:56,370 So what we're doing here is we're changing the destination of where stream number one points. 48 00:02:56,520 --> 00:03:01,890 In other words, we are redirecting or changing the destination of standard output because standard 49 00:03:01,890 --> 00:03:03,600 output is number one. 50 00:03:03,600 --> 00:03:05,040 So that's why we type the number one. 51 00:03:05,040 --> 00:03:10,200 And we said put the number one, put data stream number one in output text. 52 00:03:10,560 --> 00:03:14,820 So instead of pointing the standard output stream at the terminal, which is where it points by default, 53 00:03:14,820 --> 00:03:18,960 we're pointing it towards a file called output text in our home folder. 54 00:03:18,960 --> 00:03:23,220 Now when I run, that cat is still waiting for some input to run. 55 00:03:23,220 --> 00:03:26,250 So if I type Linux is amazing. 56 00:03:26,640 --> 00:03:26,990 Okay. 57 00:03:27,030 --> 00:03:28,140 And press enter. 58 00:03:28,500 --> 00:03:34,380 We notice now that actually it didn't come back out to the terminal and when I tell cat that done an 59 00:03:34,380 --> 00:03:36,180 input is finished again by pressing control. 60 00:03:36,180 --> 00:03:41,160 And see, first thing we notice is that the command is no longer running and as well we also don't see 61 00:03:41,160 --> 00:03:42,000 any output. 62 00:03:42,000 --> 00:03:47,400 But if I click here to navigate to our home folder, so I click files and I when that window opens up, 63 00:03:47,400 --> 00:03:53,370 if I never get to our home folder, we now see a folder, a file called output txt and inside output 64 00:03:53,580 --> 00:03:54,510 txt. 65 00:03:55,290 --> 00:03:57,540 If I open it we see the text. 66 00:03:57,540 --> 00:03:59,160 Linux is amazing. 67 00:03:59,160 --> 00:04:04,200 How cool is that now because standard output is usually what you want to redirect. 68 00:04:04,200 --> 00:04:07,620 Linux makes it so that you don't actually even need to put the number one. 69 00:04:07,620 --> 00:04:14,100 So if I just get rid of if I just delete output txt, i just press the delete key on my keyboard. 70 00:04:15,390 --> 00:04:15,810 Yeah. 71 00:04:15,960 --> 00:04:19,079 You don't actually even need to put the number one in this symbol here. 72 00:04:19,079 --> 00:04:29,940 So actually if I just do cat then the greater than sign an output txt and I enter linux is amazing and 73 00:04:29,940 --> 00:04:33,690 outputs it and then i press control and c to stop the command. 74 00:04:33,690 --> 00:04:35,910 You can see that we basically get the same result. 75 00:04:35,910 --> 00:04:40,530 Output txt has been created and inside there is linux is amazing. 76 00:04:40,530 --> 00:04:41,550 So that's a nice shortcut. 77 00:04:41,550 --> 00:04:45,960 You don't have to put the number one there, but it's useful to understand that for when you actually 78 00:04:45,960 --> 00:04:51,240 come to redirect the other data streams, you might be wondering about the spacing. 79 00:04:51,240 --> 00:04:52,890 So let's take the long form. 80 00:04:52,890 --> 00:04:56,830 Again, the long form is cat one greater than and then output txt. 81 00:04:56,880 --> 00:05:01,590 Okay, but I couldn't add it so I could write it like this. 82 00:05:01,590 --> 00:05:04,500 I could get rid of the space there and that would be fine. 83 00:05:05,220 --> 00:05:08,440 But I couldn't add a space between the one and the greater than okay. 84 00:05:08,490 --> 00:05:13,860 So I couldn't like do that have a space in between there because this has to be all one symbol that 85 00:05:13,860 --> 00:05:14,730 wouldn't work. 86 00:05:15,090 --> 00:05:19,200 But this would I could have I could close the gap there. 87 00:05:19,200 --> 00:05:20,730 So this symbol is preserved. 88 00:05:21,120 --> 00:05:25,130 The symbol is preserved, but I can have a space after it between that and outputs. 89 00:05:25,140 --> 00:05:26,370 T This is fine. 90 00:05:26,370 --> 00:05:27,510 This is fine. 91 00:05:27,510 --> 00:05:28,560 This is not fine. 92 00:05:28,860 --> 00:05:29,700 Does that make sense? 93 00:05:31,080 --> 00:05:31,440 But yeah. 94 00:05:31,440 --> 00:05:34,260 Just remember that the shell is interpreting what we write. 95 00:05:34,260 --> 00:05:39,930 The shell is looking for one greater than or just greater than as a symbol. 96 00:05:39,930 --> 00:05:42,780 And it has to find that symbol in order to understand what it wants us. 97 00:05:42,850 --> 00:05:48,100 To do so if we break up one greater than we the space now, it no longer works because the shell isn't 98 00:05:48,100 --> 00:05:48,760 looking for that. 99 00:05:49,340 --> 00:05:50,080 Okay, awesome. 100 00:05:50,080 --> 00:05:52,370 So let's try adding more text to the file. 101 00:05:52,390 --> 00:05:54,130 Let's try and add the text. 102 00:05:54,130 --> 00:05:54,940 Wow. 103 00:05:54,940 --> 00:05:56,950 So that it says Linux is amazing. 104 00:05:56,950 --> 00:05:58,120 And then wow. 105 00:05:58,150 --> 00:06:01,000 Okay, so if we do that and we type wow. 106 00:06:02,080 --> 00:06:10,480 And then press control C to stop now when we check the output txt file, we expected to say linux is 107 00:06:10,480 --> 00:06:10,960 amazing. 108 00:06:10,960 --> 00:06:11,380 Wow. 109 00:06:11,380 --> 00:06:11,800 Right. 110 00:06:11,800 --> 00:06:12,910 But when we open it. 111 00:06:14,910 --> 00:06:16,830 It just says the word wow. 112 00:06:17,220 --> 00:06:20,010 What happened to the text that says Linux is amazing? 113 00:06:20,190 --> 00:06:25,530 Well, redirection by default will remove everything in a file before writing to it. 114 00:06:25,560 --> 00:06:28,050 The fancy word for that is called truncation. 115 00:06:28,050 --> 00:06:32,580 But basically what happened is Linux emptied our file and then wrote wow to it. 116 00:06:32,730 --> 00:06:33,870 Well, that's kind of annoying. 117 00:06:33,870 --> 00:06:37,860 So how can we write to a file again using redirection without truncating it? 118 00:06:38,790 --> 00:06:41,900 Well, to do that, we would write the redirection like this. 119 00:06:42,390 --> 00:06:48,860 So you would write Cat one greater than greater than output. 120 00:06:49,440 --> 00:06:51,480 So you can see we've used two arrows there. 121 00:06:51,960 --> 00:06:55,420 So instead of having one arrow, we have to write after one another. 122 00:06:55,440 --> 00:07:04,290 Now, when I type Linux is amazing and I press control and see just to end that, now we look inside 123 00:07:04,290 --> 00:07:04,840 outputs. 124 00:07:05,340 --> 00:07:07,830 We now have Linux is amazing after. 125 00:07:07,830 --> 00:07:08,500 Wow. 126 00:07:08,520 --> 00:07:08,880 Okay. 127 00:07:09,060 --> 00:07:11,490 And again, the shortcut still applies. 128 00:07:11,490 --> 00:07:18,060 So instead of having cat one greater than greater that by the way, that shortcut right there, I just 129 00:07:18,060 --> 00:07:22,590 press the up arrow key instead of having cat one greater than greater, then I can just have greater 130 00:07:22,590 --> 00:07:29,790 than greater than when I do that and I say Linux is very amazing and do that and stop. 131 00:07:29,790 --> 00:07:34,770 Then when I reload the file we can see that now it adds Linux is very amazing. 132 00:07:34,980 --> 00:07:39,300 So this works just as well with just the greater than greater than it worked, just as well as having 133 00:07:39,300 --> 00:07:43,080 the number one because Linux makes it just that bit easier for us because usually you're redirecting 134 00:07:43,080 --> 00:07:45,810 standard output, which is data stream number one. 135 00:07:45,810 --> 00:07:49,110 So that's how you redirect standard output. 136 00:07:49,110 --> 00:07:54,780 And this process, as I said, of using the double arrows is called appending to a file. 137 00:07:56,110 --> 00:07:59,980 So in the interest of time, we're going to cut our discussion short in this video and we're going to 138 00:07:59,980 --> 00:08:01,450 pick it up again in the next video. 139 00:08:01,450 --> 00:08:06,010 But in summary, what you've seen in this video is, first of all, the data streams have numbers associated 140 00:08:06,010 --> 00:08:06,490 with them. 141 00:08:06,670 --> 00:08:09,400 Data stream number zero is standard input. 142 00:08:09,400 --> 00:08:14,230 Data stream number one is standard output and data stream number two is standard error. 143 00:08:14,230 --> 00:08:19,490 And you can use these numbers to actually access the specific data streams that you want for redirection. 144 00:08:19,510 --> 00:08:24,160 Now, in this video, you've seen how to redirect standard output, which is data stream number one. 145 00:08:24,160 --> 00:08:27,640 And it's probably the one that you will be using most of the time. 146 00:08:27,640 --> 00:08:32,470 But what you need to make sure of and this works for all data streams, is you need to be careful of 147 00:08:32,470 --> 00:08:33,789 truncating your files. 148 00:08:33,789 --> 00:08:38,230 You saw that when you use one arrow, when you were reading, when you were redirecting such an output, 149 00:08:39,039 --> 00:08:45,010 the shell would delete everything that was in the file or in the place that you were redirecting to 150 00:08:45,010 --> 00:08:47,050 before adding the data in. 151 00:08:47,050 --> 00:08:51,970 But if you use two arrows, then you can actually append your data to the file. 152 00:08:51,970 --> 00:08:54,730 You can add your data on top of what is already there. 153 00:08:54,730 --> 00:08:56,260 So just be aware of that. 154 00:08:56,260 --> 00:09:00,670 So in the next video, we're going to take a look now at how you would redirect standard error and standard 155 00:09:00,670 --> 00:09:01,180 input. 156 00:09:01,180 --> 00:09:03,160 So I'll see you in the next video.