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 “Truncate” Command In Linux? How To Use “Truncate” Command In Linux? 1625341637 166 How To Use Truncate Command In Linux

 

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”:

How To Use “Truncate” Command In Linux? How To Use “Truncate” Command In Linux? 1625341638 606 How To Use Truncate Command In Linux

Another way to truncate file is:

$ cat /dev/null > test.sh

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

How To Use “Truncate” Command In Linux? How To Use “Truncate” Command In Linux? 1625341638 448 How To Use Truncate Command In Linux

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:

How To Use “Truncate” Command In Linux? How To Use “Truncate” Command In Linux? 1625341639 708 How To Use Truncate Command In Linux

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

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

How To Use “Truncate” Command In Linux? How To Use “Truncate” Command In Linux? 1625341639 90 How To Use Truncate Command In Linux

Truncating a File to a Specific Size

To create a file, use:

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

How To Use “Truncate” Command In Linux? How To Use “Truncate” Command In Linux? 1625341640 149 How To Use Truncate Command In Linux

Let’s truncate the file to 100 bytes size:

$ truncate -s 100 Test.txt

How To Use “Truncate” Command In Linux? How To Use “Truncate” Command In Linux? 1625341640 991 How To Use Truncate Command In Linux

To confirm the size, use:

How To Use “Truncate” Command In Linux? How To Use “Truncate” Command In Linux? 1625341640 709 How To Use Truncate Command In Linux

To truncate a file size to 300K:

$ truncate -s 300k Test.txt

How To Use “Truncate” Command In Linux? How To Use “Truncate” Command In Linux? 1625341641 364 How To Use Truncate Command In Linux

Type below mentioned command to check the size:

How To Use “Truncate” Command In Linux? How To Use “Truncate” Command In Linux? 1625341642 59 How To Use Truncate Command In Linux

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:

How To Use “Truncate” Command In Linux? How To Use “Truncate” Command In Linux? 1625341642 831 How To Use Truncate Command In Linux

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

$ truncate -s +300k Test.txt

How To Use “Truncate” Command In Linux? How To Use “Truncate” Command In Linux? 1625341643 300 How To Use Truncate Command In Linux

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

How To Use “Truncate” Command In Linux? How To Use “Truncate” Command In Linux? 1625341643 573 How To Use Truncate Command In Linux

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

How To Use “Truncate” Command In Linux? How To Use “Truncate” Command In Linux? 1625341644 45 How To Use Truncate Command In Linux

How To Use “Truncate” Command In Linux? How To Use “Truncate” Command In Linux? 1625341645 860 How To Use Truncate Command In Linux

The current size of the file is 330k.

Getting Help

To get a help message, use:

How To Use “Truncate” Command In Linux? How To Use “Truncate” Command In Linux? 1625341646 604 How To Use Truncate Command In Linux

Checking Version

To check the version of the truncate command, use:

How To Use “Truncate” Command In Linux? How To Use “Truncate” Command In Linux? 1625341647 321 How To Use Truncate Command In Linux

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