5 ways to examine the content of files on Linux

如何在Linux的文件,而不仅仅是文本文件的内容使用cat,更多,头尾命令来看看。

Linux的检查文件的内容,包括提供许多命令,更多,尾巴,但是这只是一个开始。

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, we’ll both at commands for viewing the contents of files and options for tailoring those views to better cater to your needs.

cat命令发送文本的全部内容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 won’t 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 haven’t 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:

美元的猫- n味精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.

cat命令可以让你忽略与-s选项重复的空行,但你必须给另一个命令添加完全压制坯线。

$猫-s味精你好 - 我希望你有一个美好的一天!以上就是......现在再见!秒。

To ignore all of the blank lines, just pipe the output fromgrep的命令如下。点(。)匹配的文本,包括空白字符,所以它会显示仅包含空格,只有看空行。

$ cat msg | grep . Hello -- I hope you are having a wonderful day! That's it for ... now bye! s.

The-Eoptoin 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.$

-一个, you get both the $ characters at the end of each line and tabs showing up as^ I而不是空洞的空间。

$猫-A味精你好 -  $ $我希望你有一个美好的一天$ $ $就是这样的......!^ Inow$ $ bye!$ $ s.$

Displaying portions of files with head and tail

The尾巴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尾巴命令的工作方法一样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.

您也可以结合尾巴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 -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更多command is an obvious choice for browsing through a file’s content a screenful at a time while添加到文件中,通过使用上下方向键,这样你就可以开始扫描,然后将文件在备份向上和向下移动的能力。

看着文本在与OD文件一旦两种方式

TheOD(八进制转储)命令,可以查看文件既为普通文字,为一系列的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

Notice how newline characters are shown as\ n(octal 012) and tabs are shown as\ t(octal 011).

其中一个的特别有用的用途OD命令是在非文本文件来获取信息识别文件类型。在这种情况下,我们看到了JFIF(JPEG File Interchange Format) label that identifies it as a jpg file to commands likefile在文件类型的报告。那里面有很多其他有用的信息,以及,特别是如果你很好奇这些文件是如何格式化。

在接下来的命令,我们正在寻找一个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Ĵ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

如果我们问filecommand 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.

$文件arrow.jpg arrow.png:JPEG图像数据,JFIF标准1.01,纵横比,密度的1x1,段长度16,基线,精密8,500x375,组件3

Looking at files as text-based images with jp2a

如果仅限于命令行,并希望得到什么特定的图像文件中包含的想法,你可以用这样的工具jp2a(jpeg to ascii) to give you a character rendition. How recognizable the image will be in this format depends on the file. Don’t expect much as the version of the image you will see is going to put “low res” to the test! This one’s 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

包起来

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.

加入对网络世界的社有个足球雷竞技app区Facebook的LinkedIn对那些顶级心态的话题发表评论。
Related:

版权© 2020Raybet2

IT薪资调查:结果是在