more是一页一页的显示文件内容,方便使用者逐页阅读。cat命令是整个文件的内容从上到下显示在屏幕上。more最基本的指令就是按空白键(space)就往下一页显示,按 b 键就会往回(back)一页显示,而且还有搜寻字串的功能 。more命令从前向后读取文件,因此在启动时就加载整个文件。

1.命令格式


more [-dlfpcsu ] [-num ] [+/ pattern] [+ linenum] [file ... ]

2.命令功能


more命令和cat的功能一样都是查看文件里的内容,不同的是more按页来查看文件的内容,还支持直接跳转行等功能。

3.命令参数


  • +n 从笫n行开始显示
  • -n 定义一页(一次)大小为n行
  • +/pattern 在每个档案显示前搜寻该字串(pattern),然后从该字串前两行之后开始显示
  • -c 从顶部清屏,然后显示
  • -d 提示“Press space to continue,’q’ to quit(按空格键继续,按q键退出)”,禁用响铃功能
  • -l 忽略Ctrl+l(换页)字符
  • -p 通过清除窗口而不是滚屏来对文件进行换页,与-c选项相似
  • -s 把连续的多个空行显示为一行
  • -u 把文件内容中的下画线去掉

4.常用操作(键盘按键)命令


  • Enter 向下n行,可定义定义。默认为1行
  • Ctrl+F 向下滚动一屏
  • 空格键 向下滚动一屏
  • Ctrl+B 返回上一屏
  • = 输出当前行的行号
  • :f 输出文件名和当前行的行号
  • V 调用vi编辑器
  • ! 命令, 调用Shell,并执行命令
  • q 退出more

5.示例


1显示文件中从第3行起的内容

命令:

more +2 test.txt 

输出:

lighthouse@VM-4-14-ubuntu:~/cat$ cat test.txt 
hello

test nl
lighthouse@VM-4-14-ubuntu:~/cat$ more +2 test.txt 

test nl
lighthouse@VM-4-14-ubuntu:~/cat$ 

2从文件中查找第一个出现”hello3“字符串的行,并从该处前两行开始显示输出

命令:

more +/hello3 text2.txt 

输出:

lighthouse@VM-4-14-ubuntu:~/more$ cat text2.txt 
hello
hello1
hello2
hello3
hello4
hello5
hello6
hello7
hello8
hello9
hello10
lighthouse@VM-4-14-ubuntu:~/more$ more +/hello3 text2.txt 

...skipping
hello1
hello2
hello3
hello4
hello5
hello6
hello7
hello8
hello9
hello10

说明:+/ 后面是匹配的字符串

3设定每屏显示行数

命令:

more -3 text2.txt 

输出:

lighthouse@VM-4-14-ubuntu:~/more$ more -3 text2.txt 
hello
hello1
hello2
--More--(25%)

说明:如上面所示–More–(25%),最下面显示了该屏展示的内容占文件总行数的比例。
按 Ctrl+F 或者 空格键 将会显示下一屏3条内容,百分比也会跟着变化。按enter则会是一条条的继续显示出来。

4列一个目录下的文件,文件太多时,用more分页显示。此时需要和管道 | 结合起来

找dev这个目录来演示效果
命令:

ls -l  | more -5

输出:

lighthouse@VM-4-14-ubuntu:/dev$ ls -l | more -5
total 0
crw-r--r-- 1 root root     10, 235 Jun 29 15:14 autofs
drwxr-xr-x 2 root root         280 Jun 29 15:14 block
drwxr-xr-x 2 root root          60 Jun 29 15:14 bsg
crw-rw---- 1 root disk     10, 234 Jun 29 15:14 btrfs-control
--More--

说明:每页显示5个文件信息,按 Ctrl+F 或者 空格键 将会显示下5条文件信息。其中第一页包括total那一行,所以实际文件条数是4,翻页之后会5条一次这样的追加显示。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