Linux more与less命令
阅读原文时间:2023年07月10日阅读:1

more (more) 该命令一次显示一屏文本,满屏后停下来,并且在屏幕的底部出现一个提示信息,给出至今己显示的该文件的百分比,方便逐页阅读(file perusal filter for crt viewing) 。more名单中内置了若干快捷键,按空白键(space)就往下一页显示,按 b 键就会往回(back)一页显示,而且还有搜寻字串的功能(与 vi 相似),使用中按h可以查看说明文件 。

more [-dlfpcsu] [-num] [+/pattern] [+linenum] [fileNames..]

参数:

- num 一次显示的行数

-d 提示使用者,在画面下方显示 [Press space to continue, 'q' to quit.] 如果按错键,则会显示 [Press 'h' for instructions.] 而不是 '哔' 声

-l 取消遇见特殊字元 ^L(送纸字元)时会暂停的功能 忽略Ctrl+l(换页)字符

  • f 计算行数时,以实际上的行数,而非自动换行过后的行数(有些单行字数太长的会被扩展为两行或两行以上)

  • p 不以卷动的方式显示每一页,而是先清除萤幕后再显示内容-c 跟 -p 相似,不同的是先显示内容再清除其他旧资料

-s squeese 当遇到有连续两行以上的空白行,就代换为一行的空白行

-u 不显示下引号 (根据环境变数 TERM 指定的 terminal 而有所不同)

+/ 在每个档案显示前搜寻该字串(pattern),然后从该字串之后开始显示

+num 从第 num 行开始显示

fileNames 欲显示内容的档案,可为复数个数

more以后可以使用的操作命令(按h 弹出)

空格键 向下滚动一屏
z Display next k lines of text [current screen size]*
Display next k lines of text []*
d or ctrl-D Scroll k lines [current scroll size, initially ]*
q or Q or 退出more
s Skip forward k lines of text []
f Skip forward k screenfuls of text []
b or ctrl-B 返回上一屏
' 跳转到上次查找开始的位置
= 输出当前行的行号
/

模式 查找符合模式的内容,按n为下一个,N为上一个
n Search for kth occurrence of last r.e []
!命令 or :! 命令 调用Shell,并执行命令
v 调用vi

ctrl-L Redraw screen

:n Go to kth next file []

:p Go to kth previous file []

:f 输出文件名和当前行的行号

. 执行上一个命令

4、示例

1、显示文件file的内容,但在显示之前先清屏,并且在屏幕的最下方显示完核的百分比。

[root@oracledb log]# more -dc boot.log

2、每次显示5行

[root@oracledb log]# more - boot.log
Welcome to CentOS
Starting udev: [ OK ]
Setting hostname oracledb: [ OK ]
Setting up Logical Volume Management: [ OK ]
Checking filesystems

--More--(%)

按空格键显示5-10行,不清屏

[root@oracledb log]# more - boot.log
Welcome to CentOS
Starting udev: [ OK ]
Setting hostname oracledb: [ OK ]
Setting up Logical Volume Management: [ OK ]
Checking filesystems
/dev/sda3: clean, / files, / blocks
/dev/sda1: clean, / files, / blocks
[ OK ]
Remounting root filesystem in read-write mode: [ OK ]
Mounting local filesystems: [ OK ]

--More--(%)

3、每次显示5行,清屏显示

[root@oracledb log]# more -p - boot.log
Welcome to CentOS
Starting udev: [ OK ]
Setting hostname oracledb: [ OK ]
Setting up Logical Volume Management: [ OK ]
Checking filesystems
--More--(%)

按空格

/dev/sda3: clean, / files, / blocks
/dev/sda1: clean, / files, / blocks
[ OK ]
Remounting root filesystem in read-write mode: [ OK ]
Mounting local filesystems: [ OK ]
--More--(%)

4、more +10 filename 从第 10 行开始显示 file之档案内容。

[root@oracledb log]# more + boot.log
Mounting local filesystems: [ OK ]
Enabling local filesystem quotas: [ OK ]
Enabling /etc/fstab swaps: [ OK ]

5、more -s file 逐页显示 testfile 之档案内容,如有连续两行以上空白行则以一行空白行显示。

[root@oracledb study]# more -s test.log

2018前面有两空行

6、more进行中!执行命令

