3

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?

davidhq
  • 215

1 Answers1

11

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.

EEAA
  • 109,904