truncate: How to clear a file in Linux?

truncate can be used to shrink or increase the size of a file. truncate with the option -s can be used to specify the size of the file. When you specify the size as 0, you can clear the contents of the file.


$ cat file.txt

This file has some content. Let's use truncate to clear the file

Let’s clear the file contents


$ truncate -s 0 file.txt

If you specify the size as 0, the contents of the file will be cleared. Let’s confirm this


$ stat file.txt

File: `file.txt'
 Size: 0             Blocks: 0          IO Block: 4096   regular empty file
Device: 801h/2049d    Inode: 1004        Links: 1

You can see more options to clear a file in Linux.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s