Instructor: Let's look at truncating file size
using the command truncate.
Now the Linux truncate command is often used to shrink
or extend this size of a file to a specified size.
Now remember one thing, truncate is not the same command
as if you wanted to compress a file or squeeze up a file,
which will reduce or shrink the size of a file.
Truncate on the other hand, will actually chop the file
into the specified size.
So if your file size is let's say a hundred bytes
and you use the command truncate and shrink it down to 50,
then it will actually chop the file
and you'll lose the data or the content of a file.
If you do cat to the file, you'll see
half of your data is gone if you are chopping
using the command truncate.
So the command that you will need to use to run it,
it's truncate minus S, S for size, which is the option,
10 is the actual integer to the specified file,
whichever file that you wanted to truncate to.
So this is the size option is everything is there.
So now let's go into our Linux machine
and we will try our command,
the truncate command over there.
Okay, so we are in our Linux machine.
To practice our truncate command or to go over it,
I will create a file called,
by the way, I am in my home directory
and I am logged in as myself
and my host name is Linux machine of course.
Okay and now I want you to create a file called touch
and call it Seinfeldwords.
The file is created, you could verify by doing ls -l
and the name of the file and hit enter.
You'll see the file is there, it has zero bytes
and is owned by me and the permission and all that stuff.
Now do echo and type the famous characters of Sein,
oh sorry, the famous words that you could think of Seinfeld
but if you don't know, then just follow my lead
and just keep typing like the first word is puffyshirt,
second word is giddyup,
third word is yadayada, kavorka, serenitynow,
and last one, let's say festivus
and put that to our Seinfeld file.
So let's make it a little bigger
so you'll see all in one line.
So I'm actually echoing out these words
and I am putting it to the file name,
the file that we just created.
Hit enter.
Now go ahead and do cat on Seinfeld words
and you'll see all of these words inside of that file.
Now do ls -l and do Seinfeld
and you'll see the size of the file is now 57.
Now if you want to shrink that file
using the command truncate,
then you would have to use command truncate -s as an option,
space, the size that you wanted to specify
that you want to shrink it too.
So right now it's 57.
Now let's make it 40 and the name of the files
and hit enter.
Now do ls -l to Seinfeld again,
and you'll see the size has been reduced to 40 bytes.
Now let's see the contents of the file now.
Seinfeld words.
Okay, you see the content right now when I did cat.
It says puffyshirt, giddyup, yadayada, kavorka
and S-E-R-E, up to here.
That's it.
It did not complete the entire word serenitynow.
See right here as I have right here, serenitynow,
and then it actually removed Festivus as well.
So it actually, so when I do cat again Seinfeld,
you see right here it actually removed the last few letters.
Why? Because it has to chop it down.
That's why we would use the truncate command
if I wanted to chop the file.
Now remember, truncate command does not, is not used
to actually compress or make it smaller
while keeping the same content so be very careful.
Anyway, so this is the lesson
where I wanted to cover the truncate command.
You could use this or you could also use the truncate
to extend or make the file bigger.
So if I do truncate to hit up arrow key
and I put it back to let's say 60 and hit enter
and now do ls -l Seinfeld, you'll see the size 60.
Now, does it mean
that we are gonna get our whole word serenitynow
and Festivus back in that file?
No, let's check, let's verify do cat Seinfeld's words.
And you see it's not there.
It's still giving me the only the size
that has all those words
because it does not have the information,
whatever, that we even lost while we did the truncate
to shrink it.
So it did extend it, but now where is that extension thing
that's been added?
Where did it go?
Well, it was just empty letters
that actually the truncate has added to this file.
It is the at letter signs to just to make it bigger.
Right now we do not know how to use VI editor.
So by the time we'll use the VI editor,
you will know how to VI but for now, just to show you,
we could do VI Seinfeld and hit enter
and you'll see right here, all these at signs,
these all at signs were added
because when we extended the file, so operating system said,
now you're extending,
I have to put it something in there, right?
But I don't know what it had before,
but I'm gonna add something so make it bigger.
So that's why you see it.
Anyway, so if you are using VI right now
and you want to get out, hit escape shift colon
Q Bang, which is right above number one, and hit enter
and you will get out of VI.
So anyway, don't get into VI right now.
If you're not familiar with it, we'll cover it later on.
So anyway, I wanted to cover this truncate.
I hope you understand how truncate works now.
Try it out with different files,
different contents in a file
and see if you get familiar with it.