快速清空文件内容

快速清空文件内容

Content #

可以在输入重定向中将/dev/null作为输入文件。由于/dev/null文件不含任何内容,因此程序员通常用它来快速清除现有文件中的数据,这样就不用先删除文件再重新创建了:

$ cat testfile
This is the first line.
This is the second line.
This is the third line.
$ cat /dev/null > testfile
$ cat testfile
$

文件testfile仍然还在,但现在是一个空文件。这是清除日志文件的常用方法,因为日志文件必须时刻等待应用程序操作。

或者用cp命令

cp -f /dev/null /var/log/apache2/access.log

From #

Linux命令行与shell脚本编程大全