Linux命令學習:Linux中more和less命令以及查詢用法
**
一、more命令
**
more功能類似 cat ,cat命令是整個檔案的內容從上到下顯示在螢幕上。 more會以一頁一頁的顯示方便使用者逐頁閱讀,而最基本的指令就是按空白鍵(space)就往下一頁顯示,按 b 鍵就會往回(back)一頁顯示,而且還有搜尋字串的功能 。more命令從前向後讀取檔案,因此在啟動時就載入整個檔案。
1.命令格式:
more [-dlfpcsu ] [-num ] [+/ pattern] [+ linenum] [file … ]
2.命令功能:
more命令和cat的功能一樣都是檢視檔案裡的內容,但有所不同的是more可以按頁來檢視檔案的內容,還支援直接跳轉行等功能。
3.命令引數:
+n 從笫n行開始顯示
-n 定義螢幕大小為n行
+/pattern 在每個檔案顯示前搜尋該字串(pattern),然後從該字串前兩行之後開始顯示
-c 從頂部清屏,然後顯示
-d 提示“Press space to continue,’q’ to quit(按空格鍵繼續,按q鍵退出)”,禁用響鈴功能
-l 忽略Ctrl+l(換頁)字元
-p 通過清除視窗而不是滾屏來對檔案進行換頁,與-c選項相似
-s 把連續的多個空行顯示為一行
-u 把檔案內容中的下畫線去掉
4.常用操作命令:
Enter 向下n行,需要定義。預設為1行
Ctrl+F 向下滾動一屏
空格鍵 向下滾動一屏
Ctrl+B 返回上一屏
= 輸出當前行的行號
:f 輸出檔名和當前行的行號
V 呼叫vi編輯器
!命令 呼叫Shell,並執行命令
q 退出more
5.命令例項:
例項1:顯示檔案中從第3行起的內容
命令:
[email protected]:~/ALM$ cat test.log #顯示所有日誌內容
[email protected]:~/ALM$ more +3 test.log #從第三行開始顯示日誌內容
輸出:
複製程式碼
1 [email protected]:~/ALM$ cat test.log
2 2018-8-1 aijian.shi
3 2018-8-2 yafang.wei
4 2018-8-3 hong.zhan
5 2018-8-4 yuyan.zhang
6 2018-8-5 senlin.zhao
7 2018-8-6 yanbin.liu
8 2018-8-7 hui.liu
9 2018-8-8 yanhua.liu
10 2018-8-9 baoixn.cui
11 2018-8-10 ge.song
12 2018-8-11 zhongjun.zhen
13 2018-8-12 qiu.liao
2018-8-3 hong.zhan
2018-8-4 yuyan.zhang
2018-8-5 senlin.zhao
2018-8-6 yanbin.liu
2018-8-7 hui.liu
2018-8-8 yanhua.liu
2018-8-9 baoixn.cui
2018-8-10 ge.song
2018-8-11 zhongjun.zhen
2018-8-12 qiu.lia
複製程式碼
例項2.將日誌內容設定為每屏顯示4行
命令:
[email protected]:~/ALM$ more -4 test.log
輸出:
複製程式碼
1 [email protected]:~/ALM$ more -4 test.log
2 2018-8-1 aijian.shi
3 2018-8-2 yafang.wei
4 2018-8-3 hong.zhan
5 2018-8-4 yuyan.zhang
6
7 …skipping one line #這裡使用ctrl+F或者空格鍵來滾屏
8 2018-8-6 yanbin.liu
9 2018-8-7 hui.liu
10 2018-8-8 yanhua.liu
11 2018-8-9 baoixn.cui
12
13 …skipping one line
14 2018-8-11 zhongjun.zhen
15 2018-8-12 qiu.liao
複製程式碼
例項3.從檔案中查詢第一個出現"liu"字串的行,並從該處前兩行開始顯示輸出
命令:
[email protected]:~/ALM$ more +/liu test.log
輸出:
複製程式碼
1 [email protected]:~/ALM$ more +/liu test.log
2
3 …skipping
4 2018-8-4 yuyan.zhang
5 2018-8-5 senlin.zhao
6 2018-8-6 yanbin.liu
7 2018-8-7 hui.liu
8 2018-8-8 yanhua.liu
9 2018-8-9 baoixn.cui
10 2018-8-10 ge.song
11 2018-8-11 zhongjun.zhen
12 2018-8-12 qiu.liao
複製程式碼
例項4.當一個目錄下的檔案內容太多,可以用more來分頁顯示。這得和管道 | 結合起來
命令:
[email protected]:~/ALM$ cat test.log | more -5 #“|”表示管道,作用是可以將前面命令的輸出當做後面命令的輸入
輸出:
複製程式碼
1 [email protected]:~/ALM$ cat test.log | more -5
2 2018-8-1 aijian.shi
3 2018-8-2 yafang.wei
4 2018-8-3 hong.zhan
5 2018-8-4 yuyan.zhang
6 2018-8-5 senlin.zhao
7
8 …skipping one line
9 2018-8-7 hui.liu
10 2018-8-8 yanhua.liu
11 2018-8-9 baoixn.cui
12 2018-8-10 ge.song
13 2018-8-11 zhongjun.zhen
14 --more–
複製程式碼
二、less指令
less 工具也是對檔案或其它輸出進行分頁顯示的工具,應該說是linux正統檢視檔案內容的工具,功能極其強大。less 的用法比起 more 更加的有彈性。 在 more 的時候,我們並沒有辦法向前面翻, 只能往後面看,但若使用了 less 時,就可以使用 [pageup] [pagedown] 等按 鍵的功能來往前往後翻看檔案,更容易用來檢視一個檔案的內容!除此之外,在 less 裡頭可以擁有更多的搜尋功能,不止可以向下搜,也可以向上搜。
1.命令格式:
less [引數] 檔案
2.命令功能:
less 與 more 類似,但使用 less 可以隨意瀏覽檔案,而 more 僅能向前移動,卻不能向後移動,而且 less 在檢視之前不會載入整個檔案。
3.命令引數:
-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]: 向上翻動一頁
4.使用例項:
例項1:ps檢視程序資訊並通過less分頁顯示同時顯示行號
命令:
[email protected]:~/ALM$ ps -ef|less -N
輸出:
複製程式碼
1 1 UID PID PPID C STIME TTY TIME CMD
2 2 root 1 0 0 Aug08 ? 00:00:00 /sbin/init
3 3 root 2 0 0 Aug08 ? 00:00:00 [kthreadd]
4 4 root 3 2 0 Aug08 ? 00:00:02 [ksoftirqd/0]
5 5 root 6 2 0 Aug08 ? 00:00:00 [migration/0]
6 6 root 7 2 0 Aug08 ? 00:00:00 [watchdog/0]
7 7 root 8 2 0 Aug08 ? 00:00:00 [migration/1]
8 8 root 9 2 0 Aug08 ? 00:00:00 [kworker/1:0]
9 9 root 10 2 0 Aug08 ? 00:00:01 [ksoftirqd/1]
10 10 root 11 2 0 Aug08 ? 00:00:00 [watchdog/1]
11 11 root 12 2 0 Aug08 ? 00:00:00 [migration/2]
12 12 root 14 2 0 Aug08 ? 00:00:01 [ksoftirqd/2]
13 13 root 15 2 0 Aug08 ? 00:00:00 [watchdog/2]
14 14 root 16 2 0 Aug08 ? 00:00:00 [migration/3]
15 15 root 18 2 0 Aug08 ? 00:00:01 [ksoftirqd/3]
16 16 root 19 2 0 Aug08 ? 00:00:00 [watchdog/3]
17 17 root 20 2 0 Aug08 ? 00:00:00 [migration/4]
18 18 root 22 2 0 Aug08 ? 00:00:00 [ksoftirqd/4]
19 19 root 23 2 0 Aug08 ? 00:00:00 [watchdog/4]
20 20 root 24 2 0 Aug08 ? 00:00:00 [migration/5]
21 21 root 26 2 0 Aug08 ? 00:00:00 [ksoftirqd/5]
22 22 root 27 2 0 Aug08 ? 00:00:00 [watchdog/5]
23 23 root 28 2 0 Aug08 ? 00:00:00 [migration/6]
24 :
複製程式碼
例項2.瀏覽多個檔案
命令:
[email protected]:~/ALM$ less test2.log test.log
輸出:
複製程式碼
1 1 ifconfig
2 2 ping www.baidu.com
3 3 ifconfig
4 4 //10.128.161.108/share
5 5 10.128.161.108/share
6 6 ssh
7 7 keygen
8 8 trsa
9 9 ssh
10 10 .ssh/
11
12
13 test2.log (file 1 of 2) (END) - Next: test.log
複製程式碼
說明:
輸入 :n後,切換到 test.log
輸入 :p 後,切換到test2.log
ps:當正在瀏覽一個檔案時,也可以使用 :e命令 開啟另一個檔案。
命令:
less file1
:e file2
5.附加備註
1.全屏導航
ctrl + F - 向前移動一屏
ctrl + B - 向後移動一屏
ctrl + D - 向前移動半屏
ctrl + U - 向後移動半屏
2.單行導航
j - 向前移動一行
k - 向後移動一行
3.其它導航
G - 移動到最後一行
g - 移動到第一行
q / ZZ - 退出 less 命令
4.其它有用的命令
v - 使用配置的編輯器編輯當前檔案
h - 顯示 less 的幫助文件
&pattern - 僅顯示匹配模式的行,而不是整個檔案
5.標記導航
當使用 less 檢視大檔案時,可以在任何一個位置作標記,可以通過命令導航到標有特定標記的文字位置:
ma - 使用 a 標記文字的當前位置
'a - 導航到標記 a 處
6.查詢
more, less 都具備查詢功能,按/ 然後輸入要找的字串,再按 Enter 即可,按 n(next) 會繼續找,大寫的 N 則是往回(上)找,按 q(quit)或者ZZ離開