1 00:00:06,510 --> 00:00:07,740 - In this video, you will learn 2 00:00:07,740 --> 00:00:09,993 about I/O Redirection and Piping. 3 00:00:11,250 --> 00:00:12,770 So, let's start with redirection. 4 00:00:12,770 --> 00:00:15,150 Redirection is used to manipulate 5 00:00:15,150 --> 00:00:17,133 input and output of commands. 6 00:00:18,079 --> 00:00:20,580 And there are three ways of doing it: 7 00:00:20,580 --> 00:00:23,760 standard input, also known as the zero. 8 00:00:23,760 --> 00:00:25,650 The zero is for the file descriptor, 9 00:00:25,650 --> 00:00:28,080 and the file descriptor is something used internally 10 00:00:28,080 --> 00:00:30,930 by the Linux kernel to take care of redirection. 11 00:00:30,930 --> 00:00:33,390 And the zero is always your standard input. 12 00:00:33,390 --> 00:00:35,460 And the standard input normally represents 13 00:00:35,460 --> 00:00:37,830 your keyboard attached to your computer. 14 00:00:37,830 --> 00:00:40,980 And if you want to get information from somewhere else, 15 00:00:40,980 --> 00:00:43,830 well, there is a smaller than redirector. 16 00:00:43,830 --> 00:00:45,998 Now, the thing with the smaller than redirector 17 00:00:45,998 --> 00:00:47,970 is that you don't need it that often, 18 00:00:47,970 --> 00:00:51,810 because many commands don't need a smaller than redirector 19 00:00:51,810 --> 00:00:54,540 to take their input from a file for instance, 20 00:00:54,540 --> 00:00:55,417 but this would give you something 21 00:00:55,417 --> 00:00:59,220 sort smaller than etc services. 22 00:00:59,220 --> 00:01:01,740 And I can only say, if you would tell me, 23 00:01:01,740 --> 00:01:03,660 Hey we can do that without a smaller than. 24 00:01:03,660 --> 00:01:05,700 And I would say, yeah, you are right. 25 00:01:05,700 --> 00:01:10,050 But standard input redirectors are just not used that often. 26 00:01:10,050 --> 00:01:12,990 That's not the case for standard output redirector. 27 00:01:12,990 --> 00:01:15,120 The standard output redirector, 28 00:01:15,120 --> 00:01:16,680 normally your standard output. 29 00:01:16,680 --> 00:01:19,500 That's your screen, your terminal attached to your computer 30 00:01:19,500 --> 00:01:22,560 and results of commands are sent to your computer. 31 00:01:22,560 --> 00:01:25,800 And you can redirect it using a greater than sign. 32 00:01:25,800 --> 00:01:28,930 So if you use ls greater then to ~ myfile 33 00:01:29,940 --> 00:01:33,180 then it'll create myfile in your home directory. 34 00:01:33,180 --> 00:01:35,310 Do notice that a single greater than sign 35 00:01:35,310 --> 00:01:38,580 will override destination if it already exists. 36 00:01:38,580 --> 00:01:41,190 if you don't want to override but append 37 00:01:41,190 --> 00:01:43,860 then you use a double redirect. 38 00:01:43,860 --> 00:01:47,160 That will add it to the end of the file. 39 00:01:47,160 --> 00:01:50,460 Also convenient is the option to redirect standard error. 40 00:01:50,460 --> 00:01:53,310 We have seen that on a couple of occasions already. 41 00:01:53,310 --> 00:01:56,850 In order to redirect standard error, you use 2 greater than 42 00:01:56,850 --> 00:01:59,160 in which the 2 is the file descriptor 2 43 00:01:59,160 --> 00:02:00,053 and the greater than will send it 44 00:02:00,053 --> 00:02:02,760 wherever you want to send it. 45 00:02:02,760 --> 00:02:04,980 Like right here, for instance, where we use 46 00:02:04,980 --> 00:02:09,420 2 greater than dev null. dev null is a discard device. 47 00:02:09,420 --> 00:02:13,200 Anything you send to the dev null device will be discarded. 48 00:02:13,200 --> 00:02:14,910 You won't see it anymore. 49 00:02:14,910 --> 00:02:17,040 And this is a perfect way to make sure that you 50 00:02:17,040 --> 00:02:21,360 don't see any error messages while running commands. 51 00:02:21,360 --> 00:02:22,920 And here we have another example 52 00:02:22,920 --> 00:02:26,070 which is using ampersand greater than. 53 00:02:26,070 --> 00:02:28,290 Now what is ampersand greater than? 54 00:02:28,290 --> 00:02:30,420 That will represent the standard output 55 00:02:30,420 --> 00:02:31,953 as well as the standard error. 56 00:02:32,940 --> 00:02:35,250 Now that is stuff that you normally don't want to write 57 00:02:35,250 --> 00:02:38,040 to the dev null device, but you might want 58 00:02:38,040 --> 00:02:40,920 to capture all that information in a specific file. 59 00:02:40,920 --> 00:02:44,400 And then this is how you do it. Before we move over 60 00:02:44,400 --> 00:02:48,123 and talk about piping, let's go check out these examples. 61 00:02:50,190 --> 00:02:52,590 So, right. Let me start using 62 00:02:52,590 --> 00:02:56,370 sort smaller than etc services 63 00:02:56,370 --> 00:03:00,270 to use etc services as the thing that we want sort. 64 00:03:00,270 --> 00:03:04,350 And yeah, the smaller than is just redirecting the 65 00:03:04,350 --> 00:03:07,773 the input from the keyboards to etc services. 66 00:03:08,790 --> 00:03:10,920 Right. As I was telling you before 67 00:03:10,920 --> 00:03:13,980 that's not a very convincing example, I know. 68 00:03:13,980 --> 00:03:18,980 But ls greater than, lsfile is a more convincing example. 69 00:03:19,650 --> 00:03:22,650 What do we get? We get no output on screen. 70 00:03:22,650 --> 00:03:24,033 We get output in lsfile. 71 00:03:25,110 --> 00:03:28,650 Which is the list of files just generated. 72 00:03:28,650 --> 00:03:32,580 Now, if I use who greater than to lsfile 73 00:03:32,580 --> 00:03:36,480 then I'm doing an, a standard output redirection again. 74 00:03:36,480 --> 00:03:39,750 And after checking the contents of lsfile 75 00:03:39,750 --> 00:03:42,660 you have seen that who command has overwritten. 76 00:03:42,660 --> 00:03:45,660 And if you don't want to override but append 77 00:03:45,660 --> 00:03:48,780 then you should use a double redirect. 78 00:03:48,780 --> 00:03:51,480 The double redirect, as you can see is adding the output 79 00:03:51,480 --> 00:03:54,213 of the command to the file we just created. 80 00:03:55,260 --> 00:03:57,783 Now, how about the error messages? 81 00:03:58,650 --> 00:04:02,460 Well, let me do grab a root on etc start. 82 00:04:02,460 --> 00:04:04,470 That's an example that we've seen before. 83 00:04:04,470 --> 00:04:07,680 Many, many permission denied is a directory 84 00:04:07,680 --> 00:04:09,210 which are error messages. 85 00:04:09,210 --> 00:04:11,760 And if you don't care about the error messages. 86 00:04:11,760 --> 00:04:14,073 2 greater than dev null. 87 00:04:15,060 --> 00:04:17,220 And that will make sure that you only see 88 00:04:17,220 --> 00:04:19,620 the regular output. Now, what is happening 89 00:04:19,620 --> 00:04:23,010 if you make that ampersand greater than. 90 00:04:23,010 --> 00:04:25,260 Ampersand greater than dev null 91 00:04:25,260 --> 00:04:28,680 is showing nothing at all. That might be useful 92 00:04:28,680 --> 00:04:31,200 for processes that are supposed to be running 93 00:04:31,200 --> 00:04:32,250 in the background 94 00:04:32,250 --> 00:04:35,880 but not for interactive commands, like grep. 95 00:04:35,880 --> 00:04:40,740 So let me create outfile and write the output to outfile. 96 00:04:40,740 --> 00:04:44,520 And there we can see the, the output 97 00:04:44,520 --> 00:04:46,980 including the error messages. 98 00:04:46,980 --> 00:04:49,550 We have the file descriptor 1 as well as 99 00:04:49,550 --> 00:04:50,970 as the file descriptor 2 100 00:04:50,970 --> 00:04:55,803 both being sent to outfile. Let's go check out piping. 101 00:04:57,780 --> 00:05:00,510 So a pipe is used to send the output of one command 102 00:05:00,510 --> 00:05:03,210 to be used as input for a second command. 103 00:05:03,210 --> 00:05:04,620 This also is something that we have 104 00:05:04,620 --> 00:05:06,570 already seen a couple of times. 105 00:05:06,570 --> 00:05:10,680 Like ps aux, grab http. Where ps aux is 106 00:05:10,680 --> 00:05:15,000 showing a lot of running processes and grep http is 107 00:05:15,000 --> 00:05:19,803 filtering out the lines that contain the text http. 108 00:05:21,060 --> 00:05:24,750 In combination with the pipe, you might see the tee command. 109 00:05:24,750 --> 00:05:26,220 Now, what is the tee command? 110 00:05:26,220 --> 00:05:29,130 The tee command combines redirection and piping. 111 00:05:29,130 --> 00:05:31,530 It allows you to write the output to somewhere 112 00:05:31,530 --> 00:05:36,000 and at the same time, use it as input for another command 113 00:05:36,000 --> 00:05:40,560 like ps aux, tee psfile, pipe grep ssh. 114 00:05:40,560 --> 00:05:42,453 Let me show you these examples. 115 00:05:44,250 --> 00:05:46,493 So to start with ps aux, ps aux. 116 00:05:47,820 --> 00:05:50,610 Which by the way, is another BSD style commands. 117 00:05:50,610 --> 00:05:51,990 This command is showing a list 118 00:05:51,990 --> 00:05:54,240 of all the processes currently running. 119 00:05:54,240 --> 00:05:56,760 And if you want to verify whether 120 00:05:56,760 --> 00:06:00,090 or not SSH is running well, this is what you can do. 121 00:06:00,090 --> 00:06:03,480 This is an ordinary pipe as we have already seen. 122 00:06:03,480 --> 00:06:05,940 Now, how does that work with, with tee? 123 00:06:05,940 --> 00:06:08,553 Well, I am going to pipe to tee. 124 00:06:09,840 --> 00:06:12,450 psfile or whatever you wanna call it. 125 00:06:12,450 --> 00:06:15,150 And then I'm going to pipe to grep SSH. 126 00:06:15,150 --> 00:06:17,850 Here we can see the result of the grep SSH, 127 00:06:17,850 --> 00:06:21,930 but at the same time, it has created psfile. 128 00:06:21,930 --> 00:06:24,758 So in psfile, we have the ps aux 129 00:06:24,758 --> 00:06:29,220 and the grep SSH is working on the ps aux. 130 00:06:29,220 --> 00:06:32,580 The tee is really like the uppercase letter T. 131 00:06:32,580 --> 00:06:35,130 It's sending you two different directions 132 00:06:35,130 --> 00:06:37,500 to a file as well as to the pipe 133 00:06:37,500 --> 00:06:40,620 where it is further treated by grep SSH. 134 00:06:40,620 --> 00:06:42,300 This tee can be handy 135 00:06:42,300 --> 00:06:44,760 if you want to have an additional opportunity to 136 00:06:44,760 --> 00:06:47,730 analyze what exactly was happening 137 00:06:47,730 --> 00:06:50,643 before you applied the command after the pipe. 138 00:06:51,540 --> 00:06:53,403 And that's all, let's continue.