了解为什么'少'是更有效的命令有效的文件导航

更多的是用于在控制台显示文件中的内容在* nix命令行。 多个命令的基本用法是运行对一个文件的命令,如下所示:

另请阅读: 了解“猫”和“TAC”之间差异的命令及示例

学习Linux“更多”命令

# more /var/log/auth.log
查看auth.log文件的内容
Apr 12 11:50:01 youcl CRON[6932]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr 12 11:50:01 youcl CRON[6932]: pam_unix(cron:session): session closed for user root
Apr 12 11:55:01 youcl CRON[7159]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr 12 11:55:01 youcl CRON[7160]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr 12 11:55:01 youcl CRON[7160]: pam_unix(cron:session): session closed for user root
Apr 12 11:55:02 youcl CRON[7159]: pam_unix(cron:session): session closed for user root
Apr 12 12:00:01 youcl CRON[7290]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr 12 12:00:01 youcl CRON[7290]: pam_unix(cron:session): session closed for user root
Apr 12 12:05:01 youcl CRON[7435]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr 12 12:05:01 youcl CRON[7436]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr 12 12:05:01 youcl CRON[7436]: pam_unix(cron:session): session closed for user root
Apr 12 12:05:02 youcl CRON[7435]: pam_unix(cron:session): session closed for user root
Apr 12 12:09:01 youcl CRON[7542]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr 12 12:09:01 youcl CRON[7542]: pam_unix(cron:session): session closed for user root
Apr 12 12:10:01 youcl CRON[7577]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr 12 12:10:01 youcl CRON[7577]: pam_unix(cron:session): session closed for user root
Apr 12 12:15:01 youcl CRON[7699]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr 12 12:15:01 youcl CRON[7700]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr 12 12:15:01 youcl CRON[7700]: pam_unix(cron:session): session closed for user root
Apr 12 12:15:01 youcl CRON[7699]: pam_unix(cron:session): session closed for user root
....

与其它命令,例如结合(管)的另一种方法使用命令cat命令 ,如实施例下面介绍了:

# cat /var/log/auth.log | more

查看和导航文件的内容

为了通过线按文件行导航Enter键或Spacebar键可一次浏览一个页面,该页面是在你的终端屏幕大小。 要退出命令只需按q键。

多个命令的一个有用的选项是-number开关,允许您设置行页面应该包含的数量。 作为一个例子显示auth.log文件的页10行:

# more -10 /var/log/auth.log

只显示文件的前10行

此外,您还可以显示使用一个特定的行号开始页+number选项,如下图所示:

