linux命令:清空文件内容

  • Post author:
  • Post category:linux


清空文件内容的方法:

1.echo:

[cti@minicc ~]$ cat test.txt

test—test

[cti@minicc ~]$ echo “” >test.txt  //将空字符写入test.txt,覆盖文件中已有的内容

[cti@minicc ~]$ cat test.txt

[cti@minicc ~]$ du test.txt

4       test.txt


注:文件中有一个空字符,文件大小为4k

2.重定向:

[cti@minicc ~]$ echo “acd” >test.txt

[cti@minicc ~]$ cat test.txt

acd

[cti@minicc ~]$ >test.txt   //重定向

[cti@minicc ~]$ cat test.txt

[cti@minicc ~]$ du test.txt

0       test.txt

5.cp

[cti@minicc ~]$ echo “test” >test.txt

[cti@minicc ~]$ cp /dev/null test.txt

[cti@minicc ~]$ cat test.txt

[cti@minicc ~]$ du test.txt

0       test.txt

6.sed

[cti@minicc ~]$ echo “test”>test.txt

[cti@minicc ~]$ cat test.txt

test

[cti@minicc ~]$ sed -i ‘1,$’d test.txt

[cti@minicc ~]$ cat test.txt

[cti@minicc ~]$ du test.txt

0       test.txt



版权声明:本文为zhy_yz原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。