如果你已经习惯了在Linux系统上工作,那你一定会发现在Linux世界中存在着大量的文本文件。配置文件和日志文件通常都采用文本格式。这些文件通常都有很长的内容,不能在一屏内全部显示。所以在处理这种文件时,我们需要分页显示。这时我们可以用上more命令。

more是干嘛的
more是一个用来分页显示大文本文件的命令,它默认是内置在各个Linux发行版中的。
怎么用more
使用more命令,只需要键入:
$ more file_name
比如,我们想检查/var/log目录下的日志文件syslog,只需要键入:
$ more /var/log/syslog

然后我们可以看到在屏幕左下角有个提示,告诉我们当前的显示比例为0%。看上去这个文件相当大,所以第一页是全部页数的0%。使用空格键可以往下翻页,然后就可以看到提示的百分比会增加。

限制每页显示的行数
在执行more命令的时候,它会占用你终端窗口的全部空间用于显示。但是你可以通过参数-数字来限制每页显示的行数。
例如,你希望每页限制显示12行,可以通过下面的命令:
$ more -12 /var/log/syslog

现在,你会看到每页只显示12行,在按下空格键后,就会翻页显示后面的12行。
Display user message
我们知道,more命令会在显示区域的左下角提示当前内容所占的百分比。对于第一次使用more命令的人来说,他或她可能会想知道怎么才能往下翻页。为了避免这种情况,我们可以在执行时增加-d参数,这样就会额外显示一行用户信息“[按空格键继续,‘q‘推出.]”

如果用户按了‘空格‘或’q‘之外的按键,more会显示一行帮助信息“ [按‘h’键查看提示。]”

如果按下h键,会显示一个帮助信息:

一个有意思的指令是b按钮,b按钮允许你退回到前面的页面。换句话说,b按钮可以允许向前翻页。
你可以通过左上角的…前1页信息来确认当前显示的是前面的页面。

禁止滚动
通过参数-c,more命令不会滚动页面,而是直接清除之前的内容,替换为下一页的内容。 With -c option, more will not scroll the page. It will clear the previous page and put the next page or lines there.
$ more -10 -c lynis.log

如果按下空格键,下一页会仍然有相同的大小。

忽略多余的空白行
使用参数-s来忽略多余的空白行,下面是个例子:

当我们增加参数-s后:
$ more -s doc_30.txt

查找字符串
如果你的日志文件非常大,那么在其中查找你想要的字符串并不是那么容易。more命令的查找功能可以帮你,通过参数+/string可以用来搜索字符串,找到的关键字会显示在第一行。比如我们要在/var/logs/syslog文件中搜索“dhclient”,那more命令的格式:
$ more +/dhclient /var/log/syslog

然后,如果要在文件中继续搜索下一个,只需要按下/按钮,后面跟有关键字dhclient。
从指定行开始显示
你也可以通过参数+数字来指定开始显示的行,例如,我们有一个15行的文件:

然后我们想从第5行开始显示这个文件的内容,命令看起来会是这个样子:
$ more +5 doc_30.txt

可以显示二进制文件吗?
答案是不行. more命令会提示这样的信息,例如:

结论
more是一个用来查看文本文件的基础工具,它不能用来显示二进制文件。通常,我们可以通过命令man more或者more –help来获取更多关于more怎么使用的信息。
发表回复