Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the js_composer domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/holhol2/public_html/wp-includes/functions.php on line 6121
How To Use “Truncate” Command In Linux? – Holhol24 | Linux Tutorials and Guides

How To Use “Truncate” Command In Linux?

5

Sometimes we need to remove the content of a file without deleting the file; for that Linux operating system offers a command called “truncate”. It is used to extend or reduce the file size. Truncating a file is much quicker and simpler without modifying the permissions and ownership of the file.

The truncated size depends on the original size of the file; the extra data will be lost if the file size is greater than the specified size.

Let’s start with different examples to see how we can truncate the file size.

Installing Coreutils Packages

The “truncate” command comes with most Linux distribution. It can also be installed, if not present, using the command given below:

$ sudo apt-get install coreutils

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

$ dpkg –l | grep coreutils

 

How to Use the “truncate” Command?

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

Syntax

The syntax for truncating files with redirection is:

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

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

Another way to truncate file is:

$ cat /dev/null > test.sh

It is removing the content of “test.sh” file.

Clear the Content of File

Use the “-s” option to remove the content of the files. This is a preferable way to manually delete a file. The truncate command effectively eliminates 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’s use truncate to clear file.txt to 0 bytes:

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

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

Truncating a File to a Specific Size

To create a file, use:

To confirm the file permission and size of the file, use:

Let’s truncate the file to 100 bytes size:

$ truncate -s 100 Test.txt

To confirm the size, use:

To truncate a file size to 300K:

$ truncate -s 300k Test.txt

Type below mentioned command to check the size:

Extending the File Size

You can increase the file size by using the “+” with “-s” option. The file is currently 300k in size, as shown in the image below:

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

$ truncate -s +300k Test.txt

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

Reducing the File Size

Let’s assume you have a 600k file and want to reduce its size to 270k, use “-s” option and “” with the size figured:

$truncate -s -270k Test.txt

The current size of the file is 330k.

Getting Help

To get a help message, use:

Checking Version

To check the version of the truncate command, use:

Conclusion:

Truncate is a very useful command for removing the content of a file while not deleting the file. You can also change the size of the file to the size you want it to be. We have learned how to truncate the content of a file, as well as how to shrink or extend the files in this article.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More