Oct 9, 2023 3 min read

How to Truncate (Empty) Files on Linux

Truncate (Empty) Files on Linux with our step-by-step tutorial. Truncating a file to zero length is useful in some circumstances.

Truncate (Empty) Files on Linux
Table of Contents

Introduction

Before we start discussing about how to truncate (empty) files on linux, let's understand-What is Trunate ?

Truncating a file to zero length is useful in some circumstances. Truncating a file removes its contents but leaves the file itself intact.

Instead of deleting a file, creating it from scratch, and then defining its rights and ownership, you may just truncate it. Furthermore, if the file is currently open in a process, deleting it might potentially break that process.

In this tutorial, we will explain how to use shell redirection and the truncate command on Linux to truncate files. We will also address a few FAQs on how to truncate (empty) files on Linux.

Shell Redirection

Using the > shell redirection operator is the most common and straightforward approach to truncating files.

The following structure is used for truncating files through redirection:

: > filename

Let's examine the command in detail:

  • The colon : denotes true and does not affect output.
  • The > redirect operator sends the results of the previous operation to the specified file.
  • filename, the file you want to truncate.

Truncating to zero will occur if the file already exists. If not, the file will be created.

You can use another command instead of : that produces no output.

Here is how to use the cat command to display the empty /dev/null device, which only returns the end-of-file character.

cat /dev/null > filename

An alternative command is echo.  By using the -n option, echo is instructed to avoid adding a new line after it finishes a line.

echo -n > filename

Most current shells, including Bash and Zsh, allow you to omit the command before the redirection sign, as seen below.

> filename

Truncating a file requires writing access to the file. Typically, you'd use sudo but the redirected file won't benefit from the extra root permissions. As an example, consider the following:

sudo : > /var/log/syslog
Output

bash: /var/log/syslog: Permission denied

Using sudo to perform a redirection is possible in several situations. The first option allows you to launch a new shell with Sudo and issue a command from inside it using the -c flag:

sudo sh -c '> filename'

An alternative is to use the tee command, and raise its privileges using sudo, and then write the empty output to a given file:

: | sudo tee filename

truncate Command

Truncate is a command-line tool for reducing or increasing the size of a file to a specified value.

The truncate command may be used to shrink files to zero bytes by using the following syntax:

truncate -s 0 filename

The size of the file may be made zero using the -s 0 option.

For instance, here's what you'd type to clear off the Nginx access log:

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

Empty All Log Files

After some time, your hard drive's available capacity may become overwhelmed by the accumulation of many, large log files.

The following command will remove all .log files from the /var/log folder:

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

The logrotate utility may be used to rotate, compress, and delete the log files.

FAQs to Truncate (Empty) Files on Linux

Is there a way to truncate all files in a directory? 

Yes, you can easily truncate all files in a directory using a combination of the find and truncate commands. For instance: find /path/to/directory -type f -exec truncate -s 0 {} +.

What happens to the file's permissions when truncating it? 

Truncating a file does not affect its permissions. The ownership and permissions of the file remain the same before and after truncation.

Does truncating a file delete its content permanently? 

No, truncating a file only removes its content, but the file's metadata and structure remain intact. The previous content can potentially be recovered until it is overwritten by new data.

Can I truncate system files or important configuration files? 

Truncating system files or important configuration files can have serious consequences, as they are crucial for the functioning of your system. It is generally not recommended to truncate such files unless you are absolutely certain about what you're doing.

Is there a way to recover the contents of a truncated file?

 If you haven't made any backups, it is usually difficult to recover the contents of a truncated file. However, there are specialized data recovery tools available that might be able to restore some or all of the truncated file's content.

Is it possible to undo the truncation of a file?

 No, once a file has been truncated, the original content is permanently deleted. It is advisable to back up your files before truncating them to avoid losing important data.

What are some use cases for truncating files on Linux? 

Truncating files can be useful when you want to erase the data within a file without deleting the file itself. It also ensures that the file retains its original permissions and metadata.

Conclusion

On Linux, you may truncate a file by typing the redirection operator > followed by the file name.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.