[email protected]Support 24/7 · Billing 09:00 – 00:00LinkedInXFacebook
NexonHost
Netherlands Dedicated ServersAmsterdam · Equinix AM6Germany Dedicated ServersFrankfurt · Equinix FR4Romania Dedicated ServersBucharest · VoxilityAustria Dedicated ServersVienna · Interxion VIEBulgaria Dedicated ServersSofia · TelepointFrance Dedicated ServersParis · Interxion PAR — Marseille · Digital Realty MRS3Ireland Dedicated ServersDublin · Equinix DB2Italy Dedicated ServersMilan · Irideos AvalonPoland Dedicated ServersWarsawSpain Dedicated ServersMadrid · Equinix MD2United States Dedicated ServersAshburn · Equinix DC
All articles
Getting startedMarch 16, 2023

How to Truncate (Empty) Files in Linux

How To Truncate (Empty) Files In Linux

This tutorial explains how to truncate files to zero size in Linux systems using shell redirection and the truncate command.

In some situations, you might want to truncate (empty) an existing file to a zero-length. In simple words, truncating a file means removing the file contents without deleting the file.

Truncating a file is much faster and easier than deleting the file , recreating it, and setting the correct permissions and ownership . Also, if the file is opened by a process, removing the file may cause the program that uses it to malfunction.

 

Shell Redirection

The easiest and most used method to truncate files is to use the > shell redirection operator.

The general format for truncating files using redirection is:

: > filename

Let’s break down the command:

  • The : colon means true and produces no output.

  • The redirection operator > redirect the output of the preceding command to the given file.

  • filename, the file you want to truncate.

If the file exists , it will be truncated to zero. Otherwise, the file will be created.

Instead of : can also use another command that produces no output.

Here is an example of using the cat command to output the contents of the /dev/null device, which returns only an end-of-file character:

cat /dev/null > filename

Another command that can be used is echo . The -n option tells echo not to append a newline:

echo -n > filename

On most modern shells such as Bash or Zsh you can omit the command before the redirection symbol and use:

> filename

To be able to truncate a file, you need to have write permissions on the file. Usually, you would use sudo for this, but the elevated root privileges do not apply to the redirection. Here is an example:

sudo : > /var/log/syslog
bash: /var/log/syslog: Permission denied

There are several solutions that allow redirecting with sudo. The first option can run a new shell with sudo and execute a command inside that shell using the -c flag:

sudo sh -c '> filename'

Another option is to pipe the output to the tee command, elevate the tee privileges with sudo, and write the empty output to a given file:

: | sudo tee filename

truncate Command

truncate is a command-line utility that allows you to shrink or extend the size of a file to a given size.

The general syntax for truncating files to zero size with the truncate command, is as follows:

truncate -s 0 filename

The -s 0 option sets the file size to zero.

For example, to empty the Nginx access log you would use:

sudo truncate -s 0 /var/log/nginx/access.log

 

Empty All Log Files

Over time, your disk drive may get cluttered with a lot of large log files taking up large amounts of disk space.

The following command will empty files ending with “.log” under the /var/log directory:

sudo truncate -s 0 /var/log/**/*.log 

A better option would be to rotate, compress, and remove the logs files with the logrotate tool.

 

Conclusion

To truncate a file in Linux use the redirection operator > followed by the file name.

Keep reading
How to use du command.
April 10, 2023

How to use du command.

How To Use Du Command. The du command, short for “disk usage” reports the estimated amount of disk space used by given files or directories. It is practically useful for finding files and directories taking up large amounts of disk space. How to Use the du command The general syntax for the du…

Read article
How to Increment and Decrement Variable in Bash
April 10, 2023

How to Increment and Decrement Variable in Bash

How To Increment And Decrement Variable In Bash One of the most common arithmetic operations when writing Bash scripts is incrementing and decrementing variables. This is most often used in loops as a counter, but it can occur elsewhere in the script as well. Incrementing and Decrementing means…

Read article
How to Install Ubuntu 20.04 on Windows 10.
March 23, 2023

How to Install Ubuntu 20.04 on Windows 10.

NOTE: If you want to learn Linux (ubuntu), this is a very good and simple way to learn, I hope you like this article. Ubuntu is a very stable and flexible operating system and a Debian-based Linux distribution consisting mainly of free and open-source software. There are different versions of…

Read article
Get in Touch

Together, Let’s Build a Faster, Safer Internet.

Build scalable infrastructure with NexonHost — high-performance dedicated servers, VPS hosting, cloud hosting, and DDoS protection across Europe.

Get Started →Contact Us
[email protected]Support 24/7 · Billing 09:00 – 00:00