translated

This commit is contained in:
geekpi 2020-06-18 08:41:46 +08:00
parent 860f49796f
commit ec93357547
2 changed files with 242 additions and 241 deletions

View File

@ -1,241 +0,0 @@
[#]: collector: (lujun9972)
[#]: translator: (geekpi)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (5 ways to examine the content of files on Linux)
[#]: via: (https://www.networkworld.com/article/3561490/5-ways-to-examine-the-content-of-files-on-linux.html)
[#]: author: (Sandra Henry-Stocker https://www.networkworld.com/author/Sandra-Henry_Stocker/)
5 ways to examine the content of files on Linux
======
How to use the cat, more, head and tail commands to look at the content of Linux files, not just text files.
[mari lezhava / Unsplash][1] [(CC0)][2]
Linux provides many commands for examining the contents of files including **cat**, **more,** **head** and **tail,** but that's just a start.
For one thing, even the most obvious commands have a lot more options than many Linux users ever get around to using. And there are some less obvious commands that offer some unique features. In this post, well both at commands for viewing the contents of files and options for tailoring those views to better cater to your needs.
### cat
The cat command sends the entire contents of text files to your terminal window for viewing. In fact, if you type “cat” followed by the name of a file with thousands of lines, those lines will whiz by your window so fast, you wont be able to make out much more of it than the last screenful of text. Yet, as familiar as the cat command is to Linux users, even this basic command provides a lot of useful options such as numbering the lines in the output that many of us likely havent ever used. To expand on that, not only can you number lines; you have some choices in how you do the numbering.
Numbering every line looks like this:
```
$ cat -n msg
1 Hello --
2
3 I hope you are having a wonderful day!
4
5
6 That's it for ... now
7
8 bye!
9
10 s.
```
You can also number only lines with content. Note that a line containing only blanks is not considered “empty” for this command and would be numbered.
```
$ cat -b msg
1 Hello --
2 I hope you are having a wonderful day!
3 That's it for ... now
4 bye!
5 s.
```
The cat command allows you to ignore repeated blank lines with the -s option, but you have to add another command to suppress blanks lines altogether.
```
$ cat -s msg
Hello --
I hope you are having a wonderful day!
That's it for ... now
bye!
s.
```
To ignore all of the blank lines, just pipe the output from **cat** to a **grep** command as follows. The dot (.) matches text including blank characters, so it will display lines that contain only blanks and only look empty.
```
$ cat msg | grep .
Hello --
I hope you are having a wonderful day!
That's it for ... now
bye!
s.
```
The **-E** optoin provides a visual cue to show whether there are extra blanks at the ends of lines by sticking a $ at the end of every line.
```
$ cat -E msg
Hello --$
$
I hope you are having a wonderful day! $
$
$
That's it for ... now$
$
bye!$
$
s.$
```
With **-A**, you get both the $ characters at the end of each line and tabs showing up as **^I** rather than empty space.
```
$ cat -A msg
Hello --$
$
I hope you are having a wonderful day!$
$
$
Thats it for …^Inow$
$
bye!$
$
s.$
```
### Displaying portions of files with head and tail
The **head** and **tail** commands show the tops or bottoms of files and default to ten lines. You can specify a different number of lines to view by using strings like -3 (show 3 lines) or -11 (show 11 lines). The **tail** command works the same way as **head** but displays the bottoms of files rather than the tops.
```
$ head -3 msg
Hello --
I hope you are having a wonderful day!
$ tail -3 msg
bye!
s.
```
You can also combine **head** and **tail** commands to view text between the tops of bottoms of files. You just have to pick your starting point and how many lines you want to see. In this example, the command would display the second hundred lines in a file and, with **cat**'s help, numbering those lines.
```
$ cat -b mybigfile | head -200 | tail -100
101 Invoice #2020-06-07a sent to vendor
...
```
### Browsing text a screenful at a time with more or less
The **more** command is an obvious choice for browsing through a files content a screenful at a time while **less** adds the ability to move up and down in a file by using the up and down keyboard arrows so that you can start scanning and then back up in the file.
### Looking at text files two ways at once with od
The **od** (octal dump) command allows you to view a file both as normal text and as a series of ASCII values (i.e., how that text is actually encoded in the files). As you can see in the example below, the numbered lines show the numeric ASCII values and the alternate lines show the text and non-printable characters.
```
$ od -bc msg
0000000 110 145 154 154 157 040 055 055 012 012 111 040 150 157 160 145
H e l l o - - \n \n I h o p e
0000020 040 171 157 165 040 141 162 145 040 150 141 166 151 156 147 040
y o u a r e h a v i n g
0000040 141 040 167 157 156 144 145 162 146 165 154 040 144 141 171 041
a w o n d e r f u l d a y !
0000060 012 012 012 124 150 141 164 047 163 040 151 164 040 146 157 162
\n \n \n T h a t ' s i t f o r
0000100 040 056 056 056 011 156 157 167 012 012 142 171 145 041 012 012
. . . \t n o w \n \n b y e ! \n \n
0000120 163 056 012
s . \n
```
Notice how newline characters are shown as **\n** (octal 012) and tabs are shown as **\t** (octal 011).
One of the especially helpful uses of the **od** command is for looking at non-text files for information that identifies the file types. In this case, we see the **JFIF** (JPEG File Interchange Format) label that identifies it as a jpg file to commands like **file** that report on file types. There's a lot of other useful information in there as well, especially if you're curious about how these files are formatted.
In this next command, we are looking at the beginning portion of a jpg file.
```
$ od -bc arrow.jpg | head -12
0000000 377 330 377 340 000 020 112 106 111 106 000 001 001 000 000 001
377 330 377 340 \0 020 J F I F \0 001 001 \0 \0 001
0000020 000 001 000 000 377 333 000 103 000 003 002 002 002 002 002 003
\0 001 \0 \0 377 333 \0 C \0 003 002 002 002 002 002 003
0000040 002 002 002 003 003 003 003 004 006 004 004 004 004 004 010 006
002 002 002 003 003 003 003 004 006 004 004 004 004 004 \b 006
0000060 006 005 006 011 010 012 012 011 010 011 011 012 014 017 014 012
006 005 006 \t \b \n \n \t \b \t \t \n \f 017 \f \n
0000100 013 016 013 011 011 015 021 015 016 017 020 020 021 020 012 014
\v 016 \v \t \t \r 021 \r 016 017 020 020 021 020 \n \f
0000120 022 023 022 020 023 017 020 020 020 377 333 000 103 001 003 003
022 023 022 020 023 017 020 020 020 377 333 \0 C 001 003 003
```
If we were to ask the **file** command for information on this image file, we might see something like this. The file command is pulling all this descriptive information from data at the beginning of the file.
```
$ file arrow.jpg
arrow.png: JPEG image data, JFIF standard 1.01, aspect ratio, density 1x1, segment length 16, baseline, precision 8, 500x375, components 3
```
### Looking at files as text-based images with jp2a
If you are limited to the command line and want to get an idea what a particular image file contains, you can use a tool like **jp2a** (jpeg to ascii) to give you a character rendition. How recognizable the image will be in this format depends on the file. Dont expect much as the version of the image you will see is going to put “low res” to the test! This ones a very low resolution emperor penguin.
```
$ jp2a Emperor_Penguin.jpg
MMMMMMMMWOdkNMMMMMMMMMMMMMMMMMMM
MMMXK0kc.... ,OKMMMMMMMMMMMMMMMM
MMNK0Ol... :Xx'dNMMMMMMMMMMMMM
MMMMMMMd;lx00Oo. ..xMMMMMMMMMMMM
MMMMMMK.OXMMMMMN,...lMMMMMMMMMMM
MMMMMMx'KXNNMMMMK....0MMMMMMMMMM
MMMMMMx:kkKNWWMMMl.. 'NMMMMMMMMM
MMMMMMddx0NNNWMMMK'...;NMMMMMMMM
MMMMMMck0NNWWWWWMMd ..lMMMMMMMM
MMMMMM.d0KXNWWWWMMo ...WMMMMMMM
MMMMMM.xOXNNWNMMMW. ....KMMMMMMM
MMMMMM'kKNKWXWMMMK ..'.0MMMMMMM
MMMMMMxckXNNNNMMMX .:..XMMMMMMM
MMMMMMW;xKNWWWMMMM. .;. NMMMMMMM
MMMMMMMok0NNWNWMMMx .l..MMMMMMMM
MMMMMMMkxOKXWXNMMMMl.:'dMMMMMMMM
MMMMMMM0dKOdKXXNMMMMNx,WMMMMMMMM
MMMMMMMWoKxldXKNNMMMMM;MMMMMMMMM
MMMMMMMMxxxxdNWNXNMMMM;MMMMMMMMM
MMMMMMMMxOcoo0XOOOOWMW,kMMMMMMMM
MMMMMMM0xK;.cO0dNX:0XXd;NMMMMMMM
MMMNkdd:,'ldXXO0xl;x0kx:;lKMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
```
#### Wrap-Up
There are quite a few commands on Linux for examining the content of files in various ways. Some have options which can prove very helpful when you need to manipulate file contents. Others are just … interesting.
Join the Network World communities on [Facebook][3] and [LinkedIn][4] to comment on topics that are top of mind.
--------------------------------------------------------------------------------
via: https://www.networkworld.com/article/3561490/5-ways-to-examine-the-content-of-files-on-linux.html
作者:[Sandra Henry-Stocker][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://www.networkworld.com/author/Sandra-Henry_Stocker/
[b]: https://github.com/lujun9972
[1]: https://unsplash.com/photos/q65bNe9fW-w
[2]: https://creativecommons.org/publicdomain/zero/1.0/
[3]: https://www.facebook.com/NetworkWorld/
[4]: https://www.linkedin.com/company/network-world

View File

@ -0,0 +1,242 @@
[#]: collector: (lujun9972)
[#]: translator: (geekpi)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (5 ways to examine the content of files on Linux)
[#]: via: (https://www.networkworld.com/article/3561490/5-ways-to-examine-the-content-of-files-on-linux.html)
[#]: author: (Sandra Henry-Stocker https://www.networkworld.com/author/Sandra-Henry_Stocker/)
在 Linux 上查看文件内容的 5 种方法
======
如何使用 cat、more、head 和 tail 命令查看 Linux 文件的内容,而不仅仅是文本文件。
[mari lezhava / Unsplash][1] [(CC0)][2]
Linux 提供了许多命令来查看文件的内容,包 括**cat**、**more**、**head** 和 **tail**,但这只是一个开始。
一方面,即使是最显而易见的命令也有很多许多用户不会去使用的选项。还有一些不太明显的命令提供了一些独特的功能。在本文中,我们将同时提供查看文件内容的命令和用于定制这些视图以更好地满足你的需求的选项。
### cat
cat 命令将文本文件的全部内容发送到终端窗口以供查看。实际上,如果你输入 “cat”然后输入包含数千行的文件名那么这些行将以极快的速度在你的窗口中滚动你将无法看到除了最后一屏外的其他文本。对于 Linux 用户来说 cat 命令很熟悉,但即使这个基本命令也提供了许多有用的选项,例如对输出中的行进行编号,这是我们许多人可能从未使用过的。扩展一下,不仅可以对行进行编号,你还可以选择如何编号。
对每行进行编号就像这样:
```
$ cat -n msg
1 Hello --
2
3 I hope you are having a wonderful day!
4
5
6 That's it for ... now
7
8 bye!
9
10 s.
```
你也可以仅对内容行编号。 请注意,对于此命令,仅包含空格的行不被视为“空”,而是会被编号。
```
$ cat -b msg
1 Hello --
2 I hope you are having a wonderful day!
3 That's it for ... now
4 bye!
5 s.
```
cat 命令允许你使用 -s 选项忽略重复的空白行,但是你必须添加另一个命令来完全禁止空白行。
```
$ cat -s msg
Hello --
I hope you are having a wonderful day!
That's it for ... now
bye!
s.
```
要忽略所有空白行,只需如下将 **cat** 的输出通过管道传递给 **grep** 命令。 点(.)匹配包含空白字符的文本,因此它将显示仅包含空格且看起来为空的行。
```
$ cat msg | grep .
Hello --
I hope you are having a wonderful day!
That's it for ... now
bye!
s.
```
**-E** 选项通过在每行末尾加 $ 符提供视觉提示,来显示行尾是否还有多余的空格。
```
$ cat -E msg
Hello --$
$
I hope you are having a wonderful day! $
$
$
That's it for ... now$
$
bye!$
$
s.$
```
使用 **-A** 时,每行的末尾都有 $ 字符,并且制表符都显示为 **^I** 而不是空白。
```
$ cat -A msg
Hello --$
$
I hope you are having a wonderful day!$
$
$
Thats it for …^Inow$
$
bye!$
$
s.$
```
### 使用 head 和 tail 显示文件部分内容
**head** 和 **tail** 显示文件的头部或底部,默认为十行。 你可以使用 -3显示 3 行)或 -11显示 11 行)之类的字符串来指定要查看的其他行数。**tail** 命令与 **head** 的工作方式相同,但是显示文件的底部而不是头部。
```
$ head -3 msg
Hello --
I hope you are having a wonderful day!
$ tail -3 msg
bye!
s.
```
你还可以结合使用 head 和 tail 命令来查看文件中间的文本。 你只需要选择起点和想要查看行数即可。 在此例中,命令将在文件中显示第二个一百行,并在 **cat** 的帮助下为这些行编号。
```
$ cat -b mybigfile | head -200 | tail -100
101 Invoice #2020-06-07a sent to vendor
...
```
### 使用 more 或者 less 浏览一屏文本
**more** 命令是一次浏览一屏内容的选择,而 **less** 通过使用上下键盘箭头增加了在文件中上下移动的能力,这样你就可以扫描,然后备份到文件中。
### 使用 od 查看文本的两种方法
**od**(八进制转储)命令能够以常规文本和一系列 ASCII 值(即该文本在文件中的实际编码方式)的形式查看。 在下面的例子中可以看到,带编号的行显示了 ASCII 数字值,而其他各行则显示了文本和不可打印的字符。
```
$ od -bc msg
0000000 110 145 154 154 157 040 055 055 012 012 111 040 150 157 160 145
H e l l o - - \n \n I h o p e
0000020 040 171 157 165 040 141 162 145 040 150 141 166 151 156 147 040
y o u a r e h a v i n g
0000040 141 040 167 157 156 144 145 162 146 165 154 040 144 141 171 041
a w o n d e r f u l d a y !
0000060 012 012 012 124 150 141 164 047 163 040 151 164 040 146 157 162
\n \n \n T h a t ' s i t f o r
0000100 040 056 056 056 011 156 157 167 012 012 142 171 145 041 012 012
. . . \t n o w \n \n b y e ! \n \n
0000120 163 056 012
s . \n
```
请注意,换行符显示为 **\n** (八进制 012而制表符显示为 **\t**(八进制 011
**od** 命令特别有用的用途之一是查看非文本文件以获取标识文件类型的信息。 在这里,我们看到 **JFIF**JPEG 文件交换格式)标签,该标签让 **file** 之类报告文件类型的命令将它标示为 jpg 文件。 这里还有很多其他有用的信息,特别是如果你对这些文件的格式感到好奇的话。
在接下来的命令中,我们查看 jpg 文件的开始部分。
```
$ od -bc arrow.jpg | head -12
0000000 377 330 377 340 000 020 112 106 111 106 000 001 001 000 000 001
377 330 377 340 \0 020 J F I F \0 001 001 \0 \0 001
0000020 000 001 000 000 377 333 000 103 000 003 002 002 002 002 002 003
\0 001 \0 \0 377 333 \0 C \0 003 002 002 002 002 002 003
0000040 002 002 002 003 003 003 003 004 006 004 004 004 004 004 010 006
002 002 002 003 003 003 003 004 006 004 004 004 004 004 \b 006
0000060 006 005 006 011 010 012 012 011 010 011 011 012 014 017 014 012
006 005 006 \t \b \n \n \t \b \t \t \n \f 017 \f \n
0000100 013 016 013 011 011 015 021 015 016 017 020 020 021 020 012 014
\v 016 \v \t \t \r 021 \r 016 017 020 020 021 020 \n \f
0000120 022 023 022 020 023 017 020 020 020 377 333 000 103 001 003 003
022 023 022 020 023 017 020 020 020 377 333 \0 C 001 003 003
```
如果我们要 **file** 命令提供有关此图像的信息我们可能会看到类似这样的信息。file 命令从文件开头的数据中提取所有这些描述性信息。
```
$ file arrow.jpg
arrow.png: JPEG image data, JFIF standard 1.01, aspect ratio, density 1x1, segment length 16, baseline, precision 8, 500x375, components 3
```
### 使用 jp2a 将文件视为基于文本的图像
如果你只能在命令行,并且想了解特定图像包含的内容,那么可以使用 **jp2a**jpeg to ascii之类的工具提供字符再现。 图像在这种格式下的识别程度取决于文件。不要太期待,因为你将看到的图像版本是“低分辨率”下的测试! 这是一只分辨率很低的帝王企鹅。
```
$ jp2a Emperor_Penguin.jpg
MMMMMMMMWOdkNMMMMMMMMMMMMMMMMMMM
MMMXK0kc.... ,OKMMMMMMMMMMMMMMMM
MMNK0Ol... :Xx'dNMMMMMMMMMMMMM
MMMMMMMd;lx00Oo. ..xMMMMMMMMMMMM
MMMMMMK.OXMMMMMN,...lMMMMMMMMMMM
MMMMMMx'KXNNMMMMK....0MMMMMMMMMM
MMMMMMx:kkKNWWMMMl.. 'NMMMMMMMMM
MMMMMMddx0NNNWMMMK'...;NMMMMMMMM
MMMMMMck0NNWWWWWMMd ..lMMMMMMMM
MMMMMM.d0KXNWWWWMMo ...WMMMMMMM
MMMMMM.xOXNNWNMMMW. ....KMMMMMMM
MMMMMM'kKNKWXWMMMK ..'.0MMMMMMM
MMMMMMxckXNNNNMMMX .:..XMMMMMMM
MMMMMMW;xKNWWWMMMM. .;. NMMMMMMM
MMMMMMMok0NNWNWMMMx .l..MMMMMMMM
MMMMMMMkxOKXWXNMMMMl.:'dMMMMMMMM
MMMMMMM0dKOdKXXNMMMMNx,WMMMMMMMM
MMMMMMMWoKxldXKNNMMMMM;MMMMMMMMM
MMMMMMMMxxxxdNWNXNMMMM;MMMMMMMMM
MMMMMMMMxOcoo0XOOOOWMW,kMMMMMMMM
MMMMMMM0xK;.cO0dNX:0XXd;NMMMMMMM
MMMNkdd:,'ldXXO0xl;x0kx:;lKMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
```
#### 总结
Linux 上有很多命令可以通过各种方式查看文件的内容。 其中一些选项在你需要处理文件内容时可能会非常有用。 其他的只是……有趣。
加入 [Facebook][3] 和 [LinkedIn][4] 上的 Network World 社区,评论热门主题。
--------------------------------------------------------------------------------
via: https://www.networkworld.com/article/3561490/5-ways-to-examine-the-content-of-files-on-linux.html
作者:[Sandra Henry-Stocker][a]
选题:[lujun9972][b]
译者:[geekpi](https://github.com/geekpi)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://www.networkworld.com/author/Sandra-Henry_Stocker/
[b]: https://github.com/lujun9972
[1]: https://unsplash.com/photos/q65bNe9fW-w
[2]: https://creativecommons.org/publicdomain/zero/1.0/
[3]: https://www.facebook.com/NetworkWorld/
[4]: https://www.linkedin.com/company/network-world