<span class="z textcut" style="text-align: left; color: rgb(153, 153, 153); text-transform: none; text-indent: 0px; letter-spacing: normal; overflow: hidden; font-family: &quot;Lantinghei SC&quot;, &quot;Helvetica Neue&quot;, &quot;Microsoft YaHei&quot;, 微软雅黑, Arial, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; font-size: 12px; font-style: normal; font-weight: normal; word-spacing: 0px; float: left; white-space: nowrap; -ms-word-wrap: break-word; -ms-text-overflow: ellipsis; max-width: 60%; orphans: 2; widows: 2; user-select: text; font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">编译自:[http://www.tecmint.com/empty-delete-file-content-linux/](http://www.tecmint.com/empty-delete-file-content-linux/)</span><span class="y" style="text-align: left; color: rgb(153, 153, 153); text-transform: none; text-indent: 0px; letter-spacing: normal; padding-left: 5px; font-family: &quot;Lantinghei SC&quot;, &quot;Helvetica Neue&quot;, &quot;Microsoft YaHei&quot;, 微软雅黑, Arial, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; font-size: 12px; font-style: normal; font-weight: normal; word-spacing: 0px; float: right; white-space: normal; -ms-word-wrap: break-word; orphans: 2; widows: 2; user-select: text; font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">作者: Aaron Kili</span>

<span class="z" style="text-align: left; color: rgb(153, 153, 153); text-transform: none; text-indent: 0px; letter-spacing: normal; font-family: &quot;Lantinghei SC&quot;, &quot;Helvetica Neue&quot;, &quot;Microsoft YaHei&quot;, 微软雅黑, Arial, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; font-size: 12px; font-style: normal; font-weight: normal; word-spacing: 0px; float: left; white-space: normal; -ms-word-wrap: break-word; orphans: 2; widows: 2; user-select: text; font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">原创:[LCTT](https://linux.cn/lctt/) [https://linux.cn/article-8024-1.html](https://linux.cn/article-8024-1.html)</span><span class="y" style="text-align: left; color: rgb(153, 153, 153); text-transform: none; text-indent: 0px; letter-spacing: normal; padding-left: 5px; font-family: &quot;Lantinghei SC&quot;, &quot;Helvetica Neue&quot;, &quot;Microsoft YaHei&quot;, 微软雅黑, Arial, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; font-size: 12px; font-style: normal; font-weight: normal; word-spacing: 0px; float: right; white-space: normal; -ms-word-wrap: break-word; orphans: 2; widows: 2; user-select: text; font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">译者: [FSSlc](https://linux.cn/lctt/FSSlc)</span>

在 Linux 终端下处理文件时,有时我们想直接清空文件的内容但又不必使用任何 [**Linux 命令行编辑器**](http://www.tecmint.com/linux-command-line-editors/) 去打开这些文件。那怎样才能达到这个目的呢?在这篇文章中,我们将介绍几种借助一些实用的命令来清空文件内容的方法。

**注意:**在我们进一步深入了解这些方法之前,请记住: 由于[**在 Linux 中一切皆文件**](http://www.tecmint.com/explanation-of-everything-is-a-file-and-types-of-files-in-linux/),你需要时刻注意,确保你将要清空的文件不是重要的用户文件或者系统文件。清空重要的系统文件或者配置文件可能会引发严重的应用失败或者系统错误。

前面已经说道,下面的这些方法都是从命令行中达到清空文件的目的。

**提示:**在下面的示例中,我们将使用名为 `access.log` 的文件来作为示例样本。

###
1. 通过重定向到 Null 来清空文件内容

清空或者让一个文件成为空白的最简单方式,是像下面那样,通过 shell 重定向 `null` (不存在的事物)到该文件:
  1. <span class="com" style="color: rgb(174, 174, 174); font-style: italic; -ms-word-wrap: break-word; user-select: text;">#</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;"> </span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;">&gt;</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;"> access</span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;">.</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;">log</span>

    Empty Large File Using Null Redirect in Linux

    在 Linux 下使用 Null 重定向来清空大文件

###
2. 使用 ‘true’ 命令重定向来清空文件

下面我们将使用 `:` 符号,它是 shell 的一个内置命令,等同于 `true` 命令,它可被用来作为一个 no-op(即不进行任何操作)。

另一种清空文件的方法是将 `:`&nbsp;或者&nbsp;`true` 内置命令的输出重定向到文件中,具体如下:
  1. <span class="com" style="color: rgb(174, 174, 174); font-style: italic; -ms-word-wrap: break-word; user-select: text;">#</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;"> </span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;">:</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;"> </span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;">&gt;</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;"> access</span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;">.</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;">log</span>
  2. <span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;">或</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;"> </span>
  3. <span class="com" style="color: rgb(174, 174, 174); font-style: italic; -ms-word-wrap: break-word; user-select: text;">#</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;"> </span><span class="kwd" style="color: rgb(226, 137, 100); -ms-word-wrap: break-word; user-select: text;">true</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;"> </span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;">&gt;</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;"> access</span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;">.</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;">log</span>

    Empty Large File Using Linux Commands

    使用 Linux 命令清空大文件

###
3. 使用 cat/cp/dd 实用工具及 /dev/null 设备来清空文件

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

所以 `/dev/null` 设备文件是一个特殊的文件,它将清空送到它这里来的所有输入,而它的输出则可被视为一个空文件。

另外,你可以通过使用 [**cat 命令**](http://www.tecmint.com/13-basic-cat-command-examples-in-linux/) 显示 `/dev/null` 的内容然后重定向输出到某个文件,以此来达到清空该文件的目的。
  1. <span class="com" style="color: rgb(174, 174, 174); font-style: italic; -ms-word-wrap: break-word; user-select: text;">#</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;"> </span><span class="kwd" style="color: rgb(226, 137, 100); -ms-word-wrap: break-word; user-select: text;">cat</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;"> </span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;">/</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;">dev</span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;">/</span><span class="kwd" style="color: rgb(226, 137, 100); -ms-word-wrap: break-word; user-select: text;">null</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;"> </span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;">&gt;</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;"> access</span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;">.</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;">log</span>

    Empty File Using cat Command

    使用 cat 命令来清空文件

    下面,我们将使用 cp 命令 复制 /dev/null 的内容到某个文件来达到清空该文件的目的,具体如下所示:

  2. <span class="com" style="color: rgb(174, 174, 174); font-style: italic; -ms-word-wrap: break-word; user-select: text;">#</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;"> </span><span class="kwd" style="color: rgb(226, 137, 100); -ms-word-wrap: break-word; user-select: text;">cp</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;"> </span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;">/</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;">dev</span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;">/</span><span class="kwd" style="color: rgb(226, 137, 100); -ms-word-wrap: break-word; user-select: text;">null</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;"> access</span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;">.</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;">log</span>

    Empty File Content Using cp Command

    使用 cp 命令来清空文件

    而下面的命令中, if 代表输入文件,of 代表输出文件。

  3. <span class="com" style="color: rgb(174, 174, 174); font-style: italic; -ms-word-wrap: break-word; user-select: text;">#</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;"> </span><span class="kwd" style="color: rgb(226, 137, 100); -ms-word-wrap: break-word; user-select: text;">dd</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;"> </span><span class="kwd" style="color: rgb(226, 137, 100); -ms-word-wrap: break-word; user-select: text;">if</span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;">=</span><span class="str" style="color: rgb(101, 176, 66); -ms-word-wrap: break-word; user-select: text;">/dev/</span><span class="kwd" style="color: rgb(226, 137, 100); -ms-word-wrap: break-word; user-select: text;">null</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;"> of</span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;">=</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;">access</span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;">.</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;">log</span>

    Empty File Content Using dd Command

    使用 dd 命令来清空文件内容

###
4. 使用 echo 命令清空文件

在这里,你可以使用 [**echo 命令**](http://www.tecmint.com/echo-command-in-linux/) 将空字符串的内容重定向到文件中,具体如下:
  1. <span class="com" style="color: rgb(174, 174, 174); font-style: italic; -ms-word-wrap: break-word; user-select: text;">#</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;"> </span><span class="kwd" style="color: rgb(226, 137, 100); -ms-word-wrap: break-word; user-select: text;">echo</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;"> </span><span class="str" style="color: rgb(101, 176, 66); -ms-word-wrap: break-word; user-select: text;">&quot;&quot;</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;"> </span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;">&gt;</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;"> access</span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;">.</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;">log</span>
  2. <span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;">或者</span>
  3. <span class="com" style="color: rgb(174, 174, 174); font-style: italic; -ms-word-wrap: break-word; user-select: text;">#</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;"> </span><span class="kwd" style="color: rgb(226, 137, 100); -ms-word-wrap: break-word; user-select: text;">echo</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;"> </span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;">&gt;</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;"> access</span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;">.</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;">log</span>

    Empty File Using echo Command

    使用 echo 命令来清空文件

    注意:你应该记住空字符串并不等同于 null 。字符串表明它是一个具体的事物,只不过它的内容可能是空的,但 null 则意味着某个事物并不存在。

    基于这个原因,当你将 echo 命令 的输出作为输入重定向到文件后,使用 cat 命令 来查看该文件的内容时,你将看到一个空白行(即一个空字符串)。

    要将 null 做为输出输入到文件中,你应该使用 -n 选项,这个选项将告诉 echo 不再像上面的那个命令那样输出结尾的那个新行。

  4. <span class="com" style="color: rgb(174, 174, 174); font-style: italic; -ms-word-wrap: break-word; user-select: text;">#</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;"> </span><span class="kwd" style="color: rgb(226, 137, 100); -ms-word-wrap: break-word; user-select: text;">echo</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;"> </span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;">-</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;">n </span><span class="str" style="color: rgb(101, 176, 66); -ms-word-wrap: break-word; user-select: text;">&quot;&quot;</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;"> </span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;">&gt;</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;"> access</span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;">.</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;">log</span>

    Empty File Using Null Redirect

    使用 Null 重定向来清空文件

###
5. 使用 truncate 命令来清空文件内容

`truncate` 可被用来[**将一个文件缩小或者扩展到某个给定的大小**](http://www.tecmint.com/parted-command-to-create-resize-rescue-linux-disk-partitions/)。

你可以利用它和 `-s` 参数来特别指定文件的大小。要清空文件的内容,则在下面的命令中将文件的大小设定为 0:
  1. <span class="com" style="color: rgb(174, 174, 174); font-style: italic; -ms-word-wrap: break-word; user-select: text;">#</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;"> </span><span class="kwd" style="color: rgb(226, 137, 100); -ms-word-wrap: break-word; user-select: text;">truncate</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;"> </span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;">-</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;">s </span><span class="lit" style="color: rgb(51, 135, 204); -ms-word-wrap: break-word; user-select: text;">0</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;"> access</span><span class="pun" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;">.</span><span class="pln" style="color: rgb(184, 255, 184); -ms-word-wrap: break-word; user-select: text;">log</span>

    Truncate File Content in Linux

    在 Linux 中截断文件内容

    我要介绍的就是这么多了。在本文中,我们介绍了几种通过使用一些简单的命令行工具和 shell 重定向机制来清除或清空文件内容的方法。

    上面介绍的这些可能并不是达到清空文件内容这个目的的所有可行的实践方法,所以你也可以通过下面的评论栏告诉我们本文中尚未提及的其他方法。