1、使用重定向的方法

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

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

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

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

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

4、使用echo命令清空文件

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

5、使用truncate命令清空文件

[root@centos7 ~]# du -h test.txt
.0K test.txt
[root@centos7 ~]# truncate -s test.txt -s参数用来设定文件的大小,清空文件,就设定为0;
[root@centos7 ~]# du -h test.txt
test.txt

参考:【Linux中国】微信公众号

将Linux文件清空的几种方法的更多相关文章

  1. 在Linux文件清空的几种方法

    在Linux文件清空的几种方法 1.使用重定向的方法 [root@centos7 ~]# du -h test.txt 4.0K test.txt [root@centos7 ~]# > tes ...

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

    这篇文章主要介绍了linux中快速清空文件内容的几种方法,需要的朋友可以参考下 $ : > filename $ > filename $ echo "" > f ...

  3. linux清空文件内容的三种方法

    linux系统中清空文件内容的三种方法 1.使用vi/vim命令打开文件后,输入"%d"清空,后保存即可.但当文件内容较大时,处理较慢,命令如下:vim file_name:%d: ...

  4. 【转】Linux 中清空或删除大文件内容的五种方法(truncate 命令清空文件)

    原文: http://www.jb51.net/article/100462.htm truncate -s 0 access.log -------------------------------- ...

  5. Linux下查看压缩文件内容的 10 种方法

    Linux下查看压缩文件内容的 10 种方法 通常来说,我们查看归档或压缩文件的内容,需要先进行解压缩,然后再查看,比较麻烦.今天给大家介绍 10 不同方法,能够让你轻松地在未解压缩的情况下查看归档或 ...

  6. Linux清除文件内容的几种方法

    # 清空或删除大文件内容的五种方法: # 法一:通过重定向到 Null 来清空文件内容 $ >test.sh # 法二:使用 ‘true' 命令重定向来清空文件 $ true > test ...

  7. 利用Linux系统生成随机密码的10种方法【转】

    Linux操作系统的一大优点是对于同样一件事情,你可以使用高达数百种方法来实现它.例如,你可以通过数十种方法来生成随机密码.本文将介绍生成随机密码的十种方法. 1. 使用SHA算法来加密日期,并输出结 ...

  8. linux安装软件的几种方法----linux下编译安装软件的一般步骤

    linux安装软件的几种方法: 一.rpm包安装方式步骤: 1.找到相应的软件包,比如soft.version.rpm,下载到本机某个目录: 2.打开一个终端,su -成root用户: 3.cd so ...

  9. php获取文件后缀的9种方法

    获取文件后缀的9种方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3 ...

随机推荐

  1. sublime text 3 license 2016.05

    补充:2016.05 最近经过测试,3个注册码在新版3103的sublime上已经不可用了. 现补充两枚新版的license key: -– BEGIN LICENSE -– Michael Barn ...

  2. Unity3D人物头顶名称与血条更新与绘制

    using UnityEngine; using System.Collections; public class NPC : MonoBehaviour { //主摄像机对象 private Cam ...

  3. 如何通过JavaScript构建Asp.net服务端控件

    摘要 虽然ASP.NET的服务器控件一直被大家所诟病,但是用户控件(ACSX)在某些场景下还是非常有用的. 在一些极特珠的情况下,我们会使用JavaScript动态的构建页面中的控件,但假设遇到了我要 ...

  4. 【HOW】如何限制Reporting Services报表导出功能中格式选项

    Reporting Services报表导出功能中缺省会提供多种导出格式选项,但很多情况下不需要全部的格式选项,因此需要对这些选项进行限制.下面我们以SQL Server 2008 R2为例来说明对这 ...

  5. Gold Game

    一.项目描述:黄金点游戏 黄金点游戏是一个数字小游戏,其游戏规则是: N个同学(N通常大于10),每人写一个0-100之间的有理数 (不包括0或100),交给裁判,裁判算出所有数字的平均值,然后乘以0 ...

  6. OS X升级El Capitan后,git difftool无法打开diffmerge的解决方法

    在git项目下执行git difftool,出现如下报错 /Library/Developer/CommandLineTools/usr/libexec/git-core/mergetools/dif ...

  7. 基于Windows服务器集群的Redis主从配置指南

    前段时间一个项目因并发量大.因防止宕机做了主从备份,首页的表连接查询又非常的耗时.故此拿出利器Redis缓存这个查询结果,并随着用户操作而更新. 因官方目前只有linux版,Windows版下载:  ...

  8. [转]解决a different object with the same identifier value was already associated with the session错误

    1.a different object with the same identifier value was already associated with the session. 错误原因:在h ...

  9. angularjs上传图片

    通过AngularJS实现图片上传及缩略图展示(读取文件内容) AngularJS图片上传功能的实现(读取文件内容) AngularJs实现Multipart/form-data 文件的上传(上传文件 ...

  10. 史上最强大的40多个纯CSS绘制的图形

    Square(正方形) #square { width: 100px; height: 100px; background: red; } Rectangle(矩形) #rectangle { wid ...