How to empty (“truncate”) a file on linux that already exists and is protected in someway?

后端 未结 9 498
梦谈多话
梦谈多话 2020-12-12 12:39

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 wo

相关标签:
9条回答
  • 2020-12-12 13:29

    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
    
    0 讨论(0)
  • 2020-12-12 13:31

    the credit goes for my senior colleague for this:

    :> filename
    

    This will not break log files, so you can even use it on syslog, for example.

    0 讨论(0)
  • 2020-12-12 13:36

    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
    
    0 讨论(0)
提交回复
热议问题