linux 清空文件常用方法

转载:
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$ 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值