How to empty the contents of continuosly streaming file in linux

Solution 1:

Since the file is held open by the application, I don't think you can just empty it traditionally. Googling a bit I found this:

Assuming that the author of the code writing the never-ending file knows even a little bit about unix:

  1. For this process there is a configuration file, which names the output logfile.
  2. The process "reconfigures" itself when it receives a SIGHUP signal, by re-reading the config file, and if the name of the logfile changed, then close the old one, open the new one.

This is pretty much UNIX standard for a never-ending log writer program.

Other than that, I can't find any obvious solution, since you used > and not >>. Next time you might want to redirect through truncate or something of the sort to avoid this problem of an infinitely-open logfile.

Solution 2:

All you need is:

cat /dev/null > sample.txt

or

>sample.txt