Most efficient way to Empty File Content in Linux.

In some situations, you might want to empty an existing file to a zero-length. In another word, removing the file contents without deleting the file. Is that even possible?

Most efficient way to Empty File Content in Linux.
Image credit: Unknown. Please contact us if you are the owner.

Let's face it, emptying a file is much faster and easier than deleting the file, recreating it, and setting the correct permissions and ownership. Moreover, if the file is opened by a process, removing the file may cause the program that uses it to malfunction.

This article will show you how to use shell redirection and the truncate command to truncate files to zero size on Linux systems.


I/O Redirection

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

$ : > access.log
OR
true > access.log
#
$ du -sh access.log
134M      access.log
##
#
$ true > access.log
##
#
$ du -sch access.log
0	access.log

Let’s break the command down:

  • The : colon means true and produces no output.
  • The redirection operator > redirects the output of the preceding command to the given file.
  • filename, the file you want to truncate.

If the file exists, it will be saved as a zero-byte file. Otherwise, the file will be created.

Also, this is the fastest of all the methods mentioned here. If you have a really large file, then the I/O redirection could be substantially faster.

echo command

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

$ echo "" > access1.log
OR
$ echo > access1.log
#
$ du -sh access1.log
134M      access1.log
##
#
$ echo > access1.log
##
#
$ du -sh access1.log
4.0K	 access1.log

Note: You should keep in mind that an empty string is not the same as null. A string is already an object much as it may be empty while null simply means non-existence of an object.

For this reason, when you redirect the out of the echo command above into the file and view the file contents using the cat command, it prints an empty line (empty string).

As you can see in the example above, the size of the file is 4.0k and not 0.

To send a null output to the file, use the flag -n which tells echo to not output the trailing newline that leads to the empty line produced in the previous command.

$ echo -n > access2.log
#
$ du -sh access2.log
134M     access2.log
##
#
$ echo -n > access2.log
##
#
$ du -sh access2.log
0	 access2.log

Empty File Using cat/cp/dd utilities with /dev/null

Something of a black hole, bit bucket or data sink, /dev/null is one of the very clever things that Unix introduced into the computing world. And what a very clever and unusual one!
The /dev/null acts as a file and looks like a file (and maybe even smells like a file), but it's really a data sinkhole implemented as a file. So basically, you can empty the contents of a file by redirecting the output /dev/null to it (file) as input using.

$ cat /dev/null > access3.log
OR
$ cp /dev/null > access3.log
#
$ du -sh access3.log
134M     access3.log
##
#
$ cat /dev/null > access3.log
##
#
$ du -sh access3.log
0	 access3.log

In the following command, if means the input file and of refers to the output file.

#
$ dd if=/dev/null of=access4.log
0+0 records in
0+0 records out
0 bytes copied, 0.000390232 s, 0.0 kB/s
##
#
$ du -sh access4.log
0	 access4.log

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:

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


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

Here, we are emptying the Nginx access log.

Keep Us Caffeinated  ⦿ ⦿