How to Truncate File in Linux

How to Truncate File in Linux to Remove File Contents

Clearing the system of useless files and data and clearing the storage disk of the operating system is one of our daily tasks. It may have happened to you that you don’t need file data, and on the other hand, creating a new file with correct permissions and ownership is a time-consuming and challenging task; what do you do in this situation? Delete the file and directory containing a lot of data and create a new file? No, this is not a good idea when you have limited time.

In addition, in some situations, deleting the file causes errors and problems in the operation of the programs. The solution to this problem is to use the “truncate” command to speed up tasks for Linux users. The “truncate” command extends or reduces the file size in Linux so that the user can delete or truncate the file’s content without having to set permissions and ownership. Users can use the “truncate” command to shrink a file’s size and delete extra data without changing the file’s permissions and ownership.

You may ask how you can truncate a file in Linux and delete the contents of the file instead of deleting the file. The answer to your question is in this article because, in this article, we will teach you how to truncate file in Linux in different ways.

The popular Linux does not leave room for the concerns of its users with the support of various and extensive tools, these features encourage users to buy Linux VPS because users can manage their server optimally using various Linux commands. To quickly learn how to truncate Linux file size, read this article to the end.

Installing the truncate command line tool on Linux

The truncate tool is available by default in most Linux distributions. To make sure that the Linux distribution you are using supports truncate, check the version of truncate by running the following command:

truncate --version

If the output does not provide information about the version of the truncate tool, it can be concluded that your operating system does not support the truncate tool. So install Coreutils Packages to take advantage of the truncate feature:

sudo apt-get install coreutils

truncate command to truncate file

The truncate command line tool is used to change the file’s size, which allows the user to reduce or expand the file’s contents to a specified size or delete the file’s content entirely by keeping the file permissions. You must have write privileges for truncating files. The general syntax for truncating files using the truncate command is as follows:

truncate -s 0 filename

Explanation:

  • -s 0: Specifies the new size for the file. In this case, -0 option sets file size to 0 to remove all contents.
  • Filename: The name of the file you want to truncate.

Be careful using the truncate command because no solution can recover the deleted information if you delete the file’s contents. Therefore, before using the truncate command, we recommend you make a backup copy of your data.

To check the size and set permissions of the file, use the following command:

ls –lh filename

The truncate command in linux supports various options to change the file size, which we will teach in the following:

  • Clearing all content of the file by linux truncate command

By setting the desired file size to zero (0), all the contents of the file will be deleted:

truncate -s 0 filename
  • Shrinking file size by truncate command

To reduce the file size to the desired size, use the “” option along with the truncate command; As a result, the extra contents of the file will be deleted. For example, if you want to reduce the file size by 200K bytes, use the following command:

truncate -s -200K example.txt
  • Extending file size by truncate command in linux

Sometimes, you must create a large file to store and maintain mass reports and data. In addition to creating large files, you can increase the size of the existing file. Using the “+” option along with the truncate command causes the truncate command to expand the size of your desired file to a specified size. For example, if you want to expand the file size from 200K bytes to 400K bytes, you should run the following command:

truncate -s +200k example.txt
  • Specifying file size by truncate command

The ubuntu truncate command allows the user to adjust the file size as desired and manage the file’s content in this way. If you want to change the file size to a specific size, for example, from 100 to 200 bytes, consider the following example:

truncate -s 200 example.txt

:> or > shell redirection for truncating file

Most Linux users prefer to use > or :> Shell redirection operator to delete file contents Because one of the easiest ways to empty file contents is in modern shells like Bash and Zsh. The main syntax for removing file content through redirection is as follows:

: > filename

Or

> filename

Explanation:

  • : (colon): It has replaced the “true” command and has the same function as the “true” command.
  • Redirection operator >: it redirects the output to the specified file.
  • Filename: The name of the file you want to truncate.

Therefore, instead of the previous command, you can use the following command to truncate file in linux:

true > filename

Note:truecommand does not display output.

Combination of cat and /dev/null for clearing file’s content

To delete the contents of the file, you can run the cat command in combination with /dev/null, which is as follows:

cat /dev/null > filename

echo command for removing the content of the file

The echo command, in combination with the redirection operator, is one of the useful commands to delete the contents of the file in linux, which is as follows:

echo > filename

Or

echo "" > filename

The “-n” option is used along with the echo command to truncate the file and prevent the addition of a new line:

echo -n > filename

Why can’t I apply the shell redirection for a truncating file with sudo privilege?

As we mentioned, you must have write permission to truncate file; that’s why many users execute commands with sudo, but sometimes they get “Permission denied” errors. To solve this problem, you can run a new shell using the sudo privilege and run the commands inside the shell through the” -c” option:

sudo sh -c '> filename'

FAQ

No, truncating a file is irreversible unless you have a backup of the file's original content.

The truncate command reduces the size of a file and doesn't support truncating based on specific characters or patterns.

Conclusion

In this article, you learned different commands to truncate the file and delete the contents of the file. The most used method to delete the file’s contents or change the file’s size is to use the truncate command. For more information on using the truncate tool to reduce the file size or remove the contents of a file, run the truncate –help command.

Now, you can truncate your desired file in any way you are more familiar with. If you know another way to delete the file content without having to delete the file and its set permissions, we will be happy to introduce it to other users in the comments section.

Leave a Reply

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