[email protected]Office hours: Mon – Fri 9:00AM – 5:00PMLinkedInXFacebook
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 Implement Network Segmentation and Resource Isolation in Linux.
February 6, 2024

How to Implement Network Segmentation and Resource Isolation in Linux.

How to Implement Network Segmentation and Resource Isolation in Linux. Securing and managing networks in a Linux environment involves advanced practices like network segmentation and resource isolation. This guide explores these concepts, providing detailed instructions and examples for…

Read article
How to open or close ports in Linux.
February 6, 2024

How to open or close ports in Linux.

How to open or close ports in Linux. Effectively managing ports on your Linux system is crucial for both security and service accessibility. This comprehensive guide will provide a detailed walkthrough on opening and closing ports using the robust tools iptables and ufw. Understanding these…

Read article
How to use ypdomainname command.
January 23, 2024

How to use ypdomainname command.

How to use ypdomainname command. In the realm of Linux networking, the ypdomainname command plays a crucial role in the context of the Network Information Service (NIS). NIS, formerly known as Yellow Pages, is a distributed database service that facilitates the sharing of network configuration and…

Read article
Get in Touch

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

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

Get Started →Contact Us
[email protected]Office hours: Mon – Fri 9:00AM – 5:00PM