云网牛站
所在位置:首页 > Linux命令 > 看Linux文件的内容:用cat,less,more,head,tail,nl,od,xxd,gv,xdvi命令

看Linux文件的内容:用cat,less,more,head,tail,nl,od,xxd,gv,xdvi命令

2020-02-27 21:53:59作者:赖以葵稿源:云网牛站

使用命令在Linux系统中查看文件的内容是Linux管理员的基本技能之一,在Linux中,有许多应用程序以不同的方式显示文件内容。您可以使用cat、less、more、head、tail、nl、od、xxd、gv、xdvi命令来查看文本文件或任何其他文件。为了对此进行测试,我创建了file-demo.txt和file-demo-1.txt文件,并添加了定义和一些内容来试验这些命令。

看Linux文件的内容:用cat,less,more,head,tail,nl,od,xxd,gv,xdvi命令

 

1)cat命令

cat最常见用法是读取文件的内容。

cat命令的通用语法:

cat [Option] [File_Name]

如何在Linux上使用cat命令,以下为您讲解,参考在Linux/Unix系统上使用cat和cp命令的示例

新用户经常使用此命令读取文件,虽然不错,但效率不高:

# cat file-demo.txt

This is a test file to experiment the "File View" command.

GNU Linux

Debian

Ubuntu

Arch Linux

Linux Mint

CentOS

Red Hat

Fedora

openSUSE

Manjaro

elementary

MX Linux

Solus

Deepin

在cat命令中添加-n选项,以在cat命令输出中打印行号:

# cat -n file-demo.txt

1 This is a test file to experiment the "File View" command.

2 GNU Linux

3 Debian

4 Ubuntu

5 Arch Linux

6 Linux Mint

7 CentOS

8 Red Hat

9 Fedora

10 openSUSE

11 Manjaro

12 elementary

13 MX Linux

14 Solus

15 Deepin

使用下面的cat命令格式一次读取多个文件:

# cat file-demo.txt file-demo-1.txt

 

2)less命令

less命令使您可以使用UP & DOWN箭头键或Page UP & Page Down按钮在顶部到底部快速查看文件内容。

less命令的通用语法:

less [Option] [File_Name]

如何在Linux上使用less命令,以下为您讲解。

如果要搜索文件中与格式匹配的任何行,那么less命令是一个不错的选择。如果找到,它将突出显示给定的模式。按CTRL+n转到下一个匹配模式,然后按CTRL+p返回上一个匹配模式。

为此,请运行以下命令:

# less file-demo.txt

This is a test file to experiment the "File View" command.

GNU Linux

Debian

Ubuntu

Arch Linux

Linux Mint

CentOS

Red Hat

Fedora

openSUSE

Manjaro

elementary

MX Linux

Solus

Deepin

(END)

 

3)more命令

more命令使您可以从上至下快速在屏幕上快速查看文件内容。

more命令的通用语法:

more [Option] [File_Name]

如何在Linux上使用more命令,以下为您讲解。

它显示带有百分比的页面,另外,允许用户查找模型匹配,但是,它不会突出显示模式,并且在当前页面上也没有找到相关性:

# more file-demo.txt

This is a test file to experiment the "File View" command.

GNU Linux

Debian

Ubuntu

Arch Linux

Linux Mint

CentOS

Red Hat

Fedora

openSUSE

Manjaro

elementary

MX Linux

Solus

Deepin

--More--(18%)

 

4)head命令

head命令显示文件的开头(文件的第一部分)。

head命令的通用语法:

head [Option] [File_Name]

如何在Linux上使用head命令,以下为您讲解。

默认情况下,它将打印前10行,如果要打印其他行,可以使用(N)选项:

# head file-demo.txt

This is a test file to experiment the "File View" command.

GNU Linux

Debian

Ubuntu

Arch Linux

Linux Mint

CentOS

Red Hat

Fedora

openSUSE

 

5)tail命令

tail命令显示文件的最后一部分。

tail命令的通用语法:

tail [Option] [File_Name]

如何在Linux上使用tail命令,请参考演示使用Linux tail命令的示例

