27

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

71

You can use this:

>error.log

(typed just like that - an empty output redirection)

or

truncate -s0 error.log
| improve this answer | |
  • @Ignacio: If the file already exists, either will truncate it. If the file doesn't exist, either will create it. – Paused until further notice. 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
  • 5
    @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' – Paused until further notice. 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. – Paused until further notice. Oct 25 '18 at 16:46
2

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

| improve this answer | |
  • 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. – Paused until further notice. Dec 4 '10 at 3:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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