Linux-less命令的使用

 

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)

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章