了解为什么“更少”比“更多”命令更快以实现有效的文件导航

More是一个命令,可一次查看一个屏幕(但不能修改)文本文件的内容。它在Unix和类似Unix的系统,DOS,OS / 2和Microsoft Windows上可用。这种程序称为寻呼机。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 -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 /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中结合示例使用“更少”和“更多”命令,我们将提供更多基于Linux的技巧和窍门。继续阅读!