Serverspace Black Friday
JT
December 26, 2023
Updated December 27, 2023

How To Delete data in File with Truncate?

Linux

Why we change size of file?

The reasons you may need to set a file size explicitly can range from simple programming tasks up to complex security issues, but in general, when writing code, it is common that you are working with specific sizes of data and need them not only be the same across different systems (which often have slightly different interpretations), but also fixed and predictable. This means if your application relies on a certain file size for its functionality to work properly, having explicit control over that file's size can prevent unexpected behavior from arising due to an uncontrolled variable.

Furthermore, having strict controls on the files you create is good practice in general for security reasons; it prevents accidental leaks that may be dangerous or expose sensitive system data, and protects against potential attacks via buffer overflow techniques where a large file overflows memory bounds and allows execution of unauthorized code (a form of remote hacking). Finally, if you're working with some sort of file encryption, the need is more straightforward. The same reason for maintaining control on file size when writing code - it prevents errors due to unexpectedly large files - extends well beyond coding into encryption as both require a degree of precise control over how information is being stored and accessed. Therefore it is crucial that you understand and correctly manage (set, reset, change) any such sizes at all times.

How to use it?

For the Linux OS there is utility that can help to sharp and extend data by simply syntax of usage. Let's consider algorithm and way to use them! First of all we need update package:

apt update && apt upgrade -y
Update
Screenshot №1 — Update

Then we can download needed package through application packet manager, which get data from trusted repositories:

apt install coreutils -y
Installation
Screenshot №2 — Installation

As we can see in the picture above we have already installed software, therefore finally consider syntax: truncate -OPTIONS -FILE. We can sharp file with option size, for example your file is named example.txt and its current size is 5MB, and you want to sharp it to 1MB:

truncate -s -4M example.txt

Option -s indicate size, minus means arithmetical action to current size of file, for make sure we can check list:

ls -l
Minus value of size
Screenshot №3 — Minus value of size

Important alert! If size of file large than new value of size therefore extradata will be delete, don't compress! In another side of hand if size less than value, utility add extra data with zero records.

If we need to indicate certainly value, command will the same as we described above, but we will not use arithmetical sign:

truncate -s 0M example.txt

That command line clear data in the file without their deletion, in another word we get as result empty file! Let's make sure:

ls -l
Empty file
Screenshot №4 — Empty file

Also in another way we can use + for extend file size, slash sign / round down to multiple of and percentage sing % round up to multiple of!

 

Mastering the usage of truncate empowers users to maintain control over file sizes, contributing to more predictable and secure file management practices in Linux environments.

Vote:
5 out of 5
Аverage rating : 5
Rated by: 1
33145 North Miami, FL 2520 Coral Way apt 2-135
+1 302 425-97-76
700 300
ITGLOBAL.COM CORP
700 300

You might also like...

We use cookies to make your experience on the Serverspace better. By continuing to browse our website, you agree to our
Use of Cookies and Privacy Policy.