學習為什麼“less”命令比“more”命令更快更有效地進行檔案導航


More 命令用於檢視(但不能修改)文字檔案的內容,每次一屏。它在 Unix 和類 Unix 系統、DOS、OS/2 和 Microsoft Windows 上可用。這類程式稱為分頁器。More 是一個非常基本的分頁器,最初只允許向前瀏覽檔案,儘管較新的實現允許有限的向後移動。

more 命令的基本用法如下:

$ more /var/log/dkpg.log

示例輸出如下:

2016-12-02 11:30:45 startup archives unpack
2016-12-02 11:30:45 install python-ptyprocess:all <none> 0.5-1
2016-12-02 11:30:45 status half-installed python-ptyprocess:all 0.5-1
2016-12-02 11:30:45 status unpacked python-ptyprocess:all 0.5-1
2016-12-02 11:30:45 status unpacked python-ptyprocess:all 0.5-1
2016-12-02 11:30:46 install python-pexpect:all <none> 4.0.1-1
2016-12-02 11:30:46 status half-installed python-pexpect:all 4.0.1-1
2016-12-02 11:30:46 status unpacked python-pexpect:all 4.0.1-1
2016-12-02 11:30:46 status unpacked python-pexpect:all 4.0.1-1
2016-12-02 11:30:46 install python-simplegeneric:all <none> 0.8.1-1
2016-12-02 11:30:46 status half-installed python-simplegeneric:all 0.8.1-1
2016-12-02 11:30:46 status unpacked python-simplegeneric:all 0.8.1-1
2016-12-02 11:30:46 status unpacked python-simplegeneric:all 0.8.1-1
2016-12-02 11:30:47 install ipython:all <none> 2.4.1-1
2016-12-02 11:30:47 status half-installed ipython:all 2.4.1-1
2016-12-02 11:30:47 status triggers-pending man-db:amd64 2.7.5-1
2016-12-02 11:30:47 status triggers-pending hicolor-icon-theme:all 0.15-0ubuntu1
2016-12-02 11:30:47 status half-installed ipython:all 2.4.1-1
.........................................................................................

cat 命令如下所示:

$ cat /var/log/boot.log | more

示例輸出如下:

[ OK ] Started File System Check on /dev/disk/by-uuid/8E9A-D511.
Mounting /boot/efi...
[ OK ] Mounted /boot/efi.
[ OK ] Reached target Local File Systems.
Starting Clean up any mess left by 0dns-up...
Starting Set console font and keymap...
Starting LSB: AppArmor initialization...
Starting Tell Plymouth To Write Out Runtime Data...
Starting Create Volatile Files and Directories...
[ OK ] Started Tell Plymouth To Write Out Runtime Data.
[ OK ] Started Clean up any mess left by 0dns-up.
Starting Nameserver information manager...
[ OK ] Started Nameserver information manager.
[ OK ] Started Set console font and keymap.
[ OK ] Created slice system-getty.slice.
[ OK ] Started Create Volatile Files and Directories.
Starting Update UTMP about System Boot/Shutdown...
Starting Network Time Synchronization...
[ OK ] Started Update UTMP about System Boot/Shutdown.
[ OK ] Started Network Time Synchronization.
[ OK ] Reached target System Time Synchronized.
[ OK ] Started LSB: AppArmor initialization.
[ OK ] Reached target System Initialization.

要導航,按 Enter 鍵或空格鍵翻頁。要退出命令,只需按 q 鍵或CTRL+C鍵。要設定每頁包含的行數,請使用以下命令:

$ more -5 /var/log/kern.log

以上命令只顯示前 10 行。示例輸出如下:

Dec 6 12:27:33 khushboo-HP-15-Notebook-PC kernel: [ 2941.623182] nouveau
0000:09:00.0: DRM: resuming kernel object tree…
Dec 6 12:27:33 khushboo-HP-15-Notebook-PC kernel: [ 2941.740892] nouveau
0000:09:00.0: bus: MMIO write of 0000001f FAULT at 6013d4 [ IBUS ]
Dec 6 12:27:33 khushboo-HP-15-Notebook-PC kernel: [ 2941.741404] nouveau 0000:0

要獲取有關more 命令的更多資訊,請使用以下命令

$ more -h

示例輸出如下:

more: unknown option -h
Usage:
more [options] ...
A file perusal filter for CRT viewing.
Options:
-d    display help instead of ringing bell
-f    count logical rather than screen lines
-l    suppress pause after form feed
-c    do not scroll, display text and clean line ends
-p    do not scroll, clean screen and display text
-s    squeeze multiple blank lines into one
-u    suppress underlining
- the number of lines per screenful
+ display file beginning from line number
+/ display file beginning from search string match
-V display version information and exit
For more details see more(1).

