less(less) 命令可以对文件或其它输出进行分页显示,与moe命令相似,但是比more命令要强大许多。应该说是linux正统查看文件内容的工具。
用法:less [选项]... [文件]...
下面是less命令的可用选项,直接照搬文档了,部分有解释:
-? ........ --help 显示帮助
-a ........ --search-skip-screen
Forward search skips current screen.
-b [N] .... --buffers=[N]
Number of buffers. 设置缓冲区的大小
-B ........ --auto-buffers
Don't automatically allocate buffers for pipes.
-c -C .... --clear-screen --CLEAR-SCREEN
Repaint by clearing rather than scrolling. 先清除屏幕后再显示内容
-d ........ --dumb
Dumb terminal.
-D [xn.n] . --color=xn.n
Set screen colors. (MS-DOS only)
-e -E .... --quit-at-eof --QUIT-AT-EOF
Quit at end of file. 当文件显示结束后,自动离开
-f ........ --force
Force open non-regular files.强迫打开特殊文件,例如外围设备代号、目录和二进制文件
-F ........ --quit-if-one-screen
Quit if entire file fits on first screen.
-g ........ --hilite-search
Highlight only last match for searches.不加亮显示搜索到的所有关键词,仅显示当前显示的关键字,以提高显示速度;
-G ........ --HILITE-SEARCH
Don't highlight any matches for searches.
-h [N] .... --max-back-scroll=[N]
Backward scroll limit.
-i ........ --ignore-case
Ignore case in searches that do not contain uppercase. 忽略搜索时的大小写
-I ........ --IGNORE-CASE
Ignore case in all searches.
-j [N] .... --jump-target=[N]
Screen position of target lines.
-J ........ --status-column
Display a status column at left edge of screen.
-k [file] . --lesskey-file=[file]
Use a lesskey file.
-L ........ --no-lessopen
Ignore the LESSOPEN environment variable.
-m -M .... --long-prompt --LONG-PROMPT
Set prompt style. 显示类似more命令的百分比
-n ........ --line-numbers
Don't use line numbers.
-N ........ --LINE-NUMBERS
Use line numbers. 显示每行的行号
-o [file] . --log-file=[file]
Copy to log file (standard input only). 将less 输出的内容在指定文件中保存起来
-O [file] . --LOG-FILE=[file]
Copy to log file (unconditionally overwrite).
-p [pattern] --pattern=[pattern]
Start at pattern (from command line).
-P [prompt] --prompt=[prompt]
Define new prompt.
-q -Q .... --quiet --QUIET --silent --SILENT
Quiet the terminal bell.
-r -R .... --raw-control-chars --RAW-CONTROL-CHARS
Output "raw" control characters.
-s ........ --squeeze-blank-lines
Squeeze multiple blank lines. 显示连续空行为一行
-S ........ --chop-long-lines
Chop long lines.
-t [tag] .. --tag=[tag]
Find a tag.
-T [tagsfile] --tag-file=[tagsfile]
Use an alternate tags file.
-u -U .... --underline-special --UNDERLINE-SPECIAL
Change handling of backspaces.
-V ........ --version
Display the version number of "less".
-w ........ --hilite-unread
Highlight first new line after forward-screen.
-W ........ --HILITE-UNREAD
Highlight first new line after any forward movement.
-x [N[,...]] --tabs=[N[,...]]
Set tab stops.
-X ........ --no-init
Don't use termcap init/deinit strings.
--no-keypad
Don't use termcap keypad init/deinit strings.
-y [N] .... --max-forw-scroll=[N]
Forward scroll limit.
-z [N] .... --window=[N]
Set size of window.
-" [c[c]] . --quotes=[c[c]]
Set shell quote characters.
-~ ........ --tilde
Don't display tildes after end of file.
-# [N] .... --shift=[N]
Horizontal scroll amount (0 = one half screen width)
常用的操作有:
e 下移一行;y 上移一行
f 下移一屏;b 回退一屏
/pattern 使用一个模式进行搜索,并定位到下一个匹配的文本
?pattern 使用模式进行搜索,并定位到前一个匹配的文本
n 查找下一个匹配的文本
N 查找上一个匹配的文本
g 跳到第一行
G 跳到最后一行
示例:
1.查看文件:[root@oracledb log]# less boot.log
2.查看命令历史使用记录并通过less分页显示:
[root@oracledb ~]# history|less
1 yum update
2 vi /etc/sysconfig/network
3 vi /etc/sysconfig/network-scripts/ifcfg-eth0
4 service network restart
5 ipconfig
6 ifconfig
7 clear
8 yum update
9 clear
10 yum update
3.less -s file 逐页显示 testfile 之档案内容,如有连续两行以上空白行则以一行空白行显示。:
[root@oracledb study]# less
-s test.log
2016
2017
2018前面有两空行
4.根据模式查找字符串,输入斜杠 /模式字符串 回车进行查找,n和N控制向上和向下查找
# Mandatory: no
# Range: 60-3600
# 上面为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.
5.less 版 tail –f 动态查看日志文件
使用 less file-name 打开日志文件,按 F 可以实现类似 tail -f 的效果。按ctrl+c退出。
[root@oracledb ~]# less log.txt
按F,出现 Waiting for data... (interrupt to abort)
2014
2015
2016
2017
Waiting
for data... (interrupt to abort)
打开新的窗口更改文件内容,使用命令[root@oracledb ~]# echo "2018" >> log.txt
,然后less输出的内容会变成
2014
2015
2016
2017
2018
Waiting for data... (interrupt to abort)