Linux清空文件的几种方式

转载:https://blog.csdn.net/ouzili/article/details/76855331

1、使用重定向的方法
 > test.txt 

2、使用true命令重定向清空文件
 true > test.txt 

3、使用cat/cp/dd命令及/dev/null设备来清空文件

在 Linux 中, null 设备基本上被用来丢弃某个进程不再需要的输出流,或者作为某个输入流的空白文件,这些通常可以利用重定向机制来达到。

 cat /dev/null >  test.txt 
 cp /dev/null test.txt 

 dd命令用于读取、转换并输出数据 具体
 dd if=/dev/null of=test.txt               // if 代表输入文件,of 代表输出文件。

4、使用echo命令清空文件

echo -n "" > test.txt    // 要加上"-n"参数,默认情况下会"\n",也就是回车符

echo > test.txt 

echo "" > test.txt 

5、使用truncate命令清空文件

 truncate -s 0 test.txt   -s参数用来设定文件的大小,清空文件,就设定为0;
 

猜你喜欢

转载自blog.csdn.net/qq_38269618/article/details/95175195