I want to erase the contents of the file error.log (nginx error log file), but I don't want to actually delete the file.

is this possible?

running ubuntu

You can use this:


(typed just like that - an empty output redirection)


truncate -s0 error.log
  • @Ignacio: If the file already exists, either will truncate it. If the file doesn't exist, either will create it. – Dennis Williamson Dec 4 '10 at 3:21
  • You're right, my bad. – Ignacio Vazquez-Abrams Dec 4 '10 at 3:24
  • The last option worked for me, for some reason sudo >error.log didnt work for me (ubuntu 12.04) – davidkonrad May 19 '14 at 10:38
  • 4
    @davidkonrad: You would have to already be root for the redirection to work since it's performed before sudo takes effect. Or you can do sudo bash -c '>error.log' – Dennis Williamson May 19 '14 at 10:56
  • 1
    @divHelper11: In my answer I say it's an "empty output redirection". That means that "nothing" is being sent to the named file and that overwrites any existing contents. It's similar to echo "some words" > output.txt but nothing is going in. – Dennis Williamson Oct 25 at 16:46

You'll confuse the daemon. Erase the file then send SIGHUP to nginx.

  • SIGHUP? Can't I shut nginx down, purge the file, then start? I want to know how to erase the file for knowledge sake also. – user27449 Dec 4 '10 at 3:14
  • If you wanted to, sure. But that would disrupt service. – Ignacio Vazquez-Abrams Dec 4 '10 at 3:15
  • I think both a null redirection and a truncate are atomic, so as long as no seeking is going on (especially if the only operation that's occurring is appending) it shouldn't be disruptive. – Dennis Williamson Dec 4 '10 at 3:25

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.