學習 Linux ‘less’ 命令

Less 命令允許您檢視檔案的內容並在檔案中導航。less 命令示例如下:

$ less /var/log/dpkg.log

示例輸出如下:

2016-12-02 11:30:48 status half-installed ipython:all 2.4.1-1
2016-12-02 11:30:48 status triggers-pending bamfdaemon:amd64 0.5.3~bzr0+16.04.20
160701-0ubuntu1
2016-12-02 11:30:48 status triggers-pending mime-support:all 3.59ubuntu1
2016-12-02 11:30:48 status unpacked ipython:all 2.4.1-1
2016-12-02 11:30:48 status unpacked ipython:all 2.4.1-1
2016-12-02 11:30:48 upgrade libmysqlclient20:i386 5.7.13-0ubuntu0.16.04.2 5.7.16-0ubuntu0.16.04.1
2016-12-02 11:30:48 status triggers-pending libc-bin:amd64 2.23-0ubuntu3
2016-12-02 11:30:48 status half-configured libmysqlclient20:i386 5.7.13-0ubuntu0.16.04.2
2016-12-02 11:30:48 status unpacked libmysqlclient20:i386 5.7.13-0ubuntu0.16.04.2

按 Enter 鍵,您可以逐行瀏覽檔案。要獲取有關 less 命令的更多資訊,請使用以下命令

$ less --help

示例輸出如下:

SUMMARY OF LESS COMMANDS
Commands marked with * may be preceded by a number, N.
Notes in parentheses indicate the behavior if N is given.
A key preceded by a caret indicates the Ctrl key; thus ^K is ctrl-K.

h H               Display this help.
q :q Q :Q ZZ      Exit.
---------------------------------------------------------------------------
                  MOVING

e ^E j ^N CR      * Forward one line (or N lines).
y ^Y k ^K ^P      * Backward one line (or N lines).
f ^F ^V SPACE     * Forward one window (or N lines).
b ^B ESC-v        * Backward one window (or N lines).
z                 * Forward one window (and set window to N).
w                 * Backward one window (and set window to N).
ESC-SPACE         * Forward one window, but don't stop at end-of-file.
d ^D              * Forward one half-window (and set half-window to N).
u ^U              * Backward one half-window (and set half-window to N).
ESC-) RightArrow  * Left one half screen width (or N positions).

如果您需要查詢每一行的行號,請使用 -N 選項,如下所示:

$ less -N /var/log/dpkg.log

示例輸出如下:

1 2016-12-02 11:30:45 startup archives unpack
2 2016-12-02 11:30:45 install python-ptyprocess:all <none> 0.5-1
3 2016-12-02 11:30:45 status half-installed python-ptyprocess:all 0.5-1
4 2016-12-02 11:30:45 status unpacked python-ptyprocess:all 0.5-1
5 2016-12-02 11:30:45 status unpacked python-ptyprocess:all 0.5-1
6 2016-12-02 11:30:46 install python-pexpect:all <none> 4.0.1-1
7 2016-12-02 11:30:46 status half-installed python-pexpect:all 4.0.1-1
8 2016-12-02 11:30:46 status unpacked python-pexpect:all 4.0.1-1
9 2016-12-02 11:30:46 status unpacked python-pexpect:all 4.0.1-1
10 2016-12-02 11:30:46 install python-simplegeneric:all <none> 0.8.1-1
11 2016-12-02 11:30:46 status half-installed python-simplegeneric:all 0.8.1 11 -1
12 2016-12-02 11:30:46 status unpacked python-simplegeneric:all 0.8.1-1
13 2016-12-02 11:30:46 status unpacked python-simplegeneric:all 0.8.1-1
14 2016-12-02 11:30:47 install ipython:all <none> 2.4.1-1
15 2016-12-02 11:30:47 status half-installed ipython:all 2.4.1-1
16 2016-12-02 11:30:47 status triggers-pending man-db:amd64 2.7.5-1
17 2016-12-02 11:30:47 status triggers-pending hicolor-icon-theme:all 0.15- 17 0ubuntu1
18 2016-12-02 11:30:47 status half-installed ipython:all 2.4.1-1
19 2016-12-02 11:30:47 status triggers-pending gnome-menus:amd64 3.13.3-6ub 19 untu3.1
20 2016-12-02 11:30:47 status triggers-pending desktop-file-utils:amd64 0.2

要退出 less 命令,請使用 q 鍵或 CTRL+C 鍵。要更改此行為並在到達檔案末尾時自動退出檔案,請使用以下命令

閱讀本文後,您將能夠理解如何在 Linux 中使用“less”和“more”命令以及示例,我們將推出更多基於 Linux 的技巧和提示。繼續閱讀!

更新於:2020年1月17日

81 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.