在命令行查看文件的 3 个工具
less Word ODT

> 
> 看一下 `less`、Antiword 和 `odt2xt` 这三个实用程序，它们都可以在终端中查看文件。
> 
> 
> 


![](/data/attachment/album/201902/26/143833mvlx9xo8qlsex5sz.png)


我常说，你不需要使用命令行也可以高效使用 Linux —— 我知道许多 Linux 用户从不打开终端窗口，并且也用的挺好。然而，即使我不认为自己是一名技术人员，我也会在命令行上花费大约 20% 的计算时间，包括操作文件、处理文本和使用实用程序。


我经常在终端窗口中做的一件事是查看文件，无论是文本还是需要用到文字处理器的文件。有时使用命令行实用程序比启动文本编辑器或文字处理器更容易。


下面是我在命令行中用来查看文件的三个实用程序。


### less


[less](https://www.gnu.org/software/less/) 的美妙之处在于它易于使用，它将你正在查看的文件分解为块（或页面），这使得它们更易于阅读。你可以使用它在命令行查看文本文件，例如 README、HTML 文件、LaTeX 文件或其他任何纯文本文件。我在[上一篇文章](https://opensource.com/article/18/4/using-less-view-text-files-command-line)中介绍了 `less`。


要使用 `less`，只需输入：



```
less file_name
```

![](/data/attachment/album/201902/26/143838zpd4g6yb4mpc0why.png)


通过按键盘上的空格键或 `PgDn` 键向下滚动文件，按 `PgUp` 键向上移动文件。要停止查看文件，按键盘上的 `Q` 键。


### Antiword


[Antiword](http://www.winfield.demon.nl/) 是一个很好地实用小程序，你可以使用它将 Word 文档转换为纯文本。只要你想，还可以将它们转换为 [PostScript](http://en.wikipedia.org/wiki/PostScript) 或 [PDF](http://en.wikipedia.org/wiki/Portable_Document_Format)。在本文中，让我们继续使用文本转换。


Antiword 可以读取和转换 Word 2.0 到 2003 版本创建的文件（LCTT 译注：此处疑为 Word 2000，因为 Word 2.0 for DOS 发布于 1984 年，而 WinWord 2.0 发布于 1991 年，都似乎太老了）。它不能读取 DOCX 文件 —— 如果你尝试这样做，Antiword 会显示一条错误消息，表明你尝试读取的是一个 ZIP 文件。这在技术上说是正确的，但仍然令人沮丧。


要使用 Antiword 查看 Word 文档，输入以下命令：



```
antiword file_name.doc
```

Antiword 将文档转换为文本并显示在终端窗口中。不幸的是，它不能在终端中将文档分解成页面。不过，你可以将 Antiword 的输出重定向到 `less` 或 [more](https://opensource.com/article/19/1/more-text-files-linux) 之类的实用程序，一遍对其进行分页。通过输入以下命令来执行此操作：



```
antiword file_name.doc | less
```

如果你是命令行的新手，那么我告诉你 `|` 称为管道。这就是重定向。


![](/data/attachment/album/201902/26/143845r2w7u32ok7v5j22k.png)


### odt2txt


作为一个优秀的开源公民，你会希望尽可能多地使用开放格式。对于你的文字处理需求，你可能需要处理 [ODT](http://en.wikipedia.org/wiki/OpenDocument) 文件（由诸如 LibreOffice Writer 和 AbiWord 等文字处理器使用）而不是 Word 文件。即使没有，也可能会遇到 ODT 文件。而且，即使你的计算机上没有安装 Writer 或 AbiWord，也很容易在命令行中查看它们。


怎样做呢？用一个名叫 [odt2txt](https://github.com/dstosberg/odt2txt) 的实用小程序。正如你猜到的那样，`odt2txt` 将 ODT 文件转换为纯文本。要使用它，运行以下命令：



```
odt2txt file_name.odt
```

与 Antiword 一样，`odt2txt` 将文档转换为文本并在终端窗口中显示。和 Antiword 一样，它不会对文档进行分页。但是，你也可以使用以下命令将 `odt2txt` 的输出管道传输到 `less` 或 `more` 这样的实用程序中：



```
odt2txt file_name.odt | more
```

![](/data/attachment/album/201902/26/143851jgxg0xemee2ieagr.png)


你有一个最喜欢的在命令行中查看文件的实用程序吗？欢迎留下评论与社区分享。




---


via: <https://opensource.com/article/19/2/view-files-command-line>


作者：[Scott Nesbitt](https://opensource.com/users/scottnesbitt) 选题：[lujun9972](https://github.com/lujun9972) 译者：[MjSeven](https://github.com/MjSeven) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
I always say you don't need to use the command line to use Linux effectively—I know many Linux users who never crack open a terminal window and are quite happy. However, even though I don't consider myself a techie, I spend about 20% of my computing time at the command line, manipulating files, processing text, and using utilities.

One thing I often do in a terminal window is viewing files, whether text or word processor files. Sometimes it's just easier to use a command line utility than to fire up a text editor or a word processor.

Here are three of the utilities I use to view files at the command line.

## less

The beauty of [less](https://www.gnu.org/software/less/) is that it's easy to use and it breaks the files you're viewing down into discrete chunks (or *pages*), which makes them easier to read. You use it to view text files at the command line, such as a README, an HTML file, a LaTeX file, or anything else in plaintext. I took a look at less in a [previous article](https://opensource.com/article/18/4/using-less-view-text-files-command-line).

To use less, just type:

`less file_name`



![less less](https://opensource.com/sites/default/files/uploads/less.png)

Scroll down through the file by pressing the spacebar or PgDn key on your keyboard. You can move up through a file by pressing the PgUp key. To stop viewing the file, press the Q key on your keyboard.

## Antiword

[Antiword](http://www.winfield.demon.nl/) is great little utility that you can use to that can convert Word documents to plaintext. If you want, you can also convert them to [PostScript](http://en.wikipedia.org/wiki/PostScript) or [PDF](http://en.wikipedia.org/wiki/Portable_Document_Format). For this article, let's just stick with the conversion to text.

Antiword can read and convert files created with versions of Word from 2.0 to 2003. It doesn't read DOCX files—if you try, Antiword displays an error message that what you're trying to read is a ZIP file. That's technically correct, but it's still frustrating.

To view a Word document using Antiword, type the following command:

`antiword file_name.doc`


Antiword converts the document to text and displays it in the terminal window. Unfortunately, it doesn't break the document into pages in the terminal. You can, though, redirect Antiword's output to a utility like less or [more](https://opensource.com/article/19/1/more-text-files-linux) to paginate it. Do that by typing the following command:

`antiword file_name.doc | less`


If you're new to the command line, the | is called a *pipe*. That's what does the redirection.

![Antiword Antiword](https://opensource.com/sites/default/files/uploads/antiword.png)

## odt2txt

Being a good open source citizen, you'll want to use as many open formats as possible. For your word processing needs, you might deal with [ODT](http://en.wikipedia.org/wiki/OpenDocument) files (used by such word processors as LibreOffice Writer and AbiWord) instead of Word files. Even if you don't, you might run into ODT files. And they're easy to view at the command line, even if you don't have Writer or AbiWord installed on your computer.

How? With a little utility called [odt2txt](https://github.com/dstosberg/odt2txt). As you've probably guessed, odt2txt converts an ODT file to plaintext. To use it, run the command:

`odt2txt file_name.odt`


Like Antiword, odt2txt converts the document to text and displays it in the terminal window. And, like Antiword, it doesn't page the document. Once again, though, you can pipe the output from odt2txt to a utility like less or more using the following command:

`odt2txt file_name.odt | more`



![odt2txt odt2txt](https://opensource.com/sites/default/files/uploads/odt2txt.png)

Do you have a favorite utility for viewing files at the command line? Feel free to share it with the community by leaving a comment.

## 2 Comments