less(less) 命令可以對文件或其它輸出進行分頁顯示相寇,與moe命令相似,但是比more命令要強大許多。應(yīng)該說是linux正統(tǒng)查看文件內(nèi)容的工具交掏。
用法:less [選項]... [文件]...
下面是less命令的可用選項枫浙,直接照搬文檔了刨肃,部分有解釋:
-? ........ --help 顯示幫助
-a ........ --search-skip-screen
Forward search skips current screen.
-b [N] .... --buffers=[N]
Number of buffers. 設(shè)置緩沖區(qū)的大小
-B ........ --auto-buffers
Don't automatically allocate buffers for pipes.
-c -C .... --clear-screen --CLEAR-SCREEN
Repaint by clearing rather than scrolling. 先清除屏幕后再顯示內(nèi)容
-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. 當文件顯示結(jié)束后古拴,自動離開
-f ........ --force
Force open non-regular files.強迫打開特殊文件,例如外圍設(shè)備代號真友、目錄和二進制文件
-F ........ --quit-if-one-screen
Quit if entire file fits on first screen.
-g ........ --hilite-search
Highlight only last match for searches.不加亮顯示搜索到的所有關(guān)鍵詞黄痪,僅顯示當前顯示的關(guān)鍵字,以提高顯示速度盔然;
-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 輸出的內(nèi)容在指定文件中保存起來
-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. 顯示連續(xù)空行為一行
-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 之檔案內(nèi)容,如有連續(xù)兩行以上空白行則以一行空白行顯示刻帚。:
[root@oracledb study]# less
-s test.log
2016
2017
2018前面有兩空行
4.根據(jù)模式查找字符串潦嘶,輸入斜杠 /模式字符串 回車進行查找,n和N控制向上和向下查找
# Mandatory: no
# Range: 60-3600
# 上面為more的顯示內(nèi)容
/Hostname=Zabbix server
查找的結(jié)果在最上面顯示
...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 動態(tài)查看日志文件
使用 less file-name 打開日志文件崇众,按 F 可以實現(xiàn)類似 tail -f 的效果掂僵。按ctrl+c退出。
[root@oracledb ~]# less log.txt
按F顷歌,出現(xiàn) Waiting for data... (interrupt to abort)
2014
2015
2016
2017
Waiting
for data... (interrupt to abort)
打開新的窗口更改文件內(nèi)容锰蓬,使用命令[root@oracledb ~]# echo "2018" >> log.txt
,然后less輸出的內(nèi)容會變成
2014
2015
2016
2017
2018
Waiting for data... (interrupt to abort)