Truncate File Linux - 5 Easy Methods

Reading time icon 3 min. read


Readers help support MSpoweruser. We may get a commission if you buy through our links. Tooltip Icon

Read our disclosure page to find out how can you help MSPoweruser sustain the editorial team Read more

truncate file linux

Want to truncate a file on Linux? This operation refers to removing a file’s content or reducing its size. More specifically, truncating is much easier and faster than deleting a file, creating it again, and specifying the correct file ownership and permissions.

In this guide, I’ll show you how to achieve this. So, let’s go!

How to Truncate Files in Linux

To truncate files in Linux, you can use:

checking content and size of a file on linux

1. Using the Colon “:” Symbol

In shell scripting, the colon:” symbol is primarily used as a null command or placeholder. This symbol results in no output. Therefore, you can utilize it to truncate an existing file.

To do so, I’ll press CTRL+ALT+T to open my terminal, type the “: > filename” command, and hit Enter.

truncating file in Linux with colon

In my case, this command truncates the content of test.txt to zero while keeping the file intact.

2. Using the cat Command

The cat command is usually used to concatenate and show the contents of files on a Linux terminal. However, you can also utilize it to access the null device and truncate the given file.

For this, I’ll run the “cat /dev/null > filename” command.

truncating file on Linux using cat command

Here, the cat command truncates the given file by redirecting the null device’s output to it.

3. Using Redirect Operator with echo

Linux echo command prints variable or text value on the terminal. However, you can add the -n option in the echo command to force it to NOT append a newline character.

This consequently redirects an empty string to the given file using the Redirect operator “>“.

For instance, I’ll type the “echo -n > filename” command.

truncating file in Linux using echo command with redirect operator

4. Using Simple Redirection

To omit the command before the redirection operator and truncate the specified file, you can simply run the “> filename” command.

truncating file in Linux using redirect operator

The above command truncates the specified file if it exists. Otherwise, it creates a new empty file.

5. Using the truncate Command

On Linux-based systems, the truncate command enables you to remove the content of a file or resize it to the given size. However, to utilize it on your system, you must install core-utils with the “sudo apt-get install coreutils” command.

After successful installation, I’ll use the “truncate -s number_of_bytes filename” command to reduce my test.txt file to 10 bytes.

truncating file to a specific size using truncate command

In the above command, you can also specify other file size units such as:

  • K for kilobytes
  • M for megabytes
  • G for gigabytes

To completely remove file contents, I’ll run the “truncate -s 0 filename” command.

truncating file to size zero with Linux truncate command

Moreover, using this effective approach, you can empty all log files at once.

truncate all log files using truncate command in Linux

Bonus Tips:

To truncate a file, you must have write permission for it. For instance, I’ve changed the file permissions of my test.txt file to read-only.

Now, if I try to truncate it, the output will show a Permission denied error.

getting permission denied error while truncating file on Linux

To resolve this issue, the first option is to open a new shell with sudo and run the same command with the -c option.

truncating file with sudo sh command

Additionally, you can pipe “|” the output of the tee command with sudo and write the empty output to the specified file.

truncating file in Linux using tee command

You may also be interested in:

So, now you know how to truncate a file on Linux! If you have any other tips, share them in the comments below.

User forum

0 messages