Let's look at truncating file
size using the command truncate.
Now the Linux truncate command is often used to shrink
or extend the size of a file to specified size.
Now remember one thing, truncate is not the same command as
if you wanted to compress a file or gzip 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 100 bytes and
you use the command truncate and shrink it down to 50,
then it will actually chop the file and you will 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
is truncate -s, s for size, which is the option.
Ten is the actual integer to the specified
file, whichever file that you want truncate to.
So this size option is everything that is there.
So now let's go into our Linux machine and we
will try our command, the truncate command over there.
[No audio]
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 hostname is my Linux machine MyFirstLinuxVM of course.
Okay, and now I want you to create
a file, 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 will see the file is there, it has zero bytes and
it's owned by me and the permission and all that stuff.
Now do echo and type the famous characters of the
famous most 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,
[No audio]
second word is giddyup,
the third word is yadayada kavaorka serenitynow,
and last one let's say festivus,
and put that to our seinfeldwords 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 seinfeldwords and
you will see all of these words inside of that file.
Now do ls -l, and do seinfeldwords, and you
will 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,
[No audio]
command truncate
-s as an option, space the size that you
want to specify, and you want to shrink it to.
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
will see the size has been reduced to 40 bytes.
Now let's see the content of the file now, seinfeldwords.
[No audio]
Okay, you see the content right now, when
I did cat, it says puffyshirt giddyup
yadayada kavorka and sere, 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 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 is not
used to actually compress or size
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, hit up arrow key and
I put it back to, let's say 60 and hit Enter.
And now do ls -l seinfeldwords,
you see, the size is 60.
Now, it doesn't mean that we're going to get our
whole word serenitynow and festivus back in that file.
No, let's check, let's verify.
Do cat, seinfeldwords and you see it's not there.
It's still giving me, only the size that has all those
words, because it does not have the information whatever that we
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's just the empty letters, that actually,
the truncate has added to this file.
It is @ letter signs 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 seinfeldwords, and hit enter,
and you will see right here all these @ signs.
These all @ signs were added because when we
extended the file, so operating system said, now you're
extending, I have to put something in there, right?
But I don't know what it had before, but
I'm going to add something to make it bigger.
So that's why you see it. Anyway,
so if you're using vi right now, and you
want to get out, hit Escape, shift, colon, q
[No audio]
q, bang, which is right above number one, and hit
enter, and you will get out of vi.
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 on different files, different contents in the file and
see if you get familiar with it.
[No audio]