1 00:00:00,000 --> 00:00:04,500 [Intro Music] 2 00:00:04,560 --> 00:00:05,850 In Linux, it's a very common 3 00:00:05,850 --> 00:00:07,740 practice to redirect data 4 00:00:07,740 --> 00:00:10,680 streams to other processes, and send 5 00:00:10,680 --> 00:00:13,050 some data to files for example, and 6 00:00:13,050 --> 00:00:14,730 here on this diagram you see some 7 00:00:14,760 --> 00:00:16,469 sample interaction between 8 00:00:16,469 --> 00:00:18,810 different processes. In this 9 00:00:18,810 --> 00:00:21,200 process you see STDOUT and STDERR; 10 00:00:21,200 --> 00:00:24,000 and we redirect STDOUT 11 00:00:24,030 --> 00:00:26,730 data stream to STDIN of other 12 00:00:26,730 --> 00:00:29,430 process. This process has also of 13 00:00:29,430 --> 00:00:32,100 course, STDOUT and STDERR data 14 00:00:32,100 --> 00:00:34,890 streams. And in this example, those 15 00:00:34,920 --> 00:00:37,470 data streams send data to specific 16 00:00:37,470 --> 00:00:39,870 file; and you could use even same 17 00:00:39,900 --> 00:00:42,540 file for both data streams. In such 18 00:00:42,540 --> 00:00:45,270 case, STDOUT and STDERR will 19 00:00:45,300 --> 00:00:48,090 write data to the same file. Next, 20 00:00:48,090 --> 00:00:50,670 you see that this STDERR data 21 00:00:50,670 --> 00:00:53,550 stream redirects data to STDIN 22 00:00:53,610 --> 00:00:56,130 of another process; and this process 23 00:00:56,160 --> 00:00:59,040 sends STDERR results to another 24 00:00:59,070 --> 00:01:02,700 file. Also, you see that STDOUT of 25 00:01:02,700 --> 00:01:04,739 this process does not send data to 26 00:01:04,769 --> 00:01:07,260 other process or to other file. And 27 00:01:07,260 --> 00:01:09,270 that means that if this process is 28 00:01:09,270 --> 00:01:11,640 running inside of specific bash, or 29 00:01:11,640 --> 00:01:14,280 sh shell, then you'll see result 30 00:01:14,310 --> 00:01:16,860 of this STDOUT data stream inside 31 00:01:16,860 --> 00:01:18,390 of the terminal where you are 32 00:01:18,390 --> 00:01:21,750 running bash or sh process. Now, 33 00:01:21,750 --> 00:01:23,460 let me jump into the terminal, and 34 00:01:23,460 --> 00:01:24,900 explain you all of that on 35 00:01:24,900 --> 00:01:27,180 practice. Let me clear the terminal 36 00:01:27,180 --> 00:01:30,030 here. And I'll use first 'cat' 37 00:01:30,030 --> 00:01:32,340 command. 'cat' command allows you to 38 00:01:32,340 --> 00:01:34,950 read contents of a specific file or 39 00:01:34,980 --> 00:01:38,310 read contents from STDIN, and that's 40 00:01:38,340 --> 00:01:39,900 actually first utility that I'm 41 00:01:39,900 --> 00:01:41,640 showing to you in this course, that 42 00:01:41,670 --> 00:01:44,474 allows you to read data from STDIN; 43 00:01:44,474 --> 00:01:46,950 I mean, you're able to send 44 00:01:46,980 --> 00:01:49,470 data to STDIN of this process; and 45 00:01:49,470 --> 00:01:51,360 this process will accept this data 46 00:01:51,390 --> 00:01:53,670 and process it. Alright, let's go 47 00:01:53,670 --> 00:01:55,530 back to terminal; and first let's 48 00:01:55,530 --> 00:01:57,630 have a look at the help for this 49 00:01:57,630 --> 00:02:00,060 command. Let me scroll up a bit, 50 00:02:00,390 --> 00:02:02,640 and here you'll see that this 51 00:02:02,880 --> 00:02:05,790 command actually concatenates file 52 00:02:05,850 --> 00:02:09,330 or files and sends data to standard 53 00:02:09,389 --> 00:02:12,600 output. With no file, because you see 54 00:02:12,600 --> 00:02:14,430 that the file is actually optional 55 00:02:14,430 --> 00:02:16,920 argument. This utility, 'cat' 56 00:02:16,920 --> 00:02:18,900 utility will read data from 57 00:02:18,930 --> 00:02:21,900 standard input. Let's try that. 58 00:02:22,170 --> 00:02:24,210 Let's try this utility without any 59 00:02:24,210 --> 00:02:26,700 argument, simply 'cat', press Enter, 60 00:02:27,150 --> 00:02:29,250 and now I see that this process is 61 00:02:29,280 --> 00:02:33,300 running. If I enter another tab and 62 00:02:33,330 --> 00:02:34,920 let me quickly have a look at 63 00:02:34,920 --> 00:02:36,210 processes running inside of this 64 00:02:36,210 --> 00:02:40,170 container, 'docker exec -it', let 65 00:02:40,170 --> 00:02:42,090 me copy this CONTAINER ID; paste 66 00:02:42,090 --> 00:02:44,730 here; and I could simply type here 67 00:02:44,760 --> 00:02:47,490 'ps'; and this will start be a process 68 00:02:47,520 --> 00:02:48,930 inside of the running container; 69 00:02:49,320 --> 00:02:51,270 let's press Enter; actually I need 70 00:02:51,270 --> 00:02:54,030 to add here option 'ps -e', I 71 00:02:54,030 --> 00:02:56,040 want to see all processes, and yes 72 00:02:56,070 --> 00:02:58,510 now I see here 'cat' process that is 73 00:02:58,510 --> 00:03:00,150 currently running; and here's 74 00:03:00,180 --> 00:03:03,390 its Process ID. Great. Here while 75 00:03:03,390 --> 00:03:05,550 this process is running we are able 76 00:03:05,550 --> 00:03:09,150 to send some data to STDIN of this 77 00:03:09,150 --> 00:03:12,870 process. Let's type "Hello cat", like 78 00:03:12,870 --> 00:03:17,040 so, Enter, and I see result, and that 79 00:03:17,040 --> 00:03:20,700 means that "Hello cat" was sent as 80 00:03:20,730 --> 00:03:23,910 STDIN data stream to 'cat' process, 81 00:03:24,420 --> 00:03:28,200 and it has sent it back via STDOUT 82 00:03:28,230 --> 00:03:30,390 data stream, and that's why we have 83 00:03:30,390 --> 00:03:32,400 seen this line in the terminal. 84 00:03:33,750 --> 00:03:35,760 Notice that if I press simply Enter, 85 00:03:36,300 --> 00:03:39,180 I get actually two empty lines, and 86 00:03:39,180 --> 00:03:41,340 that means that this Enter is also 87 00:03:41,370 --> 00:03:43,860 returned back by STDOUT data 88 00:03:43,860 --> 00:03:46,320 stream of 'cat' process. Let's type 89 00:03:46,320 --> 00:03:50,250 something else, "Hi", "Hey", "Good morning", 90 00:03:51,330 --> 00:03:53,670 and so on. That's how you're able to 91 00:03:53,670 --> 00:03:56,910 interact with STDIN data stream of 92 00:03:56,910 --> 00:03:59,550 specific process. All right, let me 93 00:03:59,550 --> 00:04:01,980 terminate this process, Ctrl+C. Now 94 00:04:01,980 --> 00:04:04,710 it was terminated. And if I list 95 00:04:04,710 --> 00:04:08,250 processes now, 'ps', I don't see 'cat' 96 00:04:08,250 --> 00:04:09,900 process here on the list. It was 97 00:04:09,900 --> 00:04:12,660 stopped. But that was example of the 98 00:04:12,660 --> 00:04:15,810 process that has STDIN open; and 99 00:04:15,810 --> 00:04:18,899 you could send data via this STDIN 100 00:04:18,930 --> 00:04:22,560 data stream; and it sends same data 101 00:04:22,589 --> 00:04:25,079 back via STDOUT data stream. 102 00:04:26,130 --> 00:04:28,620 Alright, if I use the same command 103 00:04:28,800 --> 00:04:31,230 but with argument, let's have a look 104 00:04:31,260 --> 00:04:33,630 at the contents of this folder; and 105 00:04:33,630 --> 00:04:35,160 here I still see two different 106 00:04:35,160 --> 00:04:38,623 files 'stderr.txt' and 'stdout.txt'. 107 00:04:38,623 --> 00:04:39,930 I could actually read 108 00:04:39,930 --> 00:04:41,940 them using 'cat' command. 'cat', and name 109 00:04:41,940 --> 00:04:45,090 of the file stdout.txt; I'll 110 00:04:45,090 --> 00:04:47,600 see actually contents of stdout.txt 111 00:04:47,600 --> 00:04:49,200 file, but this file is 112 00:04:49,230 --> 00:04:51,720 empty. Let me read the contents of 113 00:04:51,750 --> 00:04:54,510 STDERR file; and there was this 114 00:04:54,510 --> 00:04:57,030 text present; and this text was 115 00:04:57,030 --> 00:05:00,600 produced by STDOUT data stream of 116 00:05:00,630 --> 00:05:01,680 'cat' process; 117 00:05:03,000 --> 00:05:05,490 and if you supply file name as 118 00:05:05,520 --> 00:05:07,740 argument for 'cat' command, you are 119 00:05:07,740 --> 00:05:09,720 not able to send any additional 120 00:05:09,750 --> 00:05:13,110 text via STDIN data stream to 'cat' 121 00:05:13,110 --> 00:05:15,540 process; and the reason for that is 122 00:05:15,540 --> 00:05:18,660 that this file is actually streamed 123 00:05:18,870 --> 00:05:22,830 to STDIN of 'cat' process. That's how 124 00:05:22,830 --> 00:05:25,800 it works under the hood, but STDOUT 125 00:05:25,950 --> 00:05:28,080 is redirected of course to 126 00:05:28,230 --> 00:05:29,910 parent process, and parent process in 127 00:05:29,910 --> 00:05:33,240 this case is bash shell. All right, 128 00:05:33,390 --> 00:05:35,490 now let me demonstrate to you that every 129 00:05:35,490 --> 00:05:39,000 data stream STDIN, STDOUT, and STDERR 130 00:05:39,000 --> 00:05:42,200 has numeric ID. Let me enter 131 00:05:42,200 --> 00:05:45,900 following command 'cat 0<', 132 00:05:45,900 --> 00:05:48,300 and 'stderr.txt', 133 00:05:49,000 --> 00:05:53,300 Enter, and I get same result as before. 134 00:05:53,300 --> 00:05:55,300 [no audio] 135 00:05:55,300 --> 00:05:56,800 And actually in this command 136 00:05:57,000 --> 00:05:59,620 I have told that I want to send 137 00:05:59,620 --> 00:06:03,210 'stderr.txt' file via 138 00:06:03,210 --> 00:06:06,720 standard in to 'cat' process; and 139 00:06:06,720 --> 00:06:10,245 actually this combination '0<' 140 00:06:10,245 --> 00:06:14,346 tells that. '0' is ID of STDIN 141 00:06:14,346 --> 00:06:19,110 data stream, '1' is ID of STDOUT 142 00:06:19,110 --> 00:06:22,847 data stream, and '2' is ID of STDERR 143 00:06:22,847 --> 00:06:24,960 data stream. Let me show you 144 00:06:24,960 --> 00:06:27,000 that. Let me clear terminal; and 145 00:06:27,000 --> 00:06:28,860 let me remove actually those two 146 00:06:28,860 --> 00:06:32,610 files - 'rm stderr.txt', and 147 00:06:32,640 --> 00:06:36,630 'rm stdout.txt'. Now those files 148 00:06:36,660 --> 00:06:38,880 are absent. Great. Let me clear 149 00:06:38,880 --> 00:06:41,370 terminal; and here again I'll use 'ls' 150 00:06:41,370 --> 00:06:43,560 command, it will start 'ls' process, 151 00:06:43,740 --> 00:06:46,080 and I want STDOUT data stream of 152 00:06:46,080 --> 00:06:49,020 this process to be sent to file, and 153 00:06:49,050 --> 00:06:51,480 STDERR data process I'll send to 154 00:06:51,510 --> 00:06:53,940 another file. For that I could use 155 00:06:53,940 --> 00:06:57,540 numeric IDs of data streams - 0, 156 00:06:57,540 --> 00:07:02,010 recap is ID of STDIN, 1 is ID of 157 00:07:02,040 --> 00:07:04,661 STDOUT, and using such syntax 158 00:07:04,661 --> 00:07:07,050 '1>', without space 159 00:07:07,050 --> 00:07:09,870 please, I could send STDOUT data 160 00:07:09,870 --> 00:07:11,880 stream to specific file, or if you 161 00:07:11,880 --> 00:07:14,000 want to other process. I will send to 162 00:07:14,010 --> 00:07:17,880 the file 'stdout.txt'. Next comes 163 00:07:17,880 --> 00:07:21,162 space; and here will be '2>', 164 00:07:21,162 --> 00:07:24,660 2 is ID of STDERR data 165 00:07:24,660 --> 00:07:27,810 stream; and I'll send it to another 166 00:07:27,810 --> 00:07:31,140 file 'stderr.txt', like so. 167 00:07:32,520 --> 00:07:34,840 With this command I will redirect 168 00:07:34,840 --> 00:07:37,410 both STDOUT and STDERR 169 00:07:37,440 --> 00:07:40,350 data streams of 'ls' process. Let's 170 00:07:40,350 --> 00:07:42,570 press Enter; and of course I see 171 00:07:42,570 --> 00:07:44,130 nothing here in the terminal 172 00:07:44,190 --> 00:07:46,440 because STDOUT was redirected to 173 00:07:46,440 --> 00:07:49,200 the file, and here I should see two 174 00:07:49,230 --> 00:07:51,859 newly created files 'stdout.txt', 175 00:07:51,859 --> 00:07:54,600 and 'stderr.txt'; and of 176 00:07:54,600 --> 00:07:57,200 course you could guess that 'stderr.txt' 177 00:07:57,200 --> 00:08:00,100 is empty, and 'stdout.txt' 178 00:08:00,150 --> 00:08:02,550 contains a list of folders, those 179 00:08:02,550 --> 00:08:04,440 folders. Let's have a look at the 180 00:08:04,440 --> 00:08:07,638 'stdout.txt'. 'cat stdout.txt', 181 00:08:07,638 --> 00:08:11,670 here are those folders. And 182 00:08:11,670 --> 00:08:15,228 let's have a look at the 'stderr.txt'. 183 00:08:15,228 --> 00:08:18,510 It's empty. Alright, but if I 184 00:08:18,510 --> 00:08:22,980 repeat same command, but with 'mkdir' 185 00:08:22,980 --> 00:08:24,510 process, let me do that, 186 00:08:25,950 --> 00:08:29,700 I will see that 'stderr.txt' 187 00:08:29,700 --> 00:08:32,429 file now contains this error that 188 00:08:32,429 --> 00:08:34,350 was produced by this process, and 189 00:08:34,350 --> 00:08:37,409 sent via the STDERR data 190 00:08:37,409 --> 00:08:39,510 stream. And please note again that 191 00:08:39,539 --> 00:08:43,260 this 2 is numeric ID of STDERR 192 00:08:43,289 --> 00:08:46,350 data stream. 1 is ID of STDOUT 193 00:08:46,380 --> 00:08:48,510 data stream. Also please notice 194 00:08:48,510 --> 00:08:50,549 that with greater than sign, '>', we 195 00:08:50,580 --> 00:08:53,340 created those files, this one and 196 00:08:53,340 --> 00:08:55,679 this one, each time from scratch. We 197 00:08:55,710 --> 00:08:58,110 rewrite previous file. If you want 198 00:08:58,110 --> 00:09:00,210 to append data to the file, you 199 00:09:00,210 --> 00:09:01,920 could use two arrows instead of 200 00:09:01,920 --> 00:09:04,680 one. Let me show you that. Here will 201 00:09:04,680 --> 00:09:08,010 be greater than sign, '>' like so; and 202 00:09:08,010 --> 00:09:10,530 here will be one more sign as well, '>'. 203 00:09:10,890 --> 00:09:12,810 Let's press Enter, and let's read 204 00:09:12,810 --> 00:09:15,630 contents of error file once again. 205 00:09:15,780 --> 00:09:17,640 And now we see that this text was 206 00:09:17,670 --> 00:09:21,060 appended to this text. Also, let's 207 00:09:21,060 --> 00:09:23,460 repeat the same command, but let's 208 00:09:23,460 --> 00:09:26,280 for example instead of 'mkdir', 209 00:09:26,520 --> 00:09:28,890 let's use 'cat' command, and let's 210 00:09:28,920 --> 00:09:30,930 send also argument to this command. 211 00:09:31,260 --> 00:09:33,000 And this argument will be the name 212 00:09:33,030 --> 00:09:35,010 of the file that is absent now in 213 00:09:35,010 --> 00:09:36,810 this folder where we are running 214 00:09:36,810 --> 00:09:38,910 'cat' command. And in such case when 215 00:09:38,910 --> 00:09:41,430 a file is absent, 'cat' will produce 216 00:09:41,460 --> 00:09:43,680 an error. And let's see which error 217 00:09:43,710 --> 00:09:45,840 it will produce. Let's here type 218 00:09:45,870 --> 00:09:49,620 for example, absent-file.txt, 219 00:09:49,620 --> 00:09:51,870 like so. And this syntax will 220 00:09:51,870 --> 00:09:54,210 remain in place. Let's press Enter, 221 00:09:54,570 --> 00:09:57,677 and let's read contents of 'stderr.txt' 222 00:09:57,677 --> 00:09:59,670 file. And I see that 223 00:09:59,670 --> 00:10:02,490 this new line appeared here in this 224 00:10:02,490 --> 00:10:05,370 text file, 'absent-file.txt: No 225 00:10:05,370 --> 00:10:07,530 such file or directory'; and that is 226 00:10:07,530 --> 00:10:10,380 result of this command. This file 227 00:10:10,380 --> 00:10:13,650 'absent-file.txt' is absent. Here 228 00:10:13,680 --> 00:10:15,780 it is, you don't see this file here 229 00:10:15,780 --> 00:10:17,970 in this folder, that's why 'cat' command 230 00:10:18,000 --> 00:10:20,310 has produced this error, and it has 231 00:10:20,310 --> 00:10:23,190 sent this error via STDERR data 232 00:10:23,190 --> 00:10:26,490 stream to this file. And using two 233 00:10:26,490 --> 00:10:28,650 arrows, '>>', we actually have appended 234 00:10:28,680 --> 00:10:30,510 the contents to existing file. 235 00:10:30,900 --> 00:10:33,210 That's why those lines still 236 00:10:33,240 --> 00:10:35,130 present here in this file, and this 237 00:10:35,130 --> 00:10:38,490 line was appended like so. Alright 238 00:10:38,490 --> 00:10:40,350 guys, that's how you are able to 239 00:10:40,350 --> 00:10:42,120 redirect data streams from a 240 00:10:42,120 --> 00:10:44,010 specific process. And you could 241 00:10:44,010 --> 00:10:45,900 send those streams either to 242 00:10:45,900 --> 00:10:47,550 files as we have tried in this 243 00:10:47,550 --> 00:10:49,740 lecture, or to other processes. 244 00:10:50,400 --> 00:10:52,710 And if you want to send the data 245 00:10:52,710 --> 00:10:55,080 from one process to another, it is 246 00:10:55,080 --> 00:10:56,850 called piping. And let me 247 00:10:56,850 --> 00:10:58,170 demonstrate to you how to perform 248 00:10:58,200 --> 00:10:59,670 piping in the next lecture. I'll 249 00:10:59,670 --> 00:11:00,750 see you there. Bye-Bye. 250 00:11:00,750 --> 00:11:03,146 [no audio]