了解为什么 'less' 比 'more' 命令更快,更有效地进行文件导航
More是一个 *nix 命令行,用于在控制台中显示文件的内容。more 命令的基本用法是对文件运行命令,如下所示:
另请阅读: 通过示例了解“cat”和“tac”命令之间的区别
学习 Linux ‘more’ 命令
# more /var/log/auth.log
Apr 12 11:50:01 example CRON[6932]: pam_unix(cron:session): session opened for user root by (uid=0) Apr 12 11:50:01 example CRON[6932]: pam_unix(cron:session): session closed for user root Apr 12 11:55:01 example CRON[7159]: pam_unix(cron:session): session opened for user root by (uid=0) Apr 12 11:55:01 example CRON[7160]: pam_unix(cron:session): session opened for user root by (uid=0) Apr 12 11:55:01 example CRON[7160]: pam_unix(cron:session): session closed for user root Apr 12 11:55:02 example CRON[7159]: pam_unix(cron:session): session closed for user root Apr 12 12:00:01 example CRON[7290]: pam_unix(cron:session): session opened for user root by (uid=0) Apr 12 12:00:01 example CRON[7290]: pam_unix(cron:session): session closed for user root Apr 12 12:05:01 example CRON[7435]: pam_unix(cron:session): session opened for user root by (uid=0) Apr 12 12:05:01 example CRON[7436]: pam_unix(cron:session): session opened for user root by (uid=0) Apr 12 12:05:01 example CRON[7436]: pam_unix(cron:session): session closed for user root Apr 12 12:05:02 example CRON[7435]: pam_unix(cron:session): session closed for user root Apr 12 12:09:01 example CRON[7542]: pam_unix(cron:session): session opened for user root by (uid=0) Apr 12 12:09:01 example CRON[7542]: pam_unix(cron:session): session closed for user root Apr 12 12:10:01 example CRON[7577]: pam_unix(cron:session): session opened for user root by (uid=0) Apr 12 12:10:01 example CRON[7577]: pam_unix(cron:session): session closed for user root Apr 12 12:15:01 example CRON[7699]: pam_unix(cron:session): session opened for user root by (uid=0) Apr 12 12:15:01 example CRON[7700]: pam_unix(cron:session): session opened for user root by (uid=0) Apr 12 12:15:01 example CRON[7700]: pam_unix(cron:session): session closed for user root Apr 12 12:15:01 example CRON[7699]: pam_unix(cron:session): session closed for user root ....
另一种方法是将more命令与其他命令(例如cat 命令)结合(管道)使用,如下例所示:
# cat /var/log/auth.log | more
为了逐行浏览文件,请按下Enter
键或按下Spacebar
键一次浏览一页,页面大小为您当前的终端屏幕大小。要退出命令,只需按下q
键。
more 命令的一个有用选项是-number
开关,它允许您设置一页应包含的行数。例如,将auth.log
文件显示为一页10
行:
# more -10 /var/log/auth.log
+number
此外,您还可以使用如下所示的选项显示从特定行号开始的页面:
# more +14 /var/log/auth.log
Apr 12 12:09:01 example CRON[7542]: pam_unix(cron:session): session closed for user root Apr 12 12:10:01 example CRON[7577]: pam_unix(cron:session): session opened for user root by ( uid=0) Apr 12 12:10:01 example CRON[7577]: pam_unix(cron:session): session closed for user root Apr 12 12:15:01 example CRON[7699]: pam_unix(cron:session): session opened for user root by ( uid=0) Apr 12 12:15:01 example CRON[7700]: pam_unix(cron:session): session opened for user root by ( uid=0) Apr 12 12:15:01 example CRON[7700]: pam_unix(cron:session): session closed for user root Apr 12 12:15:01 example CRON[7699]: pam_unix(cron:session): session closed for user root Apr 12 12:16:01 example mate-screensaver-dialog: gkr-pam: unlocked login keyring Apr 12 12:17:01 example CRON[7793]: pam_unix(cron:session): session opened for user root by ( uid=0) Apr 12 12:17:01 example CRON[7793]: pam_unix(cron:session): session closed for user root Apr 12 12:20:01 example CRON[7905]: pam_unix(cron:session): session opened for user root by ( uid=0) Apr 12 12:20:01 example CRON[7905]: pam_unix(cron:session): session closed for user root Apr 12 12:25:01 example CRON[8107]: pam_unix(cron:session): session opened for user root by ( uid=0) Apr 12 12:25:01 example CRON[8108]: pam_unix(cron:session): session opened for user root by (
学习 Linux ‘less’ 命令
与more类似,less命令允许您查看文件内容并浏览文件。more和less之间的主要区别在于less命令速度更快,因为它不会一次加载整个文件,而是允许使用上/下翻页键浏览文件。
它可以用作针对文件发出的独立命令,也可以与大量 Linux 命令的管道一起使用,以缩小其屏幕输出,让您可以滚动浏览结果。
# less /var/log/auth.log # ls /etc | less
您可以按下 键逐行浏览文件Enter
。可以使用 r 键进行页面导航spaceba
。页面大小由您当前的终端屏幕大小表示。要退出命令,请键入q
键,方法与 more 命令相同。
less命令的一个有用功能是使用/word-to-seach选项。例如,您可以通过交互方式指定字符串来搜索并匹配日志文件中的所有sshd/sshd
消息。
为了显示特定行号的文件,请使用以下语法:
# less +5 /var/log/auth.log
如果您需要使用less命令来追踪每一行的编号,请使用该-N
选项。
# less -N /var/log/daemon.log
1 Apr 12 11:50:01 example CRON[6932]: pam_unix(cron:session): session opened for user root by (uid=0) 2 Apr 12 11:50:01 example CRON[6932]: pam_unix(cron:session): session closed for user root 3 Apr 12 11:55:01 example CRON[7159]: pam_unix(cron:session): session opened for user root by (uid=0) 4 Apr 12 11:55:01 example CRON[7160]: pam_unix(cron:session): session opened for user root by (uid=0) 5 Apr 12 11:55:01 example CRON[7160]: pam_unix(cron:session): session closed for user root 6 Apr 12 11:55:02 example CRON[7159]: pam_unix(cron:session): session closed for user root 7 Apr 12 12:00:01 example CRON[7290]: pam_unix(cron:session): session opened for user root by (uid=0) 8 Apr 12 12:00:01 example CRON[7290]: pam_unix(cron:session): session closed for user root 9 Apr 12 12:05:01 example CRON[7435]: pam_unix(cron:session): session opened for user root by (uid=0) 10 Apr 12 12:05:01 example CRON[7436]: pam_unix(cron:session): session opened for user root by (uid=0) 11 Apr 12 12:05:01 example CRON[7436]: pam_unix(cron:session): session closed for user root
默认情况下,退出less命令的唯一方法是按下q
键。要更改此行为并在到达文件末尾时自动退出文件,请使用-e
或-E
选项:
# less -e /var/log/auth.log # less -E /var/log/auth.log
要在第一次出现模式时打开文件,请使用以下语法:
# less +/sshd /var/log/auth.log
Apr 12 16:19:39 example sshd[16666]: Accepted password for example from 192.168.0.15 port 41634 ssh2 Apr 12 16:19:39 example sshd[16666]: pam_unix(sshd:session): session opened for user example by (uid=0) Apr 12 16:19:39 example systemd-logind[954]: New session 1 of user example. Apr 12 16:19:48 example sshd[16728]: Received disconnect from 192.168.0.15: 11: disconnected by user Apr 12 16:19:48 example sshd[16666]: pam_unix(sshd:session): session closed for user example Apr 12 16:20:01 example CRON[16799]: pam_unix(cron:session): session opened for user root by (uid=0) Apr 12 16:20:02 example CRON[16799]: pam_unix(cron:session): session closed for user root Apr 12 16:25:01 example CRON[17026]: pam_unix(cron:session): session opened for user root by (uid=0) Apr 12 16:25:01 example CRON[17025]: pam_unix(cron:session): session opened for user root by (uid=0)
上述命令告诉less在第一次匹配字符串时打开auth.logsshd
文件。
为了自动附加在less命令中打开的文件的内容,请使用Shift+f
组合键或使用以下语法运行 less。
# less +F /var/log/syslog
这使得 less 可以在交互模式(实时)下运行,并在等待新数据写入文件时动态显示新内容。此行为类似于tail -f 命令。
结合模式,您可以Shift+f
在匹配关键字的同时通过按键交互地查看日志文件。要退出实时模式,只需Ctrl+c
按键即可。
# less +/CRON /var/log/syslog
无论您决定使用更多还是更少,这都是个人选择,请记住,功能越多,少即是多。