转载:
http://www.cnblogs.com/mrwang1101/p/6166326.htm
1 . 使用重定向的方法
2 .使用true命令重定向清空文件
3 .使用cat/cp/dd命令及/dev/null设备来清空文件
4 . 使用echo命令清空文件
5 . 使用truncate命令清空文件
guosheng@guosheng:~/code/practice/bash_practice$ du -h test1.sh
4.0K test1.sh
guosheng@guosheng:~/code/practice/bash_practice$ > test1.sh
guosheng@guosheng:~/code/practice/bash_practice$ du -h test1.sh
0 test1.sh
guosheng@guosheng:~/code/practice/bash_practice$ du -h test2.sh
4.0K test2.sh
guosheng@guosheng:~/code/practice/bash_practice$ true > test2.sh
guosheng@guosheng:~/code/practice/bash_practice$ du -h test2.sh
0 test2.sh
guosheng@guosheng:~/code/practice/bash_practice$ du -h test3.sh
du: 无法访问'test3.sh': 没有那个文件或目录
guosheng@guosheng:~/code/practice/bash_practice$ ls
3test.sh 4test.sh 5test.sh 6test.sh 7test.sh 8test.sh 9test.sh lizi_bash test1.sh test2.sh test7.sh
guosheng@guosheng:~/code/practice/bash_practice$ du -h 3test.sh
4.0K 3test.sh
guosheng@guosheng:~/code/practice/bash_practice$ cat /dev/null > 3test.sh
guosheng@guosheng:~/code/practice/bash_practice$ du -h 3test.sh
0 3test.sh
guosheng@guosheng:~/code/practice/bash_practice$ du -h 4test.sh
4.0K 4test.sh
guosheng@guosheng:~/code/practice/bash_practice$ echo -n "" >4test.sh
guosheng@guosheng:~/code/practice/bash_practice$ du -h 4test.sh
0 4test.sh
guosheng@guosheng:~/code/practice/bash_practice$ du -h 5test.sh
4.0K 5test.sh
guosheng@guosheng:~/code/practice/bash_practice$ truncate -s 0 5test.sh
guosheng@guosheng:~/code/practice/bash_practice$ du -h 5test.sh
0 5test.sh
guosheng@guosheng:~/code/practice/bash_practice$