Linux more與less命令

一、命令簡介

more (more) 該命令一次顯示一屏文本,滿屏後停下來,而且在屏幕的底部出現一個提示信息,給出至今己顯示的該文件的百分比,方便逐頁閱讀(file perusal filter for crt viewing) 。more名單中內置了若干快捷鍵,按空白鍵(space)就往下一頁顯示,按 b 鍵就會往回(back)一頁顯示,並且還有搜尋字串的功能(與 vi 類似),使用中按h能夠查看說明文件 。linux

二、用法

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]*
<return>                   Display next k lines of text [1]*
d or ctrl-D               Scroll k lines [current scroll size, initially 11]*
q or Q or <interrupt>   退出more
s                       Skip forward k lines of text [1]
f                       Skip forward k screenfuls of text [1]
b or ctrl-B             返回上一屏
'                       跳轉到上次查找開始的位置
=                       輸出當前行的行號
/ 

  模式                      查找符合模式的內容,按n爲下一個,N爲上一個
n                       Search for kth occurrence of last r.e [1]
    !命令 or :! 命令             調用Shell,並執行命令    
  v                       調用vi 

  ctrl-L                  Redraw screen 
    
  :n                      Go to kth next file [1] 
    
  :p                      Go to kth previous file [1] 
    
  :f                      輸出文件名和當前行的行號 

  .                       執行上一個命令 

四、示例

一、顯示文件file的內容,但在顯示以前先清屏,而且在屏幕的最下方顯示完核的百分比。
[root@oracledb log]# more -dc boot.log
二、每次顯示5行
[root@oracledb log]# more -5 boot.log 
        Welcome to CentOS 
Starting udev:                                             [  OK  ]
Setting hostname oracledb:                                 [  OK  ]
Setting up Logical Volume Management:                      [  OK  ]
Checking filesystems

  --More--(9%)

按空格鍵顯示5-10行,不清屏oracle

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

  --More--(21%)
三、每次顯示5行,清屏顯示
[root@oracledb log]# more -p -5 boot.log 
        Welcome to CentOS 
Starting udev:                                             [  OK  ]
Setting hostname oracledb:                                 [  OK  ]
Setting up Logical Volume Management:                      [  OK  ]
Checking filesystems
--More--(9%)

按空格less

/dev/sda3: clean, 167181/6152192 files, 4828976/24590080 blocks
/dev/sda1: clean, 51/51200 files, 103880/204800 blocks
                                                           [  OK  ]
Remounting root filesystem in read-write mode:             [  OK  ]
Mounting local filesystems:                                [  OK  ]
--More--(21%)
四、more +10 filename 從第 10 行開始顯示 file之檔案內容。
[root@oracledb log]# more +10 boot.log 
Mounting local filesystems:                                [  OK  ]
Enabling local filesystem quotas:                          [  OK  ]
Enabling /etc/fstab swaps:                                 [  OK  ]
五、more -s file 逐頁顯示 testfile 之檔案內容,若有連續兩行以上空白行則以一行空白行顯示。
[root@oracledb study]# more -s test.log 
2016
2017

2018前面有兩空行
六、more進行中!執行命令
!ifconfig
eth5      Link encap:Ethernet  HWaddr 08:00:27:60:11:C4  
          inet addr:172.17.22.31  Bcast:172.17.23.255  Mask:255.255.254.0
          inet6 addr: fe80::a00:27ff:fe60:11c4/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:13261006 errors:0 dropped:0 overruns:0 frame:0
          TX packets:55082 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:941658780 (898.0 MiB)  TX bytes:5059734 (4.8 MiB)

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

------------------------
--More--(9%)
七、根據模式查找字符串,輸入斜槓 /模式字符串 回車進行查找,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.

 

原文

less命令 與 more命令 很是相似,但less命令 能夠更加隨意地瀏覽文件,並且 less 在查看以前不會加載整個文件。oop

相比較於more命令,不少人更喜歡使用 less命令。ui

1、Linux less命令語法

less [參數] 文件

less命令很是強大,在此只介紹幾個經常使用的參數,更多參數使用man less來查看Linux幫助手冊。spa

-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]: 向上翻動一頁

2、Linux less命令使用實例

1)查看文件.net

$less linuxdaxue.com-access_log
120.26.216.168 - - [12/Jun/2016:03:09:16 +0800] "HEAD / HTTP/1.1" 200 -
42.96.189.63 - - [12/Jun/2016:03:09:41 +0800] "HEAD / HTTP/1.1" 200 -
123.56.138.37 - - [12/Jun/2016:03:12:52 +0800] "HEAD / HTTP/1.1" 200 -
120.26.216.168 - - [12/Jun/2016:03:14:16 +0800] "HEAD / HTTP/1.1" 200 -
42.96.189.63 - - [12/Jun/2016:03:14:41 +0800] "HEAD / HTTP/1.1" 200 -
linuxdaxue.com-access_log

2)一次查看多個文件code

#less linuxdaxue.com-access_log linuxdaxue.com-error_log
120.26.216.168 - - [12/Jun/2016:03:09:16 +0800] "HEAD / HTTP/1.1" 200 -
42.96.189.63 - - [12/Jun/2016:03:09:41 +0800] "HEAD / HTTP/1.1" 200 -
123.56.138.37 - - [12/Jun/2016:03:12:52 +0800] "HEAD / HTTP/1.1" 200 -
120.26.216.168 - - [12/Jun/2016:03:14:16 +0800] "HEAD / HTTP/1.1" 200 -
42.96.189.63 - - [12/Jun/2016:03:14:41 +0800] "HEAD / HTTP/1.1" 200 -
123.56.138.37 - - [12/Jun/2016:03:17:52 +0800] "HEAD / HTTP/1.1" 200 -
linuxdaxue.com-access_log (file 1 of 2)

說明:
輸入 :n後,切換到linuxdaxue.com-error_log 
輸入 :p 後,切換到linuxdaxue.com-access_logserver

3、Linux less命令經常使用操做命令

接下來給你們介紹一些在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 處
相關文章
相關標籤/搜索