1 00:00:00,000 --> 00:00:02,734 Let's look at truncating file 2 00:00:02,782 --> 00:00:05,910 size using the command truncate. 3 00:00:06,650 --> 00:00:11,266 Now the Linux truncate command is often used to shrink 4 00:00:11,338 --> 00:00:16,640 or extend the size of a file to specified size. 5 00:00:16,970 --> 00:00:21,414 Now remember one thing, truncate is not the same command as 6 00:00:21,452 --> 00:00:24,694 if you wanted to compress a file or gzip a file 7 00:00:24,802 --> 00:00:29,370 which will reduce or shrink the size of a file. 8 00:00:29,810 --> 00:00:32,537 truncate on the other hand, will actually 9 00:00:32,624 --> 00:00:36,750 chop the file into the specified size. 10 00:00:36,800 --> 00:00:42,198 So if your file size is, let's say 100 bytes and 11 00:00:42,224 --> 00:00:46,482 you use the command truncate and shrink it down to 50, 12 00:00:46,616 --> 00:00:50,782 then it will actually chop the file and you will lose 13 00:00:50,866 --> 00:00:55,290 the data or the content of a file. 14 00:00:55,850 --> 00:00:58,198 If you do cat to the file, you'll 15 00:00:58,234 --> 00:01:00,678 see half of your data is gone if 16 00:01:00,704 --> 00:01:04,522 you are chopping using the command truncate. 17 00:01:04,666 --> 00:01:07,890 So the command that you will need to use to run it 18 00:01:07,940 --> 00:01:12,882 is truncate -s, s for size, which is the option. 19 00:01:13,076 --> 00:01:16,678 Ten is the actual integer to the specified 20 00:01:16,774 --> 00:01:20,058 file, whichever file that you want truncate to. 21 00:01:20,144 --> 00:01:23,142 So this size option is everything that is there. 22 00:01:23,216 --> 00:01:26,358 So now let's go into our Linux machine and we 23 00:01:26,384 --> 00:01:30,200 will try our command, the truncate command over there. 24 00:01:30,200 --> 00:01:39,330 [No audio] 25 00:01:39,330 --> 00:01:43,742 Okay, so we are in our Linux machine to practice 26 00:01:43,886 --> 00:01:48,550 our truncate command or to go over it. I will 27 00:01:48,600 --> 00:01:52,558 create a file called, by the way I am in 28 00:01:52,584 --> 00:01:55,980 my home directory and I am logged in as myself, 29 00:01:56,550 --> 00:02:00,286 and my hostname is my Linux machine MyFirstLinuxVM of course. 30 00:02:00,468 --> 00:02:03,202 Okay, and now I want you to create 31 00:02:03,276 --> 00:02:11,967 a file, touch and call it seinfeldwords. 32 00:02:13,710 --> 00:02:17,947 The file is created, you could verify by doing ls -l, 33 00:02:17,947 --> 00:02:21,790 and the name of the file, and hit enter. 34 00:02:21,840 --> 00:02:26,578 You will see the file is there, it has zero bytes and 35 00:02:26,604 --> 00:02:29,122 it's owned by me and the permission and all that stuff. 36 00:02:29,256 --> 00:02:37,702 Now do echo and type the famous characters of the 37 00:02:37,716 --> 00:02:40,442 famous most words that you could think of Seinfeld. 38 00:02:40,466 --> 00:02:42,202 But if you don't know then just 39 00:02:42,276 --> 00:02:44,906 follow my lead and just keep typing. 40 00:02:44,978 --> 00:02:48,565 Like the first word is puffyshirt, 41 00:02:48,565 --> 00:02:53,153 [No audio] 42 00:02:53,153 --> 00:02:56,240 second word is giddyup, 43 00:02:56,240 --> 00:03:08,158 the third word is yadayada kavaorka serenitynow, 44 00:03:08,158 --> 00:03:11,930 and last one let's say festivus, 45 00:03:12,670 --> 00:03:18,774 and put that to our seinfeldwords file. 46 00:03:18,822 --> 00:03:20,310 So let's make it a little bigger. 47 00:03:20,370 --> 00:03:22,622 So you'll see all in one line. 48 00:03:22,816 --> 00:03:26,870 So I'm actually echoing out these words and I am putting 49 00:03:26,920 --> 00:03:31,982 it to the file name, the file that we just created, hit enter. 50 00:03:32,176 --> 00:03:35,882 Now go ahead and do cat on seinfeldwords and 51 00:03:35,896 --> 00:03:41,622 you will see all of these words inside of that file. 52 00:03:41,766 --> 00:03:46,742 Now do ls -l, and do seinfeldwords, and you 53 00:03:46,756 --> 00:03:50,620 will see the size of the file is now 57. 54 00:03:51,190 --> 00:03:54,458 Now if you want to shrink that file using the 55 00:03:54,484 --> 00:03:58,655 command truncate, then you would have to use, 56 00:03:58,655 --> 00:04:01,441 [No audio] 57 00:04:01,441 --> 00:04:03,126 command truncate 58 00:04:05,310 --> 00:04:12,494 -s as an option, space the size that you 59 00:04:12,532 --> 00:04:17,077 want to specify, and you want to shrink it to. 60 00:04:17,164 --> 00:04:19,370 So right now it's 57. 61 00:04:19,480 --> 00:04:22,394 Now let's make it 40, and the 62 00:04:22,432 --> 00:04:25,420 name of the files and hit Enter. 63 00:04:25,870 --> 00:04:29,642 Now do ls -l to seinfeld again and you 64 00:04:29,656 --> 00:04:32,646 will see the size has been reduced to 40 bytes. 65 00:04:32,778 --> 00:04:38,800 Now let's see the content of the file now, seinfeldwords. 66 00:04:38,800 --> 00:04:41,870 [No audio] 67 00:04:41,870 --> 00:04:44,634 Okay, you see the content right now, when 68 00:04:44,672 --> 00:04:48,783 I did cat, it says puffyshirt giddyup 69 00:04:48,783 --> 00:04:54,140 yadayada kavorka and sere, up to here. That's it. 70 00:04:54,710 --> 00:04:58,346 It did not complete the entire word serenitynow 71 00:04:58,346 --> 00:05:00,930 see right here, as I have right here, serenitynow. 72 00:05:00,980 --> 00:05:04,170 And then it actually removed festivus as well. 73 00:05:04,340 --> 00:05:09,354 So when I do cat again, seinfeld, you see 74 00:05:09,392 --> 00:05:13,042 right here, it actually removed the last few letters. 75 00:05:13,126 --> 00:05:15,560 Why? Because it has to chop it down. 76 00:05:16,430 --> 00:05:19,582 That's why we would use the truncate command 77 00:05:19,666 --> 00:05:22,210 if I wanted to chop the file. 78 00:05:22,330 --> 00:05:26,586 Now remember, truncate command is not 79 00:05:26,648 --> 00:05:30,366 used to actually compress or size 80 00:05:30,428 --> 00:05:33,342 make it smaller while keeping the same content. 81 00:05:33,416 --> 00:05:34,894 So be very careful. 82 00:05:35,062 --> 00:05:36,798 Anyway, so this is the lesson where 83 00:05:36,824 --> 00:05:38,590 I wanted to cover the truncate command. 84 00:05:38,650 --> 00:05:41,010 You could use this, or you could also use 85 00:05:41,120 --> 00:05:44,554 the truncate to extend or make the file bigger. 86 00:05:44,602 --> 00:05:47,778 So if I do truncate, hit up arrow key and 87 00:05:47,804 --> 00:05:52,254 I put it back to, let's say 60 and hit Enter. 88 00:05:52,352 --> 00:05:55,650 And now do ls -l seinfeldwords, 89 00:05:56,030 --> 00:05:57,546 you see, the size is 60. 90 00:05:57,608 --> 00:06:00,006 Now, it doesn't mean that we're going to get our 91 00:06:00,068 --> 00:06:04,918 whole word serenitynow and festivus back in that file. 92 00:06:05,074 --> 00:06:07,126 No, let's check, let's verify. 93 00:06:07,198 --> 00:06:10,494 Do cat, seinfeldwords and you see it's not there. 94 00:06:10,532 --> 00:06:17,166 It's still giving me, only the size that has all those 95 00:06:17,228 --> 00:06:20,706 words, because it does not have the information whatever that we 96 00:06:20,768 --> 00:06:24,030 lost while we did the truncate to shrink it. 97 00:06:24,200 --> 00:06:25,890 So it did extend it. 98 00:06:25,940 --> 00:06:27,246 But now where is that 99 00:06:27,308 --> 00:06:30,366 extension thing, that's been added? Where did it go? 100 00:06:30,428 --> 00:06:33,606 Well, it's just the empty letters, that actually, 101 00:06:33,668 --> 00:06:35,890 the truncate has added to this file. 102 00:06:36,010 --> 00:06:40,090 It is @ letter signs just to make it bigger. 103 00:06:40,210 --> 00:06:44,158 Right now we do not know how to use vi editor. 104 00:06:44,254 --> 00:06:46,234 So by the time we'll use the vi 105 00:06:46,282 --> 00:06:47,998 editor, you will know how to vi. 106 00:06:48,034 --> 00:06:50,358 But for now, just to show you, we 107 00:06:50,384 --> 00:06:54,258 could do vi seinfeldwords, and hit enter, 108 00:06:54,344 --> 00:06:58,354 and you will see right here all these @ signs. 109 00:06:58,402 --> 00:07:01,086 These all @ signs were added because when we 110 00:07:01,148 --> 00:07:04,102 extended the file, so operating system said, now you're 111 00:07:04,126 --> 00:07:06,450 extending, I have to put something in there, right? 112 00:07:06,560 --> 00:07:08,322 But I don't know what it had before, but 113 00:07:08,336 --> 00:07:10,654 I'm going to add something to make it bigger. 114 00:07:10,822 --> 00:07:12,462 So that's why you see it. Anyway, 115 00:07:12,536 --> 00:07:15,138 so if you're using vi right now, and you 116 00:07:15,164 --> 00:07:19,919 want to get out, hit Escape, shift, colon, q 117 00:07:19,919 --> 00:07:22,071 [No audio] 118 00:07:22,071 --> 00:07:26,794 q, bang, which is right above number one, and hit 119 00:07:26,832 --> 00:07:28,946 enter, and you will get out of vi. 120 00:07:29,018 --> 00:07:31,526 Anyway, don't get into vi right now if you're 121 00:07:31,538 --> 00:07:33,694 not familiar with it, we'll cover it later on. 122 00:07:33,732 --> 00:07:35,702 So anyway, I wanted to cover this truncate. 123 00:07:35,786 --> 00:07:37,510 I hope you understand how truncate works 124 00:07:37,560 --> 00:07:43,630 now. Try it out on different files, different contents in the file and 125 00:07:43,680 --> 00:07:44,860 see if you get familiar with it. 126 00:07:44,860 --> 00:07:46,134 [No audio]