Gice

Technology and General Blog

Occasionally we require to take out the content material of a file without having deleting the file for that Linux running method gives a command termed “truncate”. It is employed to extend or reduce the file dimension. Truncating a file is a great deal quicker and more simple with no modifying the permissions and possession of the file.

The truncated size relies upon on the initial sizing of the file the further data will be lost if the file sizing is bigger than the specified sizing.

Let’s begin with distinct illustrations to see how we can truncate the file measurement.

Installing Coreutils Deals

The “truncate” command arrives with most Linux distribution. It can also be mounted, if not present, using the command supplied down below:

$ sudo apt-get put in coreutils

Use the “grep” command to list the element of packages:

$ dpkg –l | grep coreutils

 

How to Use the “truncate” Command?

The “>” shell redirection operator is the most common and most basic way to truncate files.

Syntax

The syntax for truncating information with redirection is:

The “:” colon denotes genuine and has no output and the redirection operator “>” redirect the output to a unique file.

The file I am truncating is “test.sh”:

Another way to truncate file is:

$ cat /dev/null > take a look at.sh

It is eradicating the written content of “examination.sh” file.

Apparent the Information of File

Use the “-s” alternative to eliminate the written content of the information. This is a preferable way to manually delete a file. The truncate command correctly removes all the contents of a file. It does not delete the file itelf, but leaves it as a zero-byte file on the disk.

Let us use truncate to very clear file.txt to bytes:

The file permissions and possession will be preserved if you use the truncate command.

Use the “ls -lh” command to validate the size:

Truncating a File to a Unique Size

To make a file, use:

To affirm the file authorization and measurement of the file, use:

Let’s truncate the file to 100 bytes dimension:

$ truncate -s 100 Take a look at.txt

To validate the sizing, use:

To truncate a file size to 300K:

$ truncate -s 300k Test.txt

Kind below stated command to test the dimension:

Extending the File Dimension

You can improve the file dimensions by working with the “+” with “-s” choice. The file is currently 300k in sizing, as proven in the graphic beneath:

I’d like to increase the sizing of the file from 300k to 600k bytes:

$ truncate -s +300k Examination.txt

The file dimension has been extended from 300k to 600k. Verify the size:

Lowering the File Dimension

Let’s assume you have a 600k file and want to decrease its measurement to 270k, use “-s” possibility and “” with the dimensions figured:

$truncate -s -270k Check.txt

The present size of the file is 330k.

Having Help

To get a enable message, use:

Checking Model

To verify the edition of the truncate command, use:

Conclusion:

Truncate is a extremely beneficial command for eradicating the written content of a file when not deleting the file. You can also transform the dimensions of the file to the measurement you want it to be. We have learned how to truncate the articles of a file, as nicely as how to shrink or increase the files in this short article.

Leave a Reply

Your email address will not be published. Required fields are marked *