How to empty (“truncate”) a file on linux that alr

2019-01-21 00:56发布

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?

8条回答
爷的心禁止访问
2楼-- · 2019-01-21 01:25

Any one can try this command to truncate any file in linux system

This will surely work in any format :

truncate -s 0 file.txt
查看更多
萌系小妹纸
3楼-- · 2019-01-21 01:28

You have the noclobber option set. The error looks like it's from csh, so you would do:

cat /dev/null >! file

If I'm wrong and you are using bash, you should do:

cat /dev/null >| file

in bash, you can also shorten that to:

>| file
查看更多
登录 后发表回答