在Linux中使用时清空文件 [英] Empty a file while in use in linux
问题描述
我正在尝试在使用中清空linux中的文件,它是一个日志文件,因此会被连续写入. 现在我用过:
I'm trying to empty a file in linux while in use, it's a log file so it is continuosly written. Right now I've used:
echo -n > filename
或
cat /dev/null > filename
但是所有这些都会产生一个带有换行符(或我可以在^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ @ ^ ..看到的奇怪字符的空文件. vi
),我必须先用vi
和dd
手动删除第一行,然后保存.
but all of this produce an empty file with a newline character (or strange character that I can see as ^@^@^@^@^@^@^@^@^@^@^@^.. on vi
) and I have to remove manually with vi
and dd
the first line and then save.
如果我不使用vi
和dd
,则无法使用grep
操作文件,但是我需要一个可以在shell脚本中编写的自动过程.
If I don't use vi
adn dd
I'm not able to manipulate file with grep
but I need an automatic procedure that i can write in a shell script.
想法?
推荐答案
这应该足以清空文件:
> file
但是,您说过尝试的其他方法也应该起作用.如果您看到奇怪的字符,则说明它们是通过其他方式写入文件的-最有可能是在那里记录的任何过程.
However, the other methods you said you tried should also work. If you're seeing weird characters, then they are being written to the file by something else - most probably whatever process is logging there.
这篇关于在Linux中使用时清空文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!