How to Truncate Files in Linux

June 13, 2023

Introduction

To truncate a file means to reduce its size by removing some of its content. Truncating a file is much faster and simpler than deleting the file, recreating it, and setting the correct permissions and ownership.

When a file is truncated, the data beyond the specified truncate point is discarded, which erases the excess content. Truncation is useful for sequential files, such as text files, log files, or database files.

In this tutorial, you will learn how to truncate files in Linux using shell redirection and the truncate command.

How to truncate files in Linux - a tutorial.

Prerequisites

  • A system running Linux.
  • The coreutils package installed for the truncate command.
  • Access to the terminal (Ctrl + Alt + T).
  • A user account with root privileges.

Truncate Files via Shell Redirection Operator

Shell redirection, specifically the > operator, is a feature in Unix-like operating systems that allows users to redirect the output of a command or program to a file instead of displaying it in the terminal.

There are several methods for truncating files using the shell redirection operator.

Use Colon (:) as Null Command

The colon symbol (:) is typically used as a placeholder or null command in shell scripting and produces no output. Use the following command to create an empty file or truncate an existing file to make it empty:

: > filename

In the command, the colon acts as a placeholder or null command that doesn't do anything. The > symbol redirects the output (which is nothing in this case) to the file specified by filename. If the file exists, it is overwritten. If the file doesn't exist, a new file is created.

For example:

Truncating a file through shell redirection and the : symbol.

The command above truncates the example file's contents to zero while keeping the file intact.

Note: Read our tutorial on the tr command, which translates or deletes characters from standard input and writes the result to standard output. tr can perform different text transformations, including truncating character sets.

Use the cat Command

The cat command is often used to concatenate and display the contents of files. In this case, it is a convenient way to access the null device and truncate a file.

The following command output the contents of the /dev/null device, which is an end-of-file character, and redirects it to the specified file:

cat /dev/null > filename

For example:

Truncating a file using shell redirection and the cat symbol.

The command truncates the example file and empties its contents by redirecting the null device's output to the file.

Redirect Using echo

The echo command displays text or variable values in the terminal. Appending the -n option instructs echo not to append a newline character, which redirects an empty string to the specified file using shell redirection.

The syntax is:

echo -n > filename

For example:

Truncating a file through shell redirection and the echo command.

The command redirects an empty string to the specified file. If the file already exists, it is overwritten. If the file doesn't exist, a new file is created. In the example above, the file's contents are removed.

Use Redirection Only

Most modern shells, such as Bash or Zsh, allow you to omit the command before the redirection operator and truncate the file. The syntax is:

> filename

For example:

Truncating a file through shell redirection only.

The command redirects the output (in this case, no output since there is no preceding command) to the specified example file and truncates it to make it empty.

If the file doesn't exist, the command creates a new empty file.

Truncate with sudo

To truncate a file, you must have write permissions on the file. While the sudo command allows you to truncate a file you don't have the write permissions for, elevated root privileges do not apply to redirection. For example, the following file is read-only, which is why the operation fails:

Permission denied when trying to truncate a read-only file.

The command fails and denies permission to truncate the file. There are several options to override this behavior and truncate the file:

  • Run a new shell. You can execute the redirection command as a superuser in a new shell to redirect the write permissions:
sudo sh -c '> filename'
Truncating a read-only file with sudo.

The sh -c part invokes a new shell and executes the command within that shell after you provide the root password.

  • Pipe output to the tee command. Another option is to pipe the output to the tee command and elevate the privileges with sudo, which writes the empty output to the specified file:
: | sudo tee filename
Truncating a read-only file with sudo and tee commands.

The null command : provides an empty input, which is piped to tee with sudo. tee writes that empty input to standard output and to the specified file using elevated privileges.

Truncate Files via truncate Command

The truncate command is a utility that allows users to resize a file to a specified size or to remove its contents entirely. However, the truncate command requires appropriate file system permissions to modify files.

Depending on the file permissions and file location, you may need administrative privileges or use sudo to execute truncate with elevated permissions.

Remove File Contents Entirely

The syntax for removing a file's contents with truncate is:

truncate -s 0 filename

For example:

Truncating a file using the truncate command.

The command removes all contents from the specified examplefile file.

Truncate File to Specific Size

Use the truncate command to reduce a file to a specific size. The default value is in bytes, which has the following syntax:

truncate -s [number of bytes] filename

For [number of bytes], specify the desired file size.

For example:

Truncate a file to a specific size.

In the example above, the command truncates the specified file's size to 10 bytes. Other units are also accepted, such as K for kilobytes, M for megabytes, G for gigabytes, etc. For example, to reduce a file's size to 10KB, use the following syntax:

truncate -s 10K filename

Increase File Size

Add a plus or minus sign in front of the size to increase or decrease the file by that amount. For example, to increase the file size by 10MB, use the following syntax:

truncate -s +5M filename

For example:

Increase a file size using the truncate command.

The +5M indicates an increase in size by 5 megabytes. The + sign before the size indicates an increase, while a - sign indicates a decrease.
Executing this command increases the size of the specified file by 5 megabytes. If the file initially has a smaller size, the command adds null bytes at the end of the file to reach the new size. If the file is already larger than 5 megabytes, the command has no effect.

Conclusion

The guide showed you how to truncate files in Linux using the shell redirection operator > and the truncate command. Truncating files is useful when you want to remove contents from a file without deleting the file itself.

The command is often used for log files or database files to manage the file size, facilitate file parsing, and improve performance.

Was this article helpful?
YesNo
Bosko Marijan
Having worked as an educator and content writer, combined with his lifelong passion for all things high-tech, Bosko strives to simplify intricate concepts and make them user-friendly. That has led him to technical writing at PhoenixNAP, where he continues his mission of spreading knowledge.
Next you should read
How to Use the Linux head Command
January 5, 2022

The head commands lists the first ten lines of a file in standard output. Follow this guide and learn how to use the head command and its options (with examples).
Read more
How to Grep for Multiple Strings, Patterns or Words
May 5, 2020

This tutorial shows how you can use the grep tool to search for multiple string patterns through various examples.
Read more
Linux Commands Cheat Sheet: With Examples
November 2, 2023

A list of all the important Linux commands in one place. Find the command you need, whenever you need it or...
Read more
How to Find Files in Linux With the Find Command
August 29, 2024

The find command is a useful command-line tool in Linux. It allows you to use the terminal to search for a specific string of characters using your Linux command-line interface.
Read more