I have a file called error.log on my server that I need to frequently truncate. I have rw permissions for the file. Opening the file in vi > deleting all content > saving works (obviously). But when I try the below
cat /dev/null > error.log
I get the message
File already exists.
Obviously there is some kind of configuration done on the server to prevent accidental overriding of files. Can anybody tell how do I "truncate" the file in a single command?
the credit goes for my senior colleague for this:
This will not break log files, so you can even use it on syslog, for example.
Since sudo will not work with redirection
>
, I like thetee
command for this purposeThis will be enough to set the file size to 0:
may work as well
You can try also:
echo -n > /my/file
You can also use function truncate
if permission denied, use sudo
Help/Manual: man truncate
tested on ubuntu Linux