How to Empty a File in Linux without Deleting It

When managing a Linux server, system administrators frequently encounter massive log files that have grown to gigabytes in size, consuming all available disk space. The intuitive response is to simply use the rm command to delete the bloated file. However, in Linux, deleting an active log file is often a terrible mistake.

If a running application (like a web server or database) is actively writing to a file, deleting that file will not stop the application. The software will simply hold the “ghost” file open in the server’s RAM, preventing the disk space from actually being freed until you restart the entire application. Furthermore, the application might crash if it expects the log file to exist but cannot find it.

The correct, professional solution is to truncate the file—emptying all of its contents, reducing its size to zero bytes, but leaving the physical file intact so the application can seamlessly continue writing new data to it.

Method 1: The Null Redirect (The Fastest Way)

The most elegant and commonly used method to empty a file relies on shell redirection. You simply redirect “nothing” into the target file.

Open your terminal and type the following command, replacing error.log with your target file:

> error.log

That is it. The greater-than symbol (>) tells the shell to redirect output into the file. Because there is no command before the symbol, it redirects absolute nothingness, instantly wiping the file clean while preserving its permissions and ownership.

Method 2: Using the truncate Command

Linux includes a dedicated utility specifically designed for shrinking or extending the size of files. This is particularly useful if you want to be explicit in your scripts about what you are doing.

To empty a file completely, you instruct the truncate command to reduce the file size (-s) to zero (0):

truncate -s 0 error.log

This achieves the exact same result as the null redirect, instantly returning the file to 0 bytes without deleting the actual file structure.

Method 3: The echo Command

If you prefer a more visual representation of what is happening, you can use the echo command to push an empty string into the file.

By default, echo prints a blank line (a newline character). If you redirect that into a file, the file will be 1 byte in size (containing only the invisible newline). To completely empty it to 0 bytes, you must use the -n flag, which tells echo not to print the newline character:

echo -n "" > error.log

Dealing with Permission Denied Errors

If you are attempting to empty a system log file located in a protected directory (like /var/log/), you will likely receive a “Permission denied” error, even if you put sudo in front of the redirect command (e.g., sudo > /var/log/syslog will fail).

This fails because the sudo command only applies to the command before the redirect, not the redirect action itself.

To successfully empty a protected file, you must pipe an empty string into the tee command, running tee with sudo privileges:

echo -n "" | sudo tee /var/log/syslog > /dev/null

Alternatively, use the truncate command, which works perfectly with standard sudo:

sudo truncate -s 0 /var/log/syslog

By mastering these truncation techniques, you can safely recover gigabytes of storage space on production servers without ever interrupting the vital applications that are actively running.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.

Receive our best articles and tips delivered straight to your inbox.