Linux文件清空的几种方法

本文涉及的产品
实时计算 Flink 版,5000CU*H 3个月
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
大数据开发治理平台 DataWorks,不限时长
简介: Linux文件清空的几种方法

Linux文件清空的几种方法

使用重定向的方法

[root@centos7 ~]# du -h test.txt 
4.0K    test.txt
[root@centos7 ~]# > test.txt 
[root@centos7 ~]# du -h test.txt 
0    test.txt

使用true命令重定向清空文件

[root@centos7 ~]# du -h test.txt 
4.0K    test.txt
[root@centos7 ~]# true > test.txt 
[root@centos7 ~]# du -h test.txt 
0    test.txt

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

[root@centos7 ~]# du -h test.txt 
4.0K    test.txt
[root@centos7 ~]# cat /dev/null >  test.txt 
[root@centos7 ~]# du -h test.txt 
0    test.txt
###################################################
[root@centos7 ~]# echo "Hello World" > test.txt 
[root@centos7 ~]# du -h test.txt 
4.0K    test.txt
[root@centos7 ~]# cp /dev/null test.txt 
cp:是否覆盖"test.txt"? y
[root@centos7 ~]# du -h test.txt 
0    test.txt
##################################################
[root@centos7 ~]# echo "Hello World" > test.txt 
[root@centos7 ~]# du -h test.txt 
4.0K    test.txt
[root@centos7 ~]# dd if=/dev/null of=test.txt 
记录了0+0 的读入
记录了0+0 的写出
0字节(0 B)已复制,0.00041594 秒,0.0 kB/秒
[root@centos7 ~]# du -h test.txt 
0    test.txt

使用echo命令清空文件

[root@centos7 ~]# echo "Hello World" > test.txt 
[root@centos7 ~]# du -h test.txt 
4.0K    test.txt
[root@centos7 ~]# echo -n "" > test.txt    #要加上"-n"参数,默认情况下是"\n",就是回车符
[root@centos7 ~]# du -h test.txt  
0    test.txt

使用truncate命令清空文件

[root@centos7 ~]# du -h test.txt 
4.0K    test.txt
[root@centos7 ~]# truncj'x'fate -s 0 test.txt   -s参数用来设定文件的大小,清空文件,就设定为0;
[root@centos7 ~]# du -h test.txt 
0    test.txt
目录
相关文章
|
4天前
|
Linux
【Linux】命名管道的创建方法&&基于命名管道的两个进程通信的实现
【Linux】命名管道的创建方法&&基于命名管道的两个进程通信的实现
|
7天前
|
Linux 开发工具
Linux E325: 注意 发现交换文件 “*.swp“
Linux E325: 注意 发现交换文件 “*.swp“
15 0
|
2天前
|
Linux Shell Python
如何计算 Linux 上文件中的空行数?
【5月更文挑战第11天】
11 0
|
3天前
|
Linux API
Linux系统编程之文件编程常用API回顾和文件编程一般步骤
Linux系统编程之文件编程常用API回顾和文件编程一般步骤
Linux系统编程之文件编程常用API回顾和文件编程一般步骤
|
3天前
|
Linux
如何在 Linux 中递归更改文件的权限?
【5月更文挑战第10天】
14 3
|
4天前
|
Linux
Linux如何查询较大文件的方法
【5月更文挑战第8天】Linux如何查询较大文件的方法
7 0
|
4天前
|
Linux 网络安全
xmanager 4 连接SuSE linux server 11方法
xmanager 4 连接SuSE linux server 11方法
|
4天前
|
Linux 程序员 Shell
【Linux】详解core dump文件的作用以及用法&&ubuntu20.04下无法形成core dump文件的解决办法
【Linux】详解core dump文件的作用以及用法&&ubuntu20.04下无法形成core dump文件的解决办法
|
4天前
|
Linux Shell
Linux操作系统下查找大文件或目录的技巧
Linux操作系统下查找大文件或目录的技巧
11 2
|
5天前
|
算法 Linux
Linux:文件增删 & 文件压缩指令
Linux:文件增删 & 文件压缩指令
9 0