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

I need to clear a log file with:

cat /dev/null > error.log 

The problem is that the file is owned by root

-rw-r--r-- 1 root root 15505 Feb  9 15:08 error.log

and this doesn't work

$ sudo cat /dev/null > error.log 
-bash: error.log: Permission denied

no idea why... so how could I get around this?

share|improve this question
    
Just wanted to share a shortcut.. > error.log will zero the file just the same. (Use the answer below to combine it with sudo.) –  Aaron Copley Feb 9 at 15:29
    
man truncate –  Zoredache Feb 10 at 8:04

1 Answer 1

up vote 4 down vote accepted

Try this:

$ sudo sh -c "cat /dev/null > error.log"

The problem is that with the command you used, you are really running two commands: the first cat is run as root (via sudo) but everything else is run as your user. The above method resolves that.

This answer over on U&L has more information on this.

share|improve this answer

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.