!ifconfig
eth5 Link encap:Ethernet HWaddr :::::C4
inet addr:172.17.22.31 Bcast:172.17.23.255 Mask:255.255.254.0
inet6 addr: fe80::a00:27ff:fe60:11c4/ Scope:Link
UP BROADCAST RUNNING MULTICAST MTU: Metric:
RX packets: errors: dropped: overruns: frame:
TX packets: errors: dropped: overruns: carrier:
collisions: txqueuelen:
RX bytes: (898.0 MiB) TX bytes: (4.8 MiB)

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::/ Scope:Host
UP LOOPBACK RUNNING MTU: Metric:
RX packets: errors: dropped: overruns: frame:
TX packets: errors: dropped: overruns: carrier:
collisions: txqueuelen:
RX bytes: (1.5 KiB) TX bytes: (1.5 KiB)


--More--(%)

7、根据模式查找字符串,输入斜杠 /模式字符串 回车进行查找,n和N控制向上和向下查找

# Mandatory: no

Range: -

上面为more的显示内容

/Hostname=Zabbix server

查找出的结果在最上面显示

…skipping

Hostname=

Hostname=Zabbix server

Option: HostnameItem

Item used for generating Hostname if it is undefined. Ignored if Hostname is defined.

Does not support UserParameters or aliases.

less命令 与 more命令 非常类似,但less命令 可以更加随意地浏览文件,而且 less 在查看之前不会加载整个文件。

相比较于more命令,很多人更喜欢使用 less命令。

一、Linux less命令语法

less [参数] 文件

less命令非常强大,在此只介绍几个常用的参数,更多参数使用man less来查看Linux帮助手册。

-b <缓冲区大小> 设置缓冲区的大小

-e 当文件显示结束后,自动离开

-f 强迫打开特殊文件,例如外围设备代号、目录和二进制文件

-g 只标志最后搜索的关键词

-i 忽略搜索时的大小写

-m 显示类似more命令的百分比

-N 显示每行的行号

-o <文件名> 将less 输出的内容在指定文件中保存起来

-Q 不使用警告音

-s 显示连续空行为一行

-S 行过长时间将超出部分舍弃

-x <数字> 将"tab"键显示为规定的数字空格

/字符串:向下搜索"字符串"的功能

?字符串:向上搜索"字符串"的功能

n:重复前一个搜索(与 / 或 ? 有关)

N:反向重复前一个搜索(与 / 或 ? 有关)

b 向后翻一页

d 向后翻半页

h 显示帮助界面

Q 退出less 命令

u 向前滚动半页

y 向前滚动一行

空格键 滚动一行

回车键 滚动一页

[pagedown]: 向下翻动一页

[pageup]: 向上翻动一页

1)查看文件

$less linuxdaxue.com-access_log
120.26.216.168 - - [/Jun/::: +] "HEAD / HTTP/1.1" -
42.96.189.63 - - [/Jun/::: +] "HEAD / HTTP/1.1" -
123.56.138.37 - - [/Jun/::: +] "HEAD / HTTP/1.1" -
120.26.216.168 - - [/Jun/::: +] "HEAD / HTTP/1.1" -
42.96.189.63 - - [/Jun/::: +] "HEAD / HTTP/1.1" -
linuxdaxue.com-access_log

2)一次查看多个文件

#less linuxdaxue.com-access_log linuxdaxue.com-error_log
120.26.216.168 - - [/Jun/::: +] "HEAD / HTTP/1.1" -
42.96.189.63 - - [/Jun/::: +] "HEAD / HTTP/1.1" -
123.56.138.37 - - [/Jun/::: +] "HEAD / HTTP/1.1" -
120.26.216.168 - - [/Jun/::: +] "HEAD / HTTP/1.1" -
42.96.189.63 - - [/Jun/::: +] "HEAD / HTTP/1.1" -
123.56.138.37 - - [/Jun/::: +] "HEAD / HTTP/1.1" -
linuxdaxue.com-access_log (file of )

说明:
输入 :n后,切换到linuxdaxue.com-error_log 
输入 :p 后,切换到linuxdaxue.com-access_log

接下来给大家介绍一些在less命令比较常用的操作命令,希望大家能够熟练掌握。

1)分屏导航

ctrl + F - 向前移动一屏

ctrl + B - 向后移动一屏

ctrl + D - 向前移动半屏

ctrl + U - 向后移动半屏

2)单行导航

j - 向前移动一行

k - 向后移动一行

3)其它导航

G - 移动到最后一行

g - 移动到第一行

4)其它命令

v - 使用配置的编辑器编辑当前文件

h - 显示 less 的帮助文档

&pattern - 仅显示匹配模式的行,而不是整个文件

q / ZZ - 退出 less 命令

5)标记导航

当使用 less 查看内容比较多的文件时,可以作一个标记,然后就能通过命令导航到标有特定标记的文本位置,方式如下所示:

ma - 使用 a 标记文本的当前位置

'a - 导航到标记 a 处