默认情况下它将打印最后10行,如果要打印其他行,可以使用(N)选项:

# tail file-demo.txt

Linux Mint

CentOS

Red Hat

Fedora

openSUSE

Manjaro

elementary

MX Linux

Solus

Deepin

 

6)nl命令

nl命令用于在屏幕上以行号打印文件内容,行号从“1”到“N”开始,这将一次在屏幕上打印文件,类似于cat命令。

nl命令的通用语法:

nl [Option] [File_Name]

如何在Linux上使用nl命令,以下为您讲解。

输入文件名,然后输入nl命令以打印带有行号的文件内容:

# nl file-demo.txt

1 This is a test file to experiment the "File View" command.

2 GNU Linux

3 Debian

4 Ubuntu

5 Arch Linux

6 Linux Mint

7 CentOS

8 Red Hat

9 Fedora

10 openSUSE

11 Manjaro

12 elementary

13 MX Linux

14 Solus

15 Deepin

 

7)od命令

od代表八进制转储,它以八进制格式(二进制格式)为默认格式以不同格式显示文件的内容。

od命令的通用语法:

od [Option] [File_Name]

如何在Linux上使用od命令,运行以下命令,会出来以下结果:

# od file-demo.txt

0000000 064124 071551 064440 020163 020141 062564 072163 063040

0000020 066151 020145 067564 062440 070170 071145 066551 067145

0000040 020164 064164 020145 043042 066151 020145 064526 073545

0000060 020042 067543 066555 067141 027144 043412 052516 046040

0000100 067151 074165 042012 061145 060551 005156 061125 067165

0000120 072564 040412 061562 020150 064514 072556 005170 064514

0000140 072556 020170 064515 072156 041412 067145 047564 005123

0000160 062522 020144 060510 005164 062506 067544 060562 067412

0000200 062560 051556 051525 005105 060515 065156 071141 005157

0000220 066145 066545 067145 060564 074562 046412 020130 064514

0000240 072556 005170 067523 072554 005163 062504 070145 067151

0000260 000012

0000261

 

8)xxd命令

xxd命令创建给定文件的十六进制转储,它还可以将十六进制转储转换回其原始二进制形式。

xxd命令的通用语法:

xxd [Option] [File_Name]

如何在Linux上使用xxd命令,运行以下命令,会出来以下结果:

# xxd file-demo.txt

00000000: 5468 6973 2069 7320 6120 7465 7374 2066  This is a test f

00000010: 696c 6520 746f 2065 7870 6572 696d 656e  ile to experimen

00000020: 7420 7468 6520 2246 696c 6520 5669 6577  t the "File View

00000030: 2220 636f 6d6d 616e 642e 0a47 4e55 204c  " command..GNU L

00000040: 696e 7578 0a44 6562 6961 6e0a 5562 756e  inux.Debian.Ubun

00000050: 7475 0a41 7263 6820 4c69 6e75 780a 4c69  tu.Arch Linux.Li

00000060: 6e75 7820 4d69 6e74 0a43 656e 744f 530a  nux Mint.CentOS.

00000070: 5265 6420 4861 740a 4665 646f 7261 0a6f  Red Hat.Fedora.o

00000080: 7065 6e53 5553 450a 4d61 6e6a 6172 6f0a  penSUSE.Manjaro.

00000090: 656c 656d 656e 7461 7279 0a4d 5820 4c69  elementary.MX Li

000000a0: 6e75 780a 536f 6c75 730a 4465 6570 696e  nux.Solus.Deepin

000000b0: 0a

 

9)gv命令

gv命令用于查看Postscript和PDF文件。

gv命令的通用语法:

gv [Option] [File_Name]

 

10)xdvi命令

xdvi命令是用于预览dvi文件的程序,Xdvi可以显示由于各种整数因素而缩小的文件,并且具有放大镜,用于查看放大的页面部分。

xdvi命令的通用语法:

xdvi [Option] [File_Name]

 

相关主题

在Linux系统中查看归档或压缩文件内容且不需要解压缩

Linux命令子频道
精选文章
热门文章