28

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

2 Answers 2

74

You can use this:

>error.log

(typed just like that - an empty output redirection)

or

truncate -s0 error.log
7
  • @Ignacio: If the file already exists, either will truncate it. If the file doesn't exist, either will create it. Dec 4, 2010 at 3:21
  • You're right, my bad. Dec 4, 2010 at 3:24
  • The last option worked for me, for some reason sudo >error.log didnt work for me (ubuntu 12.04) May 19, 2014 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' May 19, 2014 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. Oct 25, 2018 at 16:46
2

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

3
  • 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, 2010 at 3:14
  • If you wanted to, sure. But that would disrupt service. Dec 4, 2010 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. Dec 4, 2010 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.