linux中快速清空文件内容的几种方法

复制代码如下:$:>filename$>filename$echo"">filename$echo>filename$cat/dev/null>filename

linux清空文件的几种常见方法

1.使用重定向方式[root@centos7~]#du-htest.txt4.0Ktest.txt[root@centos7~]#>test.txt2使用正确的命令重定向并清除文件[root@centos7~]#du-htest.txt4.0Ktest.txt[root@centos7~]#true>test.txt[root@centos7~]#du-htest.txt0test.txt3,使用cat/cp/dd命令和/dev/n清除文件ull设备[root@centos7~]#du-htest.txt4.0Ktest.txt[root@centos7~]#cat/dev/null>test.txt[root@centos7~]#echo"HelloWorld">test.txt[root@centos7~]#du-htest.txt4.0Ktest.txt[root@centos7~]#cp/dev/nulltest.txtcp::您想覆盖“test.txt”吗?y[root@centos7~]#echo"HelloWorld">test.txt[root@centos7~]#ddif=/dev/nullof=test.txt4使用echo命令清理文件[root@centos7~]#echo"HelloWorld">test.[root@centos7~]#echo-n"添加参数">test.txt==>"-n"。
默认情况下,这将是“\n”,即回车符。
5、使用truncate命令清理文件[root@centos7~]#du-htest.txt4.0Ktest.txt[root@centos7~]#trundate-s0test.txt-s使用参数设置文件的大小要清除文件,请将其设置为0;