学习为什么“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 次浏览

开启你的职业生涯

完成课程获得认证

开始学习
广告