Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I tried to clear the content of error.log file on my apache server with commands

sudo >error.log

sudo truncate -s0 error.log

But in both cases I got -bash: error.log: Permission denied.

I tried the same thing while stopping apache, but still got the same message. After that I tried to clear it with sudo nano error.log and was able to remove everything.

So why was not I able to clear the log properly?

share|improve this question

1 Answer 1

up vote 4 down vote accepted

You should try this :

sudo sh -c ">/var/log/apache2/error.log"
share|improve this answer
    
Can you please explain what does this command do? –  Salvador Dali Mar 18 at 0:47
    
It runs an inline sh (shell) command under sudo context. Actually this inline sh command clears Apache error log (as you know) –  krisFR Mar 18 at 0:50
1  
My guess is that > is not a command without an interpreter like sh or bash –  krisFR Mar 18 at 0:59
1  
However your sudo truncate... command should work...That is the real thing i cannot explain by now –  krisFR Mar 18 at 1:07
3  
Redirection operators are evaluated by your local shell before the sudo command even runs. sudo > foo opens the file foo for writing, then runs the command sudo . If you put it inside quotes or escape the redirection operator, then it is evaluated within the context of the sub-shell. –  Zoredache Mar 18 at 1:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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