# more +14 /var/log/auth.log
仅显示auth.log文件的前14行
Apr 12 12:09:01 youcl CRON[7542]: pam_unix(cron:session): session closed for user root
Apr 12 12:10:01 youcl CRON[7577]: pam_unix(cron:session): session opened for user root by (
uid=0)
Apr 12 12:10:01 youcl CRON[7577]: pam_unix(cron:session): session closed for user root
Apr 12 12:15:01 youcl CRON[7699]: pam_unix(cron:session): session opened for user root by (
uid=0)
Apr 12 12:15:01 youcl CRON[7700]: pam_unix(cron:session): session opened for user root by (
uid=0)
Apr 12 12:15:01 youcl CRON[7700]: pam_unix(cron:session): session closed for user root
Apr 12 12:15:01 youcl CRON[7699]: pam_unix(cron:session): session closed for user root
Apr 12 12:16:01 youcl mate-screensaver-dialog: gkr-pam: unlocked login keyring
Apr 12 12:17:01 youcl CRON[7793]: pam_unix(cron:session): session opened for user root by (
uid=0)
Apr 12 12:17:01 youcl CRON[7793]: pam_unix(cron:session): session closed for user root
Apr 12 12:20:01 youcl CRON[7905]: pam_unix(cron:session): session opened for user root by (
uid=0)
Apr 12 12:20:01 youcl CRON[7905]: pam_unix(cron:session): session closed for user root
Apr 12 12:25:01 youcl CRON[8107]: pam_unix(cron:session): session opened for user root by (
uid=0)
Apr 12 12:25:01 youcl CRON[8108]: pam_unix(cron:session): session opened for user root by (

学习Linux'less'命令

相似, 更少的命令可以查看文件的内容,并通过文件浏览。 之间的主要区别是,less命令更快,因为它不会加载整个文件一次,并允许导航虽然文件中使用向上 / 向下翻页键。

In可以用作对文件发出的独立命令,或者与具有多个Linux命令的管道一起使用,以缩小其屏幕输出,从而允许您滚动浏览结果。

# less /var/log/auth.log
# ls /etc | less

您可以通过压线文件一行导航Enter键。 页面导航可以办理spaceba r键。 页面大小由当前终端屏幕大小表示。 要退出命令类型q键,相同的方式,更多的命令。

less命令的一个有用的功能是使用/字到SEACH选项。 例如,您可以搜索和通过交互指定匹配从日志文件中的所有的sshd消息/sshd字符串。

使用less命令查看文件内容

要显示查看特定行号的文件,请使用以下语法:

# less +5 /var/log/auth.log

如果你需要跟踪每行的数量较少命令中使用-N选项。

# less -N /var/log/daemon.log
在文件中显示每行的编号
      1 Apr 12 11:50:01 youcl CRON[6932]: pam_unix(cron:session): session opened for user root by (uid=0)
2 Apr 12 11:50:01 youcl CRON[6932]: pam_unix(cron:session): session closed for user root
3 Apr 12 11:55:01 youcl CRON[7159]: pam_unix(cron:session): session opened for user root by (uid=0)
4 Apr 12 11:55:01 youcl CRON[7160]: pam_unix(cron:session): session opened for user root by (uid=0)
5 Apr 12 11:55:01 youcl CRON[7160]: pam_unix(cron:session): session closed for user root
6 Apr 12 11:55:02 youcl CRON[7159]: pam_unix(cron:session): session closed for user root
7 Apr 12 12:00:01 youcl CRON[7290]: pam_unix(cron:session): session opened for user root by (uid=0)
8 Apr 12 12:00:01 youcl CRON[7290]: pam_unix(cron:session): session closed for user root
9 Apr 12 12:05:01 youcl CRON[7435]: pam_unix(cron:session): session opened for user root by (uid=0)
10 Apr 12 12:05:01 youcl CRON[7436]: pam_unix(cron:session): session opened for user root by (uid=0)
11 Apr 12 12:05:01 youcl 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 youcl sshd[16666]: Accepted password for youcl from 192.168.0.15 port 41634 ssh2
Apr 12 16:19:39 youcl sshd[16666]: pam_unix(sshd:session): session opened for user youcl by (uid=0)
Apr 12 16:19:39 youcl systemd-logind[954]: New session 1 of user youcl.
Apr 12 16:19:48 youcl sshd[16728]: Received disconnect from 192.168.0.15: 11: disconnected by user
Apr 12 16:19:48 youcl sshd[16666]: pam_unix(sshd:session): session closed for user youcl
Apr 12 16:20:01 youcl CRON[16799]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr 12 16:20:02 youcl CRON[16799]: pam_unix(cron:session): session closed for user root
Apr 12 16:25:01 youcl CRON[17026]: pam_unix(cron:session): session opened for user root by (uid=0)
Apr 12 16:25:01 youcl CRON[17025]: pam_unix(cron:session): session opened for user root by (uid=0)

上面的命令告诉auth.log文件在第一场比赛sshd字符串。

为了自动追加在更短的命令打开一个文件的内容使用Shift+f键组合或语法如下少跑。

# less +F /var/log/syslog

这使得在交互式模式(实况)下运行和在等待将新数据写入文件时即时显示新内容。 这种行为类似于尾-f命令

在用图案组合,可以交互与观看日志文件Shift+f同时匹配关键字击键。 要退出现场模式下只要按下Ctrl+c键。

# less +/CRON /var/log/syslog

无论你决定使用更多更少 ,这是个人的选择,请记住, 即是多有更多的功能。

另请参阅: 管理文件有效利用头部,尾部和猫命令

赞(52) 打赏
未经允许不得转载:优客志 » 系统运维
分享到:

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