1 00:00:00,300 --> 00:00:01,859 Hello there, you beautiful people. 2 00:00:01,859 --> 00:00:06,810 Now, although each of the Linux commands are very powerful on their own, the true power of the Linux 3 00:00:06,810 --> 00:00:11,910 command line comes from our ability to chain these commands together to build what are known as command 4 00:00:11,910 --> 00:00:12,900 pipelines. 5 00:00:12,900 --> 00:00:17,310 Now, each Linux command is designed to do one thing and do it incredibly well. 6 00:00:17,310 --> 00:00:22,680 So by being able to combine together loads of powerful and feature rich building blocks, you can create 7 00:00:22,680 --> 00:00:27,150 very powerful workflows and pipelines without much work at all. 8 00:00:27,240 --> 00:00:31,680 But in order to chain the commands together and create these powerful workflows, these pipelines, 9 00:00:31,680 --> 00:00:36,480 we need to understand a bit better how Linux commands actually take input and give output so that we 10 00:00:36,480 --> 00:00:38,460 can link the outputs and inputs together. 11 00:00:38,460 --> 00:00:43,020 So in this video, you're going to learn about the different ways that a command can take input and 12 00:00:43,020 --> 00:00:47,730 give output, and by the end you'll be able to understand the different ways that data can flow into 13 00:00:47,730 --> 00:00:48,540 and out of a command. 14 00:00:48,540 --> 00:00:54,390 And you'll be ready to connect these commands, these data flows together to build powerful Linux pipelines. 15 00:00:54,390 --> 00:00:59,100 This is going to be an absolutely revolutionary time for your Linux abilities because we're going to 16 00:00:59,100 --> 00:01:03,990 step up from taking just one command and learning about it to actually learn how to chain multiple commands 17 00:01:03,990 --> 00:01:04,349 together. 18 00:01:04,349 --> 00:01:06,510 So I hope you're very, very excited. 19 00:01:06,510 --> 00:01:08,850 Let's go ahead and jump right into it. 20 00:01:09,500 --> 00:01:13,600 So as you learned earlier, each command is basically its own computer program. 21 00:01:13,610 --> 00:01:19,360 Now, a computer program is simply an automated way to take inputs, process them and give outputs. 22 00:01:19,370 --> 00:01:24,580 So here's a diagram that shows you the different ways that a command can take inputs and give outputs. 23 00:01:24,590 --> 00:01:30,890 Now you can see that a general Linux command can take input from two places and will output to two other 24 00:01:30,890 --> 00:01:36,620 places, making a total of four possible ways to get data into and out of a conventional Linux command. 25 00:01:36,620 --> 00:01:44,180 So the inputs are command arguments and standard input, and the outputs are standard output and standard 26 00:01:44,180 --> 00:01:44,630 error. 27 00:01:45,140 --> 00:01:50,570 You can see that three of these are indicated by the yellow dashed arrows and are examples of something 28 00:01:50,570 --> 00:01:52,460 called a standard data stream. 29 00:01:52,460 --> 00:01:55,820 You can tell this from their names because they all start with the word standard. 30 00:01:55,820 --> 00:02:01,310 So you've got standard input, standard output and standard error, and they're all standard data streams, 31 00:02:01,310 --> 00:02:05,990 whereas the command line arguments have a solid blue arrow and aren't a standard data stream. 32 00:02:05,990 --> 00:02:10,610 So in this video, I'm going to explain to you what the different input or output options are, and 33 00:02:10,610 --> 00:02:15,080 you'll also learn what is special about a standard data stream and how these data streams can be used 34 00:02:15,080 --> 00:02:16,640 to connect commands together. 35 00:02:16,640 --> 00:02:19,790 So let's jump over to Ubuntu and see all this stuff in real time. 36 00:02:21,590 --> 00:02:21,870 Okay. 37 00:02:21,920 --> 00:02:25,040 So for this discussion, it makes more sense to start with the outputs. 38 00:02:25,040 --> 00:02:26,270 So let's start there. 39 00:02:26,300 --> 00:02:31,340 Now, when a command has run successfully, the primary way that the data is outputted from the command 40 00:02:31,340 --> 00:02:34,070 is on the channel called Standard Output. 41 00:02:34,100 --> 00:02:37,430 Now, standard output is something called a standard data stream. 42 00:02:37,490 --> 00:02:41,900 Now, just like a stream of water, data, streams start somewhere and they end somewhere. 43 00:02:41,900 --> 00:02:44,060 So where does standard output lead? 44 00:02:44,090 --> 00:02:47,570 Well, by default, standard output will lead to your terminal. 45 00:02:47,570 --> 00:02:51,770 So that's why when we type commands, the output of the command appears on the screen. 46 00:02:51,770 --> 00:02:53,770 So for example, let's take the data command. 47 00:02:53,780 --> 00:02:59,210 If we type, date and press enter when we tell the date command to run, the output will be sent from 48 00:02:59,210 --> 00:03:04,400 the date command and flow down the standard output stream, which by default is connected to our terminal 49 00:03:04,400 --> 00:03:04,910 screen. 50 00:03:04,910 --> 00:03:07,820 Therefore, we see the data appear below our command. 51 00:03:08,250 --> 00:03:09,590 Okay, so that's pretty cool. 52 00:03:09,590 --> 00:03:12,740 But I want to plant a little seed in your mind for us to come back to later. 53 00:03:12,770 --> 00:03:17,540 Now, remember how I said that by default, the standard output of a command comes to the terminal? 54 00:03:17,570 --> 00:03:22,880 Well, the amazing thing about output data streams is that you can redirect where they go using a process 55 00:03:22,880 --> 00:03:25,640 imaginatively, imaginatively called redirection. 56 00:03:25,640 --> 00:03:30,560 So just like redirecting a pipe will change where water flows, redirecting a data stream will change 57 00:03:30,560 --> 00:03:31,520 where the data goes. 58 00:03:32,100 --> 00:03:35,570 Okay, so hopefully that seed has been planted and we'll come back to it in a moment. 59 00:03:35,660 --> 00:03:39,050 Now, the second type of output is something called standard error. 60 00:03:39,050 --> 00:03:44,510 So when a command gives an error message or a log message or something like that, anything that isn't 61 00:03:44,510 --> 00:03:51,650 the primary output of the command that is outputted into the stream known as standard error, this helps 62 00:03:51,650 --> 00:03:54,920 keep useful output and log or error information separate. 63 00:03:54,950 --> 00:03:57,320 So for example, let's take the data command again. 64 00:03:57,320 --> 00:04:03,830 If I make the date command fail by giving it some invalid command argument like this, okay. 65 00:04:03,860 --> 00:04:09,680 And then I press enter, you'll see that we get an error message and it says Invalid date blue a little 66 00:04:09,710 --> 00:04:12,110 lower now because it's an error message. 67 00:04:12,110 --> 00:04:17,630 It was sent down the standard error stream and because the error message arrived at our terminal screen, 68 00:04:17,630 --> 00:04:22,880 we can see that the standard error is also automatically connected to our terminal, just like standard 69 00:04:22,880 --> 00:04:23,630 output is. 70 00:04:23,630 --> 00:04:28,640 But again, because standard error is a data stream, it can be redirected wherever we wish. 71 00:04:29,830 --> 00:04:34,630 So now that we understand that standard output is where the main output for the commands go, and that 72 00:04:34,630 --> 00:04:39,160 standard error is where the error and log messages for the command go and that both of those are by 73 00:04:39,160 --> 00:04:40,960 default connected to the terminal. 74 00:04:41,170 --> 00:04:43,240 Let's now turn our attention to inputs. 75 00:04:43,510 --> 00:04:46,900 As mentioned, there are also two ways for a command to get input. 76 00:04:46,930 --> 00:04:51,490 The first we will discuss is another type of standard data stream, which, as you can guess, is called 77 00:04:51,490 --> 00:04:52,750 standard input. 78 00:04:52,780 --> 00:04:56,730 Now standard input is by default connected to the keyboard. 79 00:04:56,740 --> 00:05:00,860 So to demonstrate this, I will introduce a new command called Cat. 80 00:05:00,880 --> 00:05:05,410 Now we're going to discuss Cat in a later section, but I'll just use it now to explain the concept 81 00:05:05,410 --> 00:05:06,460 of standard input. 82 00:05:06,490 --> 00:05:13,270 If we take a look at the main page for Cat, so we type man cat, we see in its description section 83 00:05:13,270 --> 00:05:16,390 the if cat is not given any. 84 00:05:16,540 --> 00:05:20,950 If it's not given any, how can I say it's not giving any command line arguments? 85 00:05:20,950 --> 00:05:21,440 That's it. 86 00:05:21,460 --> 00:05:23,620 Cat will read from standard input. 87 00:05:23,620 --> 00:05:27,490 So if no file is given, then read it or it'll read from standard input. 88 00:05:28,810 --> 00:05:29,770 So let's do that. 89 00:05:29,770 --> 00:05:35,560 If I just type cat and don't give it any other form of input, we can see that the terminal kind of 90 00:05:35,560 --> 00:05:36,610 just hangs there. 91 00:05:36,730 --> 00:05:42,970 But if I start typing and I type hello and press enter, we see that the text is read from our keyboard, 92 00:05:42,970 --> 00:05:48,760 i.e. from standard input and then cat processes it and outputs the results to standard output. 93 00:05:48,940 --> 00:05:53,830 Now, because standard output is by default attached to our terminal, we see the output appear right 94 00:05:53,830 --> 00:05:54,640 in our terminal. 95 00:05:54,970 --> 00:05:55,960 Okay, that's pretty awesome. 96 00:05:55,960 --> 00:06:00,300 Now because standard input is a data stream, it can be redirected as well. 97 00:06:00,310 --> 00:06:03,580 That means we can tell a command where it should read data from. 98 00:06:03,610 --> 00:06:08,740 Now this could be a file where we already have some pre configured input or more powerfully it could 99 00:06:08,740 --> 00:06:10,390 be another data stream. 100 00:06:10,900 --> 00:06:16,240 Now is this ability for data streams to connect together that makes them so powerful, you can simply 101 00:06:16,240 --> 00:06:21,970 pass the standard output stream from one command to the standard input stream of another, then pass 102 00:06:21,970 --> 00:06:26,110 the standard output stream of that second command to the standard input stream of the third command 103 00:06:26,110 --> 00:06:29,820 and so on and so on until you build up a very powerful pipeline. 104 00:06:29,830 --> 00:06:35,080 Now, connecting outputs to inputs in this way is known as piping together commands and is an incredibly 105 00:06:35,080 --> 00:06:39,790 important concept in Linux as it's what makes working with the command line so powerful and effective, 106 00:06:39,790 --> 00:06:41,740 as you will see in coming videos. 107 00:06:42,190 --> 00:06:45,000 Now the final type of input is command line arguments. 108 00:06:45,010 --> 00:06:48,320 Now, we've already actually seen command line arguments in action already. 109 00:06:48,340 --> 00:06:52,660 For example, when studying the command structure previously we took a look at the core command and 110 00:06:52,660 --> 00:06:57,460 we saw, by the way, I'm going to press control and see to cancel this control and see and that cancels 111 00:06:57,460 --> 00:06:59,350 it and press control now to clear the screen. 112 00:06:59,350 --> 00:07:05,620 So when we're using the command, we saw something like Cal 12, 2017, which will allow us to see the 113 00:07:05,620 --> 00:07:07,810 calendar for December 2017. 114 00:07:07,810 --> 00:07:13,210 And the numbers 12 and 2017 were command line arguments. 115 00:07:13,210 --> 00:07:18,550 And you can tell that because they were added on the command line as an input to the command, we also 116 00:07:18,550 --> 00:07:20,110 made the command more advanced like this. 117 00:07:20,110 --> 00:07:25,810 If you remember, we had Cal gave it the capital ay option, gave that option and input gave the capital 118 00:07:25,840 --> 00:07:30,580 B option, gave that option its own input and then gave the Cal command to other inputs. 119 00:07:30,820 --> 00:07:35,500 So it was quite complicated and it showed us the calendar for December 2017 with one month after and 120 00:07:35,500 --> 00:07:36,460 one month before. 121 00:07:36,550 --> 00:07:40,720 Now the A and B options have their own command line arguments. 122 00:07:40,720 --> 00:07:46,210 Here, the aye option has the command line argument, which is number one, and B also has a command 123 00:07:46,210 --> 00:07:48,130 line argument of the number one. 124 00:07:48,130 --> 00:07:55,330 And on top of those, the overall Cal Command has 12 and 2017 as its own command line arguments. 125 00:07:55,570 --> 00:08:00,820 So there's one argument for the option, one argument for the B option and two arguments for the overall 126 00:08:00,820 --> 00:08:01,660 CAL command. 127 00:08:01,810 --> 00:08:06,640 Now, the difference between command line arguments and data streams is that data streams can flow, 128 00:08:06,670 --> 00:08:09,130 they can be redirected and piped together. 129 00:08:09,160 --> 00:08:14,650 Command line arguments only associate with the command or option that they're dealing with at the moment. 130 00:08:14,770 --> 00:08:19,150 Now there are commands such as the X ARGs Command that allow to blur the lines a little bit between 131 00:08:19,150 --> 00:08:20,530 data streams and command line arguments. 132 00:08:20,530 --> 00:08:25,120 But in general, the difference is that data streams can flow and be piped together, but command line 133 00:08:25,120 --> 00:08:26,290 arguments are more static. 134 00:08:26,290 --> 00:08:31,960 And still, there is one other key difference to keep in mind, though, in that not all commands accept 135 00:08:31,960 --> 00:08:32,830 standard input. 136 00:08:32,830 --> 00:08:37,960 For example, the Echo Command doesn't accept standard input, but pretty much every command can accept 137 00:08:37,960 --> 00:08:39,340 command line arguments. 138 00:08:39,340 --> 00:08:43,780 And if you want to know whether a command can actually accept standard input, just check its main page. 139 00:08:43,780 --> 00:08:46,000 And if it's not there, just assume that it can't. 140 00:08:46,820 --> 00:08:47,390 Awesome. 141 00:08:47,390 --> 00:08:51,090 So in this video you've learned some incredibly important concepts. 142 00:08:51,110 --> 00:08:56,330 As a quick recap, we learnt that there are two ways to get data into commands and two ways to get data 143 00:08:56,330 --> 00:08:56,840 out. 144 00:08:56,840 --> 00:09:02,360 The ways in our standard input and command line arguments and the ways out are standard output and standard 145 00:09:02,360 --> 00:09:02,870 error. 146 00:09:03,500 --> 00:09:08,600 Now, the three ways that start with the word standard are examples of standard data streams. 147 00:09:08,600 --> 00:09:13,130 We mentioned that standard output is where the outputted data for a command goes, and that standard 148 00:09:13,130 --> 00:09:16,130 error is where the error and log messages for a command goes. 149 00:09:16,130 --> 00:09:20,480 And by default, standard output and standard error are attached to your terminal. 150 00:09:20,480 --> 00:09:22,160 But this can be changed. 151 00:09:22,190 --> 00:09:23,900 You also learned about standard input. 152 00:09:23,900 --> 00:09:29,390 Now standard input by default is attached to your keyboard and is also a data stream, which means it 153 00:09:29,390 --> 00:09:34,040 can take data from other places too, using something called redirection. 154 00:09:34,040 --> 00:09:34,730 That's right. 155 00:09:34,730 --> 00:09:40,430 Data streams can be redirected from their default locations to wherever you wish, using a process called 156 00:09:40,430 --> 00:09:41,360 redirection. 157 00:09:41,390 --> 00:09:47,300 We also mentioned that you can actually connect the output streams of one command to the standard input 158 00:09:47,300 --> 00:09:50,260 of another using a method called piping. 159 00:09:50,270 --> 00:09:55,790 Now, piping and redirection are incredibly powerful abilities that Linux gives us, and being able 160 00:09:55,790 --> 00:10:01,040 to use them effectively will basically give you super powers for interacting with your computer in ways 161 00:10:01,040 --> 00:10:02,960 that you never thought possible. 162 00:10:02,960 --> 00:10:07,940 And I hope that you're super excited because piping and redirection are going to be the topic of the 163 00:10:07,940 --> 00:10:08,960 next few lectures. 164 00:10:08,960 --> 00:10:12,920 So if you want to gain super powers, I'll see you in the next video.