Truncate Large Text File in UNIX / Linux

To truncate a large text file in UNIX or Linux, you can use the “truncate” command.

The general syntax for the “truncate” command is:

truncate [OPTION]... SIZE FILE...

where:

  • OPTION: Any options you want to use with the command (e.g., -s for specifying the size in bytes, -c for not creating the file if it does not exist, etc.)
  • SIZE: The new size you want to set for the file (in bytes)
  • FILE: The name of the file you want to truncate

For example, to truncate a file named “large_file.txt” to a size of 1 gigabyte, you can use the following command:

truncate -s 1G large_file.txt

This will truncate the “large_file.txt” to a size of 1 gigabyte, and any content beyond that size will be deleted. Note that the size can be specified in various formats, such as kilobytes (K), megabytes (M), gigabytes (G), terabytes (T), etc. (https://www.onecrazyhouse.com/)

It is important to note that truncating a file will delete any data beyond the specified size, so be careful when using this command.

Leave a Comment