L17.linux命令每日一练 -- 第三章 文件过滤及内容编辑处理命令 -- more和less命令

3.3 more:分页显示文件内容

3.3.1 命令详解

【命令星级】 ★★★★★

【功能说明】

​ more命令的功能类似于cat,但cat命令是将整个文件的内容一次性的显示在屏幕上,而more则会一页一页地显示文件内容。但more的功能还是比较简单的,其有一个增强版的命令是less,将在3.4节详细讲解。

【语法格式】

more [option] [file]
more [选项] [文件]

​ **说明:**在more命令及后面的选项和文件里,每个元素直接都至少要有一个空格。

【选项说明】

​ 表3-4针对more命令的参数选项进行了说明。

​ 表3-4 more命令的参数选项及说明

在这里插入图片描述
​ 在交互模式下,使用more命令打开文本后,会进入一个基于vi的交互界面,在这里可以使用部分vi编辑器的功能,如搜索功能,还可以切换到vi编辑器。表3-5给出了more命令的交互式子命令。

​ 表3-5 more命令的交互式子命令及说明
在这里插入图片描述

3.3.2 使用范例

3.3.2.1 基础范例

​ **范例3-14:**执行more命令,后面不接任何参数。

[root@centos7 ~]# more /etc/services	#若不接任何参数,则会满屏显示文件内容。
# /etc/services:
# $Id: services,v 1.55 2013/04/14 ovasik Exp $
#
# Network services, Internet style
# IANA services version: last updated 2013-04-10
#
# Note that it is presently the policy of IANA to assign a single well-kn
own
# port number for both TCP and UDP; hence, most entries here have two ent
ries
# even if the protocol doesn't support UDP operations.
# Updated from RFC 1700, ``Assigned Numbers'' (October 1994).  Not all po
rts
# are included, only the more common ones.
#
# The latest IANA port assignments can be gotten from
#       http://www.iana.org/assignments/port-numbers
# The Well Known Ports are those from 0 through 1023.
# The Registered Ports are those from 1024 through 49151
# The Dynamic and/or Private Ports are those from 49152 through 65535
#
# Each line describes one service, and is of the form:
#
# service-name  port/protocol  [aliases ...]   [# comment]

tcpmux          1/tcp                           # TCP port service multip
--More--(0%)	#还有已经显示内容的百分比。

​ 读者可以在上面的交互界面测试一下常用的交互命令,比如按空格键往下翻一屏,按“b”往上翻一屏,若想要查找“3306”,则先输入一个“/”,然后输入“3306”敲回车即可找到。

​ **范例3-15:**查看前n行。

[root@centos7 ~]# more -5 /etc/services	#此时并不是满屏显示文件内容,只会显示5行内容。
# /etc/services:
# $Id: services,v 1.55 2013/04/14 ovasik Exp $
#
# Network services, Internet style
# IANA services version: last updated 2013-04-10
--More--(0%)

​ **范例3-16:**从指定的行数开始显示内容。

[root@centos7 ~]# more +888 /etc/services	#此时直接从第888行显示文件内容。
rescap          283/udp                 # rescap
corerjd         284/tcp                 # corerjd
corerjd         284/udp                 # corerjd
fxp             286/tcp                 # FXP Communication
fxp             286/udp                 # FXP Communication
k-block         287/tcp                 # K-BLOCK
k-block         287/udp                 # K-BLOCK
novastorbakcup  308/tcp                 # Novastor Backup
novastorbakcup  308/udp                 # Novastor Backup
entrusttime     309/tcp                 # EntrustTime
entrusttime     309/udp                 # EntrustTime
bhmds           310/tcp                 #       bhmds
bhmds           310/udp                 #       bhmds
asip-webadmin   311/tcp                 # AppleShare IP WebAdmin
asip-webadmin   311/udp                 # AppleShare IP WebAdmin
vslmp           312/tcp                 # VSLMP
vslmp           312/udp                 # VSLMP
magenta-logic   313/tcp                 # Magenta Logic
magenta-logic   313/udp                 #       Magenta Logic
opalis-robot    314/tcp                 # Opalis Robot
opalis-robot    314/udp                 # Opalis Robot
dpsi            315/tcp                 # DPSI
dpsi            315/udp                 # DPSI
decauth         316/tcp                 # decAuth
decauth         316/udp                 # decAuth
zannet          317/tcp                 # Zannet
--More--(7%)

3.3.2.2 技巧性范例

​ **范例3-17:**分页显示目录下的内容。

