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

后端 未结 9 497
梦谈多话
梦谈多话 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条回答
  • This will be enough to set the file size to 0:

    > error.log
    
    0 讨论(0)
  • 2020-12-12 13:15

    You can try also:

    echo -n > /my/file

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

    You can also use function truncate

    $truncate -s0 yourfile
    

    if permission denied, use sudo

    $sudo truncate -s0 yourfile
    

    Help/Manual: man truncate

    tested on ubuntu Linux

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

    I do like this: cp /dev/null file

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

    false|tee fileToTruncate

    may work as well

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

    Since sudo will not work with redirection >, I like the tee command for this purpose

    echo "" | sudo tee fileName
    
    0 讨论(0)
提交回复
热议问题