1 00:00:00,000 --> 00:00:01,390 [No audio] 2 00:00:01,390 --> 00:00:06,710 Standard output to a file using the command tee. 3 00:00:06,710 --> 00:00:09,740 Now we have learned how to actually 4 00:00:10,250 --> 00:00:14,134 populate a file using the redirect commands, 5 00:00:14,182 --> 00:00:16,302 like the greater than sign. 6 00:00:16,376 --> 00:00:18,882 You echo something you output to a 7 00:00:18,896 --> 00:00:20,394 file using the greater than sign. 8 00:00:20,432 --> 00:00:22,122 Now there's another way that you could do 9 00:00:22,136 --> 00:00:24,680 the same thing with using the command tee. 10 00:00:25,550 --> 00:00:29,898 What tee command does, tee command is used to store and 11 00:00:29,924 --> 00:00:34,890 view (both at the same time) the output of any command. 12 00:00:35,990 --> 00:00:39,030 So in other words, the command is named 13 00:00:39,830 --> 00:00:43,078 after the T-splitter used in plumbing. 14 00:00:43,114 --> 00:00:45,474 So you've seen the T-splitter, right? 15 00:00:45,512 --> 00:00:47,502 So you put in the water from one 16 00:00:47,516 --> 00:00:49,810 side, it comes out from the both sides. 17 00:00:49,990 --> 00:00:53,574 This tee command works pretty much the same way. 18 00:00:53,672 --> 00:00:55,698 So it basically breaks the output of a 19 00:00:55,724 --> 00:00:59,242 program so that it can be both displayed 20 00:00:59,386 --> 00:01:02,926 on the screen and saved into a file. 21 00:01:03,118 --> 00:01:07,054 It does both the task simultaneously. Copies 22 00:01:07,102 --> 00:01:09,234 the result into a specified files or 23 00:01:09,272 --> 00:01:11,660 variable and also displays the result. 24 00:01:12,050 --> 00:01:14,914 So if you look at the picture, you'll 25 00:01:14,962 --> 00:01:18,454 see right here we have a command. 26 00:01:18,502 --> 00:01:20,994 So whatever the command you run, you run echo, you run 27 00:01:21,032 --> 00:01:26,970 ls, pwd, chmod or whatever the command that you run, 28 00:01:27,020 --> 00:01:32,106 right, you put a tee in front of it, and then 29 00:01:32,168 --> 00:01:35,658 it will not only send the output on the screen, but 30 00:01:35,684 --> 00:01:39,682 it would also put it into a specified file, the file 31 00:01:39,706 --> 00:01:42,080 that you wanted the output to go to. 32 00:01:42,410 --> 00:01:45,354 So that's how the tee command works. 33 00:01:45,512 --> 00:01:47,914 The reason we want the tee command 34 00:01:48,082 --> 00:01:51,478 over the greater than sign command, 35 00:01:51,574 --> 00:01:54,090 which both do the same thing, 36 00:01:54,200 --> 00:01:56,830 is because if you wanted to run a command, 37 00:01:56,890 --> 00:01:59,526 and you want to output to a file, you 38 00:01:59,588 --> 00:02:02,590 also wanted to view while you're running the command. 39 00:02:02,710 --> 00:02:06,238 You wanted to view how or what the output 40 00:02:06,274 --> 00:02:09,574 looks like and whatever that goes into the file 41 00:02:09,622 --> 00:02:13,220 actually go in the right output that you're looking for. 42 00:02:13,550 --> 00:02:17,506 Okay, so let me go into my Linux 43 00:02:17,578 --> 00:02:22,460 machine and I have it right here open. 44 00:02:23,210 --> 00:02:27,140 Let's clear the screen and type whoami. 45 00:02:28,130 --> 00:02:31,998 And you will see the name of the user account 46 00:02:32,084 --> 00:02:35,790 that I've been logged in with. Which directory I'm in? 47 00:02:35,900 --> 00:02:40,870 I am in my /home/iafzal directory. 48 00:02:41,050 --> 00:02:43,242 Of course, one more thing you should also check 49 00:02:43,316 --> 00:02:45,762 is the hostname that you logged in. 50 00:02:45,836 --> 00:02:48,004 The hostname is the correct hostname, 51 00:02:48,004 --> 00:02:49,590 which is MyFirstLinuxVM. 52 00:02:50,090 --> 00:02:51,642 These are the few things, of course, you need 53 00:02:51,656 --> 00:02:54,198 to check all the time to make sure that 54 00:02:54,224 --> 00:02:56,600 you are logged in to the correct system. 55 00:02:56,930 --> 00:03:00,222 Now let's get back to our command tee. 56 00:03:00,356 --> 00:03:02,446 Now let's say if you want to echo 57 00:03:02,518 --> 00:03:06,510 something on the screen, let's say we echo. 58 00:03:06,510 --> 00:03:09,536 [No audio] 59 00:03:09,570 --> 00:03:18,830 David Puddy is Eliane's boy friend. 60 00:03:18,830 --> 00:03:24,162 David Puddy is just a character in a show Seinfeld, 61 00:03:24,162 --> 00:03:29,214 and he was the boy friend of Eliane Bennis. 62 00:03:29,322 --> 00:03:31,502 So it's just a statement that I'm putting in. 63 00:03:31,576 --> 00:03:33,038 And then you hit enter and you will 64 00:03:33,064 --> 00:03:37,120 see that same echo on the screen. 65 00:03:37,450 --> 00:03:40,274 Now we could use the greater than 66 00:03:40,312 --> 00:03:42,486 sign to output to a file. 67 00:03:42,558 --> 00:03:45,282 So let's say if I wanted to output to a file 68 00:03:45,426 --> 00:03:49,154 eliane-david, this is the name of the file, right? 69 00:03:49,252 --> 00:03:53,334 I could do that. When I hit enter, you'll see it has added 70 00:03:53,382 --> 00:04:01,122 this text to a file name, eliane-david. 71 00:04:01,266 --> 00:04:06,786 Now, what if I verified by running the command cat eliane-david, and 72 00:04:06,786 --> 00:04:11,966 you will see the file has the correct text 73 00:04:12,028 --> 00:04:15,434 that I wanted to have it in the file. Perfect. 74 00:04:15,592 --> 00:04:20,200 Now, if I replace it, the same thing, 75 00:04:20,890 --> 00:04:23,029 let's clear the screen one more time, 76 00:04:23,079 --> 00:04:24,613 and run the command one more time. 77 00:04:24,652 --> 00:04:26,558 and you will see when you run this command with 78 00:04:26,584 --> 00:04:32,118 uppercase sorry, not uppercase with redirect sign, it actually runs 79 00:04:32,154 --> 00:04:35,440 it, but it doesn't throw anything on the screen. 80 00:04:36,130 --> 00:04:43,850 But if you replace this with pipe, and tee, then you 81 00:04:43,900 --> 00:04:49,530 will not only see the output going into eliane-david file, 82 00:04:49,650 --> 00:04:52,814 the output will also show up on the screen. 83 00:04:52,912 --> 00:04:54,674 So hit enter and you'll see 84 00:04:54,712 --> 00:04:56,706 David Puddy is a Eliane's boy friend. 85 00:04:56,778 --> 00:04:58,766 So it is going both ways. 86 00:04:58,828 --> 00:05:02,378 As I said that tee is like 87 00:05:02,464 --> 00:05:04,610 a plumber's T that goes both ways. 88 00:05:04,660 --> 00:05:08,620 So when you do cat eliane-david 89 00:05:08,620 --> 00:05:11,430 David Puddy is Eliane's boy friend. 90 00:05:11,550 --> 00:05:13,782 So now don't be confused that, hey, we ran 91 00:05:13,806 --> 00:05:17,126 that command earlier and it has that same text 92 00:05:17,188 --> 00:05:19,982 message that we ran it with greater than sign. 93 00:05:20,116 --> 00:05:22,406 If that's what it's in your mind, that's fine. 94 00:05:22,468 --> 00:05:27,745 Let's go ahead and remove eliane-david file, 95 00:05:27,745 --> 00:05:30,170 so it doesn't exist. Yes. 96 00:05:30,220 --> 00:05:31,600 So clear the screen. 97 00:05:32,350 --> 00:05:35,860 Now run the same command by hitting up arrow key. 98 00:05:37,330 --> 00:05:40,626 All right. echo, David Puddy is Eliane's boy friend. 99 00:05:40,758 --> 00:05:43,734 So, and tee eliane-david. 100 00:05:43,902 --> 00:05:46,660 This is the file. Hit enter. 101 00:05:48,010 --> 00:05:50,270 Okay, now do cat david, 102 00:05:51,670 --> 00:05:55,158 sorry, eliane-david, 103 00:05:55,194 --> 00:05:58,358 and you'll see the file there. Perfect. 104 00:05:58,504 --> 00:06:02,860 Now, what if you wanted to add 105 00:06:04,030 --> 00:06:07,674 or append to the same file? 106 00:06:07,842 --> 00:06:09,927 Because if you type, let's say, 107 00:06:09,927 --> 00:06:13,398 [No audio] 108 00:06:13,398 --> 00:06:14,532 echo 109 00:06:16,061 --> 00:06:18,380 David and Eliane 110 00:06:19,010 --> 00:06:22,434 into the same file and hit enter, and you'll do 111 00:06:22,472 --> 00:06:28,474 cat eliane-david, you'll see it removed the older file 112 00:06:28,522 --> 00:06:31,762 and created a new file with a newer output. 113 00:06:31,846 --> 00:06:34,198 So you want to append it to a file 114 00:06:34,234 --> 00:06:38,410 that the older text message does not get deleted. 115 00:06:38,590 --> 00:06:40,458 For that, you would have to use a 116 00:06:40,484 --> 00:06:43,290 command, same command but with -a. 117 00:06:43,340 --> 00:06:45,918 So hit up arrow key and go back 118 00:06:45,944 --> 00:06:48,034 to the same command that we ran earlier, 119 00:06:48,142 --> 00:06:53,926 and this time, type tee -a. Same thing echo 120 00:06:53,998 --> 00:06:55,882 David Puddy is Eliane's boy friend. 121 00:06:55,966 --> 00:06:57,294 This is just a text, 122 00:06:57,452 --> 00:06:59,490 pipe it and tee it, 123 00:06:59,660 --> 00:07:02,060 end with the option -a, 124 00:07:02,570 --> 00:07:06,238 and the file name same file, 125 00:07:06,394 --> 00:07:08,600 and now go ahead and hit enter. 126 00:07:09,590 --> 00:07:12,718 Now go ahead and do a cat on eliane-david 127 00:07:12,754 --> 00:07:15,370 file and you will see it has two lines. 128 00:07:15,490 --> 00:07:19,422 First line is that we created without -a option, 129 00:07:19,496 --> 00:07:22,520 and second line we created with -a option. 130 00:07:23,090 --> 00:07:26,986 So that's how you append or add or create a new file. 131 00:07:27,178 --> 00:07:30,250 Now let's try to see how many characters 132 00:07:30,310 --> 00:07:34,546 do we have in eliane-david file. 133 00:07:34,546 --> 00:07:39,642 So the command is wc -c, and you 134 00:07:39,656 --> 00:07:43,042 will see it has 52 eliane-david characters. 135 00:07:43,126 --> 00:07:47,670 We could also output whatever that came 136 00:07:47,840 --> 00:07:49,890 on the screen to another file. 137 00:07:50,870 --> 00:07:55,758 So I use this command, which can be 138 00:07:55,784 --> 00:07:57,882 discussed or which I will cover later on 139 00:07:57,896 --> 00:08:02,334 of course, this command actually count every 140 00:08:02,372 --> 00:08:05,374 word in the file called eliane-david 141 00:08:05,482 --> 00:08:07,002 and it will output on the screen. 142 00:08:07,076 --> 00:08:09,342 You could actually redirect that output using a 143 00:08:09,356 --> 00:08:11,274 tee command to another file as well. 144 00:08:11,432 --> 00:08:14,790 Now, let's say if you want to do ls -l, 145 00:08:14,840 --> 00:08:17,998 and you see that listing, entire listing of your directory. 146 00:08:18,094 --> 00:08:22,074 So let's say if you wanted to have all that, with 147 00:08:22,112 --> 00:08:25,242 the tee, meaning I wanted to view it, and at the 148 00:08:25,256 --> 00:08:26,878 same time I want to put it to a file. 149 00:08:26,914 --> 00:08:32,013 So I'll put it as, name it listdir, 150 00:08:33,481 --> 00:08:37,085 and hit enter, and you will see the entire listing, 151 00:08:38,085 --> 00:08:40,322 from here till bottom, 152 00:08:41,688 --> 00:08:45,409 and you'll also see your file listdir. 153 00:08:45,409 --> 00:08:48,464 If you do cat listdir, and 154 00:08:48,464 --> 00:08:51,022 hit enter, and you will see the same listing. 155 00:08:51,166 --> 00:08:54,498 So let's clear the screen, and now hit up 156 00:08:54,524 --> 00:08:57,666 arrow key to cat listdir file again, 157 00:08:57,728 --> 00:09:00,874 and you will see that ls -l output. 158 00:09:01,042 --> 00:09:04,254 So basically it's outputting on the screen 159 00:09:04,412 --> 00:09:07,710 and it is outputting to a file. 160 00:09:08,090 --> 00:09:11,926 If you wanted to do the same thing, let's 161 00:09:11,938 --> 00:09:17,038 say echo ls -l, and you could also output 162 00:09:17,074 --> 00:09:21,618 to multiple files, meaning ls -l tee, to 163 00:09:21,704 --> 00:09:27,714 let's say file1, file2, file3, and hit 164 00:09:27,752 --> 00:09:36,042 enter, you see it actually ran and it has all 165 00:09:36,056 --> 00:09:38,300 the listing of all three files as well. 166 00:09:38,990 --> 00:09:42,762 And when I do cat file1, it has the 167 00:09:42,776 --> 00:09:47,274 same content, cat file2, it has the same content, 168 00:09:47,432 --> 00:09:52,630 and so on, to cat file3, same ls -l output. 169 00:09:52,750 --> 00:09:55,938 So this is how the tee command works. 170 00:09:56,024 --> 00:09:58,866 If you wanted to get more help or more options 171 00:09:58,988 --> 00:10:04,578 on the tee command, you simply type tee --help, and 172 00:10:04,604 --> 00:10:08,814 you'll see it will tell you exactly the options that 173 00:10:08,852 --> 00:10:12,154 is being offered by the help command. 174 00:10:12,262 --> 00:10:15,066 See -a is to append, and -I 175 00:10:15,128 --> 00:10:18,534 is to ignore, and version, and help. 176 00:10:18,692 --> 00:10:25,806 So if you type tee --version to see the version of 177 00:10:25,868 --> 00:10:30,774 this command, you will also see that this command version is 178 00:10:30,812 --> 00:10:34,810 8.22, and it tells you this is a free software. 179 00:10:34,990 --> 00:10:37,470 You are free to change and redistribute it. 180 00:10:37,520 --> 00:10:40,818 There is NO WARRANTY, to extend the permitted by law. 181 00:10:40,904 --> 00:10:41,874 And so on, and so on. 182 00:10:41,912 --> 00:10:44,694 Written by Mike Parker, Richard M. 183 00:10:44,732 --> 00:10:47,218 Stallman, and David MacKenzie. 184 00:10:47,374 --> 00:10:48,390 So these are the people 185 00:10:48,440 --> 00:10:50,062 who actually built this command. 186 00:10:50,146 --> 00:10:53,574 Again, since Linux is an open source, you could 187 00:10:53,612 --> 00:10:56,646 even create your own command, write your own code, 188 00:10:56,828 --> 00:10:59,120 and introduce it to the Linux world. 189 00:10:59,870 --> 00:11:02,660 So that's how the tee command works. 190 00:11:03,050 --> 00:11:04,254 I hope you understand. 191 00:11:04,352 --> 00:11:06,498 If you have any questions, you could just shoot me an 192 00:11:06,524 --> 00:11:11,346 email and try it out, try it with different options, a 193 00:11:11,348 --> 00:11:14,946 couple of options, and I'm sure it will work just fine. 194 00:11:14,946 --> 00:11:17,718 [No audio]