前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Linux命令之less/more

Linux命令之less/more

作者头像
AsiaYe
发布2019-11-06 17:17:59
1.3K0
发布2019-11-06 17:17:59
举报
文章被收录于专栏:DBA随笔DBA随笔
Linux命令之less/more 当我们在Linux中使用cat查看文件的时候,有时候文件的内容过多,会刷满整个页面,不利于我们观看,这种情况下经常会使用到less和more这两个命令,他们的作用就是将文件中的内容一页一页的显示,从而方便我们观看。

01

more命令

首先我们看看more命令,more命令有几个常用的参数,如下:

+n:从第n行开始显示

-n:定义屏幕的大小为n行

-s:将连续的过个空行显示为一行

+/string:从匹配string的那一行开始显示

接下来是测试环节,首先我们使用下面的脚本创建一个aaa.txt的文本文件,里面循环写了一些文字:

代码语言:javascript
复制
#!/bin/bash  

for((i=;i<=;i++));  
do   
echo "this is line $i">>aaa.txt;  
done

然后我们使用more命令打开这个aaa的文件:

代码语言:javascript
复制
[dba_mysql /tmp]$cat aaa.txt|more -20
this is line 
this is line 
this is line 
this is line 
this is line 
this is line 
this is line 
this is line 
this is line 
this is line 
this is line 
this is line 
this is line 
this is line 
this is line 
this is line 
this is line 
this is line 
this is line 
this is line 

我们使用了-20这样的方式,让当前页面只显示20行内容。此时我们按q键退出当前的观察模式。

当我们想要从第90行开始显示的时候,可以使用+90的命令,如下:

代码语言:javascript
复制
[dba_mysql /tmp]$cat aaa.txt|more +
this is line 
this is line 
this is line 
this is line 
this is line 
this is line 
this is line 
this is line 
this is line 
this is line 
this is line 

如果我们想要从包含50这个字符串的那一行开始的话,可以使用如下命令:

代码语言:javascript
复制
[dba_mysql /tmp]$cat aaa.txt|more +/50
this is line 51
this is line 52
this is line 53
this is line 54
this is line 55
this is line 56
this is line 57
this is line 58
this is line 59
this is line 60
this is line 61
this is line 62
this is line 63
this is line 64
this is line 65
this is line 66
this is line 67
this is line 68
this is line 69
this is line 70

当我们的文件中包含多个空行时,如果我们想要将这些空行显示为一行,那么可以使用-s命令,如下:

代码语言:javascript
复制
[dba_mysql /tmp]$cat bbb.txt
aaaa
bbbb


cccc
dddd
[dba_mysql@ /tmp]$cat bbb.txt|more -s
aaaa
bbbb

cccc
dddd

除了常用的这几个命令,还有一些快捷键:

空格:向下翻页

q:退出观察模式

回车:向下滚动一行

b:向上翻页

=:输出当前行的行号

:f:输出当前文件名称和行号

v:调用vim,这个功能一般在找到匹配项之后使用。

除此之外,还有一些其他的参数和快捷键,但是不太用到,所以就没有列出来,有兴趣可以看看。

02

less命令

less命令和more命令比较像,但是less命令的可用性更好一些。如下:

-e 当文件显示结束后,自动离开

-i 忽略搜索时的大小写

-m 显示类似more命令的百分比

-N 显示每行的行号

-o <文件名> 将less 输出的内容在指定文件中保存起来

-Q 不使用警告音

-s 显示连续空行为一行

代码语言:javascript
复制
[dba_mysql /tmp]$less -mN aaa.txt
       this is line 
       this is line 
       this is line 
       this is line 
       this is line 
       this is line 
       this is line 
       this is line 
       this is line 
aaa.txt %

其中-m是显示百分比,-N是显示行号。

一般情况下,less是搭配一些管道运算符一起的,例如:

ls -lrt|less

cat aaa.txt|less

除此之外,还有一些快捷键,其实和vim里面的操作很像,如下:

/字符串:向下搜索"字符串"的功能

?字符串:向上搜索"字符串"的功能

n:重复前一个搜索(与 / 或 ? 有关)

N:反向重复前一个搜索(与 / 或 ? 有关)

d 向后翻半页

u 向前滚动半页

y 向前滚动一行

空格键 滚动一行

ctrl + F - 向前移动一屏

ctrl + B - 向后移动一屏

ctrl + D - 向前移动半屏

ctrl + U - 向后移动半屏

j - 向前移动一行

k - 向后移动一行

G - 移动到最后一行

g - 移动到第一行

q / ZZ - 退出 less 命令

v - 使用配置的编辑器编辑当前文件

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-06-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 DBA随笔 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档