[root@centos7 ~]# ls /etc/ |more -10	#大家应该知道/etc下有很多文件目录,直接ls查看则会显示太多内容,所以可以借助more命令分页显示。
abrt
adjtime
aliases
aliases.db
alternatives
anacrontab
asound.conf
audisp
audit
bash_completion.d
--More--

3.4 less:分页显示文件内容

3.4.1 命令详解

【命令星级】 ★★★★★

【功能说明】

​ 如果使用man less查看less的帮助文档,则会发现官方的解释是less为more的反义词(opposite of more)。但less命令的名称只是一个文字游戏,它是more命令的高级版本(less这个名称来自俗语“越简单就越丰富”,即 less is more)。

​ less命令的基本功能类似于more命令,可以分页显示文件内容,但比more的功能更强大。less命令在读取文件内容时,并不是想more、vi命令一样,需要一次性将整个文件加载之后再显示,而是会根据需要来加载文件内容,这样打开文件的速度更快。而且less命令支持[page up]、[page down]等按键的功能,可通过该功能往前或往后翻看文件,这样更容易查看一个文件的内容。

【语法格式】

less [option] [file]
less [选项] [文件]

​ **说明:**在less命令及后面的选项和文件里,每个元素直接都至少要有一个空格。

【选项说明】

​ 表3-6针对less命令的参数选项进行了说明。

​ 表3-6 less命令的参数选项及说明

在这里插入图片描述
​ 在交互模式下,less命令也是基于more命令和vi命令的,在这里可以使用vi编辑器的部分功能,如搜索功能,还可以切换到vi编辑器。表3-7给出了less命令的交互式子命令。

​ 表3-7 less命令的交互式子命令及说明
在这里插入图片描述
在这里插入图片描述

3.4.2 使用范例

3.4.2.1 基础范例

​ **范例3-18:**通过less命令查看文件。

[root@centos7 ~]# less /etc/services	#若不接任何参数,则会满屏显示文件内容。
# /etc/services:
# $Id: services,v 1.55 2013/04/14 ovasik Exp $
#
# Network services, Internet style
# IANA services version: last updated 2013-04-10
#
# Note that it is presently the policy of IANA to assign a single well-known
# port number for both TCP and UDP; hence, most entries here have two entries
# even if the protocol doesn't support UDP operations.
# Updated from RFC 1700, ``Assigned Numbers'' (October 1994).  Not all ports
# are included, only the more common ones.
#
# The latest IANA port assignments can be gotten from
#       http://www.iana.org/assignments/port-numbers
# The Well Known Ports are those from 0 through 1023.
# The Registered Ports are those from 1024 through 49151
# The Dynamic and/or Private Ports are those from 49152 through 65535
#
# Each line describes one service, and is of the form:
#
# service-name  port/protocol  [aliases ...]   [# comment]

tcpmux          1/tcp                           # TCP port service multip/etc/services

​ **范例3-19:**显示行号的示例。

[root@centos7 ~]# less -N /etc/services	#每一行前面都有行号了。
      1 # /etc/services:
      2 # $Id: services,v 1.55 2013/04/14 ovasik Exp $
      3 #
      4 # Network services, Internet style
      5 # IANA services version: last updated 2013-04-10
      6 #
      7 # Note that it is presently the policy of IANA to assign a single      7  well-known
      8 # port number for both TCP and UDP; hence, most entries here have      8  two entries
      9 # even if the protocol doesn't support UDP operations.
     10 # Updated from RFC 1700, ``Assigned Numbers'' (October 1994).  No     10 t all ports
     11 # are included, only the more common ones.
     12 #
     13 # The latest IANA port assignments can be gotten from
     14 #       http://www.iana.org/assignments/port-numbers
     15 # The Well Known Ports are those from 0 through 1023.
     16 # The Registered Ports are those from 1024 through 49151
     17 # The Dynamic and/or Private Ports are those from 49152 through 6     17 5535
     18 #
     19 # Each line describes one service, and is of the form:
     20 #
     21 # service-name  port/protocol  [aliases ...]   [# comment]
     22 
/etc/services

3.4.2.2 技巧性范例

​ **范例3-20:**分页显示命令结果的示例。

[root@centos7 ~]# ls /etc|less	#分页查看etc目录文件内容。
abrt
adjtime
aliases
aliases.db
alternatives
anacrontab
asound.conf
audisp
audit
bash_completion.d
bashrc
binfmt.d
centos-release
centos-release-upstream
chkconfig.d
cron.d
cron.daily
cron.deny
cron.hourly
cron.monthly
crontab
cron.weekly
crypttab
csh.cshrc
csh.login
dbus-1
:

猜你喜欢

转载自blog.csdn.net/qq_25599925/article/details/125356878