linux 清空文件常用方法

来源:互联网 发布:淘宝usa商城假货 编辑:程序博客网 时间:2024/06/15 03:29

转载:
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.shguosheng@guosheng:~/code/practice/bash_practice$ > test1.sh guosheng@guosheng:~/code/practice/bash_practice$ du -h test1.sh 0   test1.shguosheng@guosheng:~/code/practice/bash_practice$ du -h test2.sh 4.0K    test2.shguosheng@guosheng:~/code/practice/bash_practice$ true > test2.sh guosheng@guosheng:~/code/practice/bash_practice$ du -h test2.sh 0   test2.shguosheng@guosheng:~/code/practice/bash_practice$ du -h test3.sh du: 无法访问'test3.sh': 没有那个文件或目录guosheng@guosheng:~/code/practice/bash_practice$ ls3test.sh  4test.sh  5test.sh  6test.sh  7test.sh  8test.sh  9test.sh  lizi_bash  test1.sh  test2.sh  test7.shguosheng@guosheng:~/code/practice/bash_practice$ du -h 3test.sh 4.0K    3test.shguosheng@guosheng:~/code/practice/bash_practice$ cat /dev/null  > 3test.sh guosheng@guosheng:~/code/practice/bash_practice$ du -h 3test.sh 0   3test.shguosheng@guosheng:~/code/practice/bash_practice$ du -h 4test.sh 4.0K    4test.shguosheng@guosheng:~/code/practice/bash_practice$ echo -n "" >4test.sh guosheng@guosheng:~/code/practice/bash_practice$ du -h 4test.sh 0   4test.shguosheng@guosheng:~/code/practice/bash_practice$ du -h 5test.sh 4.0K    5test.shguosheng@guosheng:~/code/practice/bash_practice$ truncate -s 0 5test.sh guosheng@guosheng:~/code/practice/bash_practice$ du -h 5test.sh 0   5test.shguosheng@guosheng:~/code/practice/bash_practice$ 
原创粉丝点击