linux 如何使用命令清空文件内容

2012-9-12 杜世伟 Linux

如何使用命令清空文件内容呢?如下为你介绍6种方法帮你清空文件内容:

1.echo:

[root@localhost ~]$ cat test.txt
test---test
[root@localhost ~]$ echo "" >test.txt //将空字符写入test.txt,覆盖文件中已有的内容
[root@localhost ~]$ cat test.txt

[root@localhost ~]$ du test.txt
4 test.txt

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

2.重定向:

[root@localhost ~]$ echo "acd" >test.txt
[root@localhost ~]$ cat test.txt
acd
[root@localhost ~]$ >test.txt //重定向
[root@localhost ~]$ cat test.txt
[root@localhost ~]$ du test.txt
0 test.txt

5.cp

[root@localhost ~]$ echo "test" >test.txt
[root@localhost ~]$ cp /dev/null test.txt
[root@localhost ~]$ cat test.txt
[root@localhost ~]$ du test.txt
0 test.txt

6.sed

[root@localhost ~]$ echo "test">test.txt
[root@localhost ~]$ cat test.txt
test
[root@localhost ~]$ sed -i '1,$'d test.txt
[root@localhost ~]$ cat test.txt
[root@localhost ~]$ du test.txt
0 test.txt

标签: linux

Powered by emlog 沪ICP备2023034538号-1