1 00:00:00,000 --> 00:00:03,250 [No audio] 2 00:00:03,250 --> 00:00:06,750 Text to files or redirects. 3 00:00:07,490 --> 00:00:12,394 Here we will learn how can we add text or digits 4 00:00:12,442 --> 00:00:16,341 or letters, or how can you populate an empty file. 5 00:00:16,486 --> 00:00:21,102 We created some of the empty files using touch command and 6 00:00:21,176 --> 00:00:24,442 touch is just a way to create an empty file. 7 00:00:24,466 --> 00:00:28,186 But now how can we add text to those files 8 00:00:28,258 --> 00:00:30,860 or anything that you like to add to it? 9 00:00:31,490 --> 00:00:34,830 There are three simple ways to add text to a file 10 00:00:35,150 --> 00:00:40,054 and the basic main way is to add text to files. 11 00:00:40,102 --> 00:00:42,730 The first one is through the vi editor. 12 00:00:42,910 --> 00:00:45,330 vi is the editor. 13 00:00:46,010 --> 00:00:49,554 It's a command that is used in Linux to 14 00:00:49,592 --> 00:00:52,580 create a file and add text messages to it. 15 00:00:52,910 --> 00:00:58,040 Then the next one is redirect command output using the 16 00:00:59,150 --> 00:01:04,078 right direct and the two right directs to a file. 17 00:01:04,233 --> 00:01:07,558 And in this lesson, we'll cover these redirects 18 00:01:07,714 --> 00:01:11,254 through a command output into a file. 19 00:01:11,362 --> 00:01:13,942 And the second way we will learn through echo, 20 00:01:14,026 --> 00:01:16,410 when you want to echo whatever that you 21 00:01:16,460 --> 00:01:19,798 type, you could output to a file. 22 00:01:19,894 --> 00:01:22,738 That's how the file gets created or that's 23 00:01:22,774 --> 00:01:24,894 how the file gets populated with information. 24 00:01:24,992 --> 00:01:27,534 So let's look on our 25 00:01:27,572 --> 00:01:29,910 Linux machine and run these commands. 26 00:01:29,910 --> 00:01:34,590 [No audio] 27 00:01:34,590 --> 00:01:36,194 So here we have our Linux machine. 28 00:01:36,242 --> 00:01:38,630 Let's make sure we are in our home directory. 29 00:01:38,690 --> 00:01:40,274 Yes, we are in our home directory. 30 00:01:40,322 --> 00:01:44,134 Let's clear the screen and let's check what are 31 00:01:44,172 --> 00:01:47,618 the different files we have in our home directory. 32 00:01:47,774 --> 00:01:50,810 Okay, we have all those different files 33 00:01:50,870 --> 00:01:53,378 and directories that we created earlier. 34 00:01:53,534 --> 00:01:56,074 Now we wanted to add something 35 00:01:56,172 --> 00:01:58,898 to a file, any empty file. 36 00:01:58,934 --> 00:02:00,910 Because if you see here, these are the few 37 00:02:00,960 --> 00:02:04,378 files that we added and all of them have 38 00:02:04,464 --> 00:02:08,942 zero bytes in it because these are empty files. 39 00:02:09,086 --> 00:02:16,920 So when you want to do echo and you type 40 00:02:17,550 --> 00:02:25,750 hi my name is Imran, and you hit enter, it 41 00:02:25,800 --> 00:02:29,494 brings you back the exact same thing that you type. 42 00:02:29,652 --> 00:02:32,954 So the command echo actually echoes 43 00:02:33,002 --> 00:02:35,820 back what you shouted out. 44 00:02:36,150 --> 00:02:41,294 So how about if we output this to a file? 45 00:02:41,402 --> 00:02:50,315 So I will say let's say, echo "Jerry seinfled 46 00:02:50,315 --> 00:03:00,266 is the main character in the show Seinfled". 47 00:03:00,398 --> 00:03:02,710 So this is the text that I wanted to type, 48 00:03:02,820 --> 00:03:06,218 I wanted to add into the file called jerry. 49 00:03:06,314 --> 00:03:12,566 And how we're going to do it? We're going do it by redirecting, the redirect, 50 00:03:12,758 --> 00:03:17,710 which is a key right above the dot, 51 00:03:18,450 --> 00:03:21,358 you type that with the shift and then you 52 00:03:21,384 --> 00:03:24,626 specify the name of the file which is jerry. 53 00:03:24,818 --> 00:03:27,394 Hit enter, you'll get your prompt back. 54 00:03:27,552 --> 00:03:32,102 Now when you do ls-l and you hit enter, you're 55 00:03:32,126 --> 00:03:37,082 going to see jerry has 58 bytes inside of that file. 56 00:03:37,106 --> 00:03:38,794 Now all other files are 0 because 57 00:03:38,832 --> 00:03:40,438 we didn't put anything in there. 58 00:03:40,584 --> 00:03:41,518 Now that's good. 59 00:03:41,544 --> 00:03:45,158 We added something to our first file. 60 00:03:45,254 --> 00:03:46,486 This is amazing. 61 00:03:46,668 --> 00:03:50,870 Okay, now how do we know what's inside of this file? 62 00:03:50,930 --> 00:03:52,978 How do we check it? How do we verify it? 63 00:03:53,064 --> 00:03:57,974 We do know that the cd gets into our directory, 64 00:03:58,082 --> 00:04:02,242 lists all the files, and pwd tells us where we are. 65 00:04:02,256 --> 00:04:03,334 But how are we going to see 66 00:04:03,372 --> 00:04:06,418 what's inside of directory in Windows? How do you do it? 67 00:04:06,504 --> 00:04:08,042 How do you open up a file? 68 00:04:08,186 --> 00:04:12,134 Well, you do a double click and it opens up. In Linux, 69 00:04:12,182 --> 00:04:14,194 there is no double click. Then how do you do it? 70 00:04:14,232 --> 00:04:19,238 There is a command called cat. cat is a 71 00:04:19,264 --> 00:04:22,974 command to read the content of a file. 72 00:04:23,142 --> 00:04:25,766 We'll cover the cat command a little 73 00:04:25,827 --> 00:04:27,678 more in detail in the next lessons. 74 00:04:27,774 --> 00:04:30,926 But for now that we want to know what's inside 75 00:04:31,048 --> 00:04:34,446 of the file, we will use the command cat, followed 76 00:04:34,458 --> 00:04:38,150 by the name of the file which is jerry. 77 00:04:38,650 --> 00:04:42,146 When you hit Enter and there you see, you have 78 00:04:42,208 --> 00:04:46,650 Jerry seinfeld is the main character in the show Seinfeld. 79 00:04:46,830 --> 00:04:50,150 Now this is what we typed right here in our 80 00:04:50,200 --> 00:04:54,950 echo and we outputted that to a file called jerry. 81 00:04:55,330 --> 00:04:57,966 Now what happened if we do, echo and 82 00:04:57,966 --> 00:04:59,993 [No audio] 83 00:04:59,993 --> 00:05:00,994 echo, 84 00:05:02,055 --> 00:05:03,424 and do, 85 00:05:04,597 --> 00:05:09,290 and we wanted to type another line in that same file. 86 00:05:10,030 --> 00:05:18,350 So let's say Jerry show was created 87 00:05:18,350 --> 00:05:21,276 [No audio] 88 00:05:21,276 --> 00:05:23,829 in 1989, 89 00:05:25,414 --> 00:05:27,466 and you output, 90 00:05:27,466 --> 00:05:30,587 [No audio] 91 00:05:30,587 --> 00:05:32,189 to a file jerry. 92 00:05:32,790 --> 00:05:37,030 Remember why we are using two output arrows here, 93 00:05:37,140 --> 00:05:41,940 it is because if you use one arrow like this, 94 00:05:42,450 --> 00:05:48,370 it will overwrite your last file and remove this 95 00:05:48,420 --> 00:05:51,026 line that we added into this file. 96 00:05:51,158 --> 00:05:53,906 You'll remove this line and create a new file 97 00:05:53,978 --> 00:05:58,110 with the content of this Jerry show was created in 1989. 98 00:05:58,110 --> 00:06:00,118 Now, we don't want to remove it. 99 00:06:00,204 --> 00:06:03,454 We want to keep this line, the first line, 100 00:06:03,552 --> 00:06:05,782 and we want to add a second line. 101 00:06:05,916 --> 00:06:07,390 If you want to add a second line, 102 00:06:07,440 --> 00:06:11,474 then you need to use the double redirect 103 00:06:11,522 --> 00:06:14,700 signs. When you do that, hit Enter. 104 00:06:15,930 --> 00:06:19,246 How do you verify what's inside or whether 105 00:06:19,308 --> 00:06:21,346 it went inside the file or not? 106 00:06:21,408 --> 00:06:27,062 Again, I want you to use up arrow key twice. 107 00:06:27,146 --> 00:06:29,434 Because the first up arrow key 108 00:06:29,472 --> 00:06:30,734 brings you the last command. 109 00:06:30,842 --> 00:06:33,062 The second time you hit up arrow key brings 110 00:06:33,086 --> 00:06:35,138 you the command to cat the jerry file. 111 00:06:35,234 --> 00:06:37,354 Hit Enter and there you go. 112 00:06:37,392 --> 00:06:41,410 Now you have two lines in your file. 113 00:06:42,030 --> 00:06:45,674 When you do ls -l, last time we had 58 bytes. 114 00:06:45,722 --> 00:06:49,750 Now we have 89 bytes because the file keeps growing. 115 00:06:50,370 --> 00:06:56,974 Now this is the simple way to add contents to 116 00:06:57,072 --> 00:07:01,860 a file, if we running the same command again. 117 00:07:01,860 --> 00:07:04,810 [No audio] 118 00:07:04,810 --> 00:07:08,500 There you go. Jerry seinfeld is the main character in the show. 119 00:07:09,550 --> 00:07:12,854 If you take out that and we just 120 00:07:12,952 --> 00:07:15,878 do Jerry seinfeld is the main character, right? 121 00:07:16,024 --> 00:07:18,098 And now we want to use one 122 00:07:18,184 --> 00:07:21,414 redirect and we want to redirect jerry. 123 00:07:21,522 --> 00:07:24,626 Now remember this, it will wipe out 124 00:07:24,688 --> 00:07:27,474 all the contents of the file jerry. 125 00:07:27,582 --> 00:07:29,658 Basically it will wipe out the entire file 126 00:07:29,694 --> 00:07:33,266 jerry, recreate the file with the new text 127 00:07:33,328 --> 00:07:36,830 message text that I am typing here. Try it. 128 00:07:36,880 --> 00:07:38,140 Let's say hit Enter. 129 00:07:38,590 --> 00:07:44,174 Do cat jerry, enter, and there you go. 130 00:07:44,212 --> 00:07:47,166 Jerry seinfeld is the main character. 131 00:07:47,298 --> 00:07:50,066 It does not have all the typing that we did 132 00:07:50,128 --> 00:07:55,382 earlier, it took the latest one, the latest typing, the 133 00:07:55,396 --> 00:07:58,686 latest echo we did in the file, and it added 134 00:07:58,758 --> 00:08:01,586 into the jerry file. The same way 135 00:08:01,648 --> 00:08:03,698 I want you to add a few things 136 00:08:03,784 --> 00:08:07,194 about anything that you know about these characters. 137 00:08:07,242 --> 00:08:09,674 By the way, these characters are actually taken from 138 00:08:09,712 --> 00:08:15,018 those hit shows, american hit shows of Seinfeld, Simpsons, 139 00:08:15,114 --> 00:08:18,246 and of course, the main movie Superman. 140 00:08:18,438 --> 00:08:20,354 If you know a little bit about any 141 00:08:20,392 --> 00:08:23,618 of these characters, please do write inside of 142 00:08:23,644 --> 00:08:26,406 these files with using the echo command. 143 00:08:26,538 --> 00:08:29,514 And this would be a very good exercise 144 00:08:29,562 --> 00:08:32,717 for you, how you could populate each file 145 00:08:32,813 --> 00:08:34,780 with different things that you know about. 146 00:08:35,409 --> 00:08:38,919 Again, remember, if you do one single 147 00:08:39,250 --> 00:08:41,861 output, that will overwrite the file. 148 00:08:41,885 --> 00:08:46,926 If you do double, that will append to the file. 149 00:08:47,058 --> 00:08:49,169 Okay, now, this is about echo. 150 00:08:49,289 --> 00:08:52,938 What if you wanted to do, let's create a new file 151 00:08:52,974 --> 00:08:54,929 called touch, 152 00:08:54,929 --> 00:08:58,600 and this time we'll call it, 153 00:08:58,600 --> 00:09:01,303 [No audio]. 154 00:09:01,303 --> 00:09:04,006 listingoffile. 155 00:09:04,438 --> 00:09:06,166 No, listingofdir. 156 00:09:06,238 --> 00:09:07,830 Let's say we call this, right. 157 00:09:08,000 --> 00:09:11,922 You hit enter, verify, the file is created. Yes. 158 00:09:12,056 --> 00:09:13,390 listingofdir. 159 00:09:13,450 --> 00:09:14,422 It's just the name I picked. 160 00:09:14,446 --> 00:09:15,594 You could pick any name. 161 00:09:15,752 --> 00:09:18,274 Now, how do I populate that file 162 00:09:18,322 --> 00:09:21,080 with whatever I want to type in? 163 00:09:21,530 --> 00:09:23,238 Of course, you know, that way you could 164 00:09:23,264 --> 00:09:25,458 do it is through echo and whatever you 165 00:09:25,484 --> 00:09:26,998 want to type and output to a file. 166 00:09:27,034 --> 00:09:29,862 But now I want to show you a second way that you 167 00:09:29,876 --> 00:09:33,942 could populate through the output of a command, which is, if I 168 00:09:33,956 --> 00:09:39,030 do ls -l, hit enter or ls -ltr, let's say, 169 00:09:39,080 --> 00:09:41,120 which brings me the latest one at the bottom, 170 00:09:41,510 --> 00:09:44,430 I wanted to have this entire 171 00:09:44,600 --> 00:09:47,526 output. Starting from here to here, 172 00:09:47,648 --> 00:09:52,014 I wanted to output this entire result 173 00:09:52,112 --> 00:09:54,574 of my ls -ltr into the file 174 00:09:54,622 --> 00:09:58,306 I created listingofdir. 175 00:09:58,438 --> 00:10:04,894 So we will do ls -ltr, output to listing. 176 00:10:05,002 --> 00:10:09,082 Hit tab, it auto completes your file, 177 00:10:09,226 --> 00:10:10,880 and now you hit enter. 178 00:10:10,880 --> 00:10:12,830 [No audio] 179 00:10:12,830 --> 00:10:14,490 You see what just happened? 180 00:10:14,660 --> 00:10:17,482 It did not give you the output 181 00:10:17,506 --> 00:10:20,390 of ls -ltr on your screen. 182 00:10:20,390 --> 00:10:22,086 What it did, instead of giving you the 183 00:10:22,088 --> 00:10:24,922 the screen, it is outputting to a file, 184 00:10:25,066 --> 00:10:27,370 you call listingofdir. 185 00:10:27,490 --> 00:10:31,866 Now ls -ltr, shows you that the total 186 00:10:31,928 --> 00:10:34,880 number of bytes this file has is 1140. 187 00:10:35,270 --> 00:10:36,954 Okay, awesome. 188 00:10:37,052 --> 00:10:40,254 Now, what's inside of this 189 00:10:40,292 --> 00:10:43,870 file? You could check by typing the command 190 00:10:43,930 --> 00:10:49,820 cat listing, tab, and hit enter. 191 00:10:50,390 --> 00:10:54,774 You see guys, it's the same output that you 192 00:10:54,812 --> 00:11:00,080 have as you would have it on this one. 193 00:11:00,410 --> 00:11:02,370 The only difference that you see this 194 00:11:02,420 --> 00:11:04,686 output gives you a different color because 195 00:11:04,868 --> 00:11:06,694 it is actually running the command. 196 00:11:06,802 --> 00:11:12,282 Whereas this output cat of this listing is just 197 00:11:12,476 --> 00:11:18,226 the simple text messages, text inside of a file. 198 00:11:18,358 --> 00:11:20,002 You could change it, you can update 199 00:11:20,026 --> 00:11:21,570 it, whatever you want to do. 200 00:11:21,740 --> 00:11:28,846 If you want to do date, and you want to append 201 00:11:28,918 --> 00:11:33,582 that date, by the way, the output of date is today's date. 202 00:11:33,776 --> 00:11:35,826 And if you want to append that 203 00:11:35,888 --> 00:11:42,710 to the file jerry, hit enter. 204 00:11:42,710 --> 00:11:48,198 Now verify jerry, you see, it has the first line that we 205 00:11:48,224 --> 00:11:51,390 type through echo command, and then it has the second 206 00:11:51,440 --> 00:11:53,900 line that we got it from the date. 207 00:11:54,230 --> 00:12:03,690 So these two, funny characters, the two outputs, appends to 208 00:12:03,740 --> 00:12:06,634 your existing file, and does not delete your file. 209 00:12:06,742 --> 00:12:09,606 So, again, these are the two ways that we learn. 210 00:12:09,668 --> 00:12:12,198 One through the echo and redirecting, and one through the 211 00:12:12,224 --> 00:12:15,382 output of a command and redirecting to a file. 212 00:12:15,526 --> 00:12:17,778 And, of course, there's another way that 213 00:12:17,804 --> 00:12:20,290 we'll cover later in the lessons. 214 00:12:20,410 --> 00:12:23,286 But now, right now, you have to practice these 215 00:12:23,348 --> 00:12:26,670 two ways of adding or appending to a file. 216 00:12:26,670 --> 00:12:31,936 [No audio]