Emptying files with shell scripts
In recent projects, a lot of log files are generated, so you need to write a script to empty them manually, here are a few typical ways.
Method 1 Code
#!/bin/bash##方法1echo "" > $1echo "$1 cleaned up."
Run
Description
I think you should notice that the file is not actually emptied, but there is a blank line.
Method 2 Code
#!/bin/bash##方法2: > $1echo "$1 cleaned up."
Run
Description
Where: is an empty command that acts as a placeholder. The emptied file here no longer has a blank line to make the real sense of emptying.
Method 3 Code
#!/bin/bash##方法3cat /dev/null > $1echo "$1 cleaned up."
Run
Description
/dev/null can be seen as a "black hole". All content written to it will be lost. Read from it and read nothing. The emptied file here no longer has a blank line to make the real sense of emptying.
Summarize
Here I only listed three methods, I would like to clear the Linux file method is still a lot of, I personally prefer Method 2.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Emptying files with shell scripts