目录
20. shell命令--more
功能说明
more 是 Linux 中的一个常用的命令行工具,用于分页显示文件内容。当文件内容过大,无法一次性在终端屏幕上完全显示时,more 命令允许用户按页查看文件内容。more 命令类似 cat ,不过会以一页一页的形式显示,更方便使用者逐页阅读。而最基本的指令就是按空白键(space)就往下一页显示,按 b 键就会往回(back)一页显示。而且还有搜寻字串的功能(与 vi 相似),使用中的说明文件,请按 h 。
语法格式
more [选项] 文件名
SYNOPSIS
more [options] file [...]
more [-dlfpcsu] [-num] [+/pattern] [+linenum] [fileNames..]
选项说明
- -c:跟 -p 相似,不同的是先显示内容再清除其他旧资料
- -s:当遇到有连续两行以上的空白行,就代换为一行的空白行
- -u:不显示下引号 (根据环境变数 TERM 指定的 terminal 而有所不同)
- +/pattern:在每个文档显示前搜寻该字串(pattern),然后从该字串之后开始显示
- -num:一次显示的行数,也就是定义屏幕显示的行数,默认为 11 行
- -d:提示使用者,在画面下方显示 [Press space to continue, 'q' to quit.] ,如果使用者按错键,则会显示 [Press 'h' for instructions.] 而不是 '哔' 声,提示用户按空格键继续或按 q 键退出(Linux 系统默认)
- -l:取消遇见特殊字元 ^L(送纸字元)时会暂停的功能
- -f:计算行数时,以实际上的行数,而非自动换行过后的行数(有些单行字数太长的会被扩展为两行或两行以上)
- -p:不以卷动的方式显示每一页,而是先清除萤幕后再显示内容
- +num:从第 num 行开始显示
- fileNames:欲显示内容的文档,可为复数个数
实践操作
1. 分页显示指定的文本文件内容,当内容过多时,按空格键查看下一页,按 q 键退出。
more /etc/profile
2. 先进行清屏操作,随后以每次5行内容的格式显示指定的文本文件内容
more -c -5 /etc/profile
3. 分页显示指定的文本文件内容,若遇到连续两行及以上空白行的情况,则以一行空白行显示
more /etc/yum.conf
more -s /etc/yum.conf
4. 从第10行开始,分页显示指定的文本文件内容
cat -n /etc/yum.conf #看一下文件行号
more +10 /etc/yum.conf #从第十行显示
more +13 /etc/yum.conf #从第十三行显示
5. 与管道符 | 结合使用,以便从其他命令的输出中分页查看内容
ls -l /etc/ | more
grep "Started" /var/log/messages | more
命令示例:1. 分页显示指定的文本文件内容,当内容过多时,按空格键查看下一页,按 q 键退出。
more /etc/profile
输出结果:
[root@MineGi ~]# more /etc/profile
# /etc/profile
# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc
# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.
pathmunge () {
case ":${PATH}:" in
*:"$1":*)
;;
*)
if [ "$2" = "after" ] ; then
PATH=$PATH:$1
else
PATH=$1:$PATH
fi
esac
--More--(33%)
...
命令示例:2. 先进行清屏操作,随后以每次5行内容的格式显示指定的文本文件内容
more -c -5 /etc/profile
输出结果:
[root@MineGi ~]#
# /etc/profile
# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc
--More--(6%)
......
命令示例:3. 分页显示指定的文本文件内容,若遇到连续两行及以上空白行的情况,则以一行空白行显示
more /etc/yum.conf
more -s /etc/yum.conf
输出结果:
[root@MineGi ~]# more /etc/yum.conf
[main]
cachedir=/var/cache/yum/$basearch/$releasever
keepcache=0
debuglevel=2
logfile=/var/log/yum.log
exactarch=1
obsoletes=1
gpgcheck=1
plugins=1
installonly_limit=5
bugtracker_url=http://bugs.centos.org/set_project.php?project_id=23&ref=http://bugs.centos.org/bug_r
eport_page.php?category=yum
distroverpkg=centos-release
# This is the default, if you make this bigger yum won't see if the metadata
# is newer on the remote and so you'll "gain" the bandwidth of not having to
# download the new metadata and "pay" for it by yum not having correct
# information.
# It is esp. important, to have correct metadata, for distributions like
# Fedora which don't keep old packages around. If you don't like this checking
--More--(74%)
.....
命令示例:4. 从第10行开始,分页显示指定的文本文件内容
cat -n /etc/yum.conf
more +10 /etc/yum.conf
more +13 /etc/yum.conf
输出结果:
[root@MineGi ~]# cat -n /etc/yum.conf
1 [main]
2 cachedir=/var/cache/yum/$basearch/$releasever
3 keepcache=0
4 debuglevel=2
5 logfile=/var/log/yum.log
6 exactarch=1
7 obsoletes=1
8 gpgcheck=1
9 plugins=1
10 installonly_limit=5
11 bugtracker_url=http://bugs.centos.org/set_project.php?project_id=23&ref=http://bugs.centos.org/bug_report_page.php?category=yum
12 distroverpkg=centos-release
13
14
15 # This is the default, if you make this bigger yum won't see if the metadata
16 # is newer on the remote and so you'll "gain" the bandwidth of not having to
17 # download the new metadata and "pay" for it by yum not having correct
18 # information.
19 # It is esp. important, to have correct metadata, for distributions like
20 # Fedora which don't keep old packages around. If you don't like this checking
21 # interupting your command line usage, it's much better to have something
22 # manually check the metadata once an hour (yum-updatesd will do this).
23 # metadata_expire=90m
24
25 # PUT YOUR REPOS HERE OR IN separate files named file.repo
26 # in /etc/yum.repos.d
[root@MineGi ~]#
[root@MineGi ~]# more +10 /etc/yum.conf
installonly_limit=5
bugtracker_url=http://bugs.centos.org/set_project.php?project_id=23&ref=http://bugs.centos.org/bug_r
eport_page.php?category=yum
distroverpkg=centos-release
# This is the default, if you make this bigger yum won't see if the metadata
# is newer on the remote and so you'll "gain" the bandwidth of not having to
# download the new metadata and "pay" for it by yum not having correct
# information.
# It is esp. important, to have correct metadata, for distributions like
# Fedora which don't keep old packages around. If you don't like this checking
# interupting your command line usage, it's much better to have something
# manually check the metadata once an hour (yum-updatesd will do this).
# metadata_expire=90m
# PUT YOUR REPOS HERE OR IN separate files named file.repo
# in /etc/yum.repos.d
[root@MineGi ~]# more +13 /etc/yum.conf
# This is the default, if you make this bigger yum won't see if the metadata
# is newer on the remote and so you'll "gain" the bandwidth of not having to
# download the new metadata and "pay" for it by yum not having correct
# information.
# It is esp. important, to have correct metadata, for distributions like
# Fedora which don't keep old packages around. If you don't like this checking
# interupting your command line usage, it's much better to have something
# manually check the metadata once an hour (yum-updatesd will do this).
# metadata_expire=90m
# PUT YOUR REPOS HERE OR IN separate files named file.repo
# in /etc/yum.repos.d
[root@MineGi ~]#
命令示例:5. 与管道符 | 结合使用,以便从其他命令的输出中分页查看内容
ls -l /etc/ | more
grep "Started" /var/log/messages | more
输出结果:
[root@MineGi ~]# ls -l /etc/ | more
总用量 1060
-rw-r--r--. 1 root root 16 11月 22 09:57 adjtime
-rw-r--r--. 1 root root 1529 4月 1 2020 aliases
-rw-r--r--. 1 root root 12288 11月 22 09:58 aliases.db
drwxr-xr-x. 2 root root 261 11月 22 10:07 alternatives
-rw-------. 1 root root 541 8月 9 2019 anacrontab
-rw-r--r--. 1 root root 55 8月 8 2019 asound.conf
drwxr-x---. 3 root root 43 11月 22 09:54 audisp
drwxr-x---. 3 root root 83 11月 22 09:58 audit
drwxr-xr-x. 2 root root 46 11月 22 10:07 bash_completion.d
-rw-r--r--. 1 root root 2971 11月 22 10:09 bashrc
drwxr-xr-x. 2 root root 6 10月 2 2020 binfmt.d
-rw-r--r--. 1 root root 37 10月 23 2020 centos-release
-rw-r--r--. 1 root root 51 10月 23 2020 centos-release-upstream
drwxr-xr-x. 2 root root 6 10月 13 2020 chkconfig.d
-rw-r--r--. 1 root root 1108 8月 8 2019 chrony.conf
-rw-r-----. 1 root chrony 481 8月 8 2019 chrony.keys
drwxr-xr-x. 2 root root 36 11月 22 10:07 cron.d
drwxr-xr-x. 2 root root 57 11月 30 2024 cron.daily
-rw-------. 1 root root 0 8月 9 2019 cron.deny
drwxr-xr-x. 2 root root 22 6月 10 2014 cron.hourly
[root@MineGi ~]# grep "Started" /var/log/messages | more
Nov 22 09:58:47 CentOS7 systemd[1]: Started Create list of required static device nodes for the curr
ent kernel.
Nov 22 09:58:47 CentOS7 systemd[1]: Started Create Static Device Nodes in /dev.
Nov 22 09:58:47 CentOS7 systemd[1]: Started Load Kernel Modules.
Nov 22 09:58:47 CentOS7 systemd[1]: Started Apply Kernel Variables.
Nov 22 09:58:47 CentOS7 systemd[1]: Started Journal Service.
Nov 22 09:58:47 CentOS7 systemd: Started Setup Virtual Console.
Nov 22 09:58:47 CentOS7 systemd: Started dracut cmdline hook.
Nov 22 09:58:47 CentOS7 systemd: Started dracut pre-udev hook.
Nov 22 09:58:47 CentOS7 systemd: Started udev Kernel Device Manager.
Nov 22 09:58:47 CentOS7 systemd: Started udev Coldplug all Devices.
Nov 22 09:58:47 CentOS7 systemd: Started Show Plymouth Boot Screen.
Nov 22 09:58:47 CentOS7 systemd: Started Forward Password Requests to Plymouth Directory Watch.
Nov 22 09:58:49 CentOS7 systemd: Started dracut initqueue hook.
Nov 22 09:58:49 CentOS7 systemd: Started File System Check on /dev/mapper/centos-root.
Nov 22 09:58:49 CentOS7 systemd: Started Reload Configuration from the Real Root.
Nov 22 09:58:49 CentOS7 systemd: Started dracut pre-pivot and cleanup hook.
Nov 22 09:58:49 CentOS7 systemd: Started Cleaning Up and Shutting Down Daemons.
Nov 22 09:58:49 CentOS7 systemd: Started Cleanup udevd DB.
Nov 22 09:58:49 CentOS7 systemd: Started Plymouth switch root service.
Nov 22 09:58:49 CentOS7 systemd: Started Read and set NIS domainname from /etc/sysconfig/network.
[root@MineGi ~]#
交互式子命令说明
- Ctrl+B:返回上一屏
- =:输出当前行的行号
- :f:输出文件名和当前行的行号
- Enter:向下 n 行,需要定义。默认为1行
- Ctrl+F:向下滚动一屏
- 空格键:向下滚动一屏
- V:调用 vi 编辑器
- !命令:调用 Shell,并执行命令,比如:ls -a /root/
- q:退出 more
注意事项
- more 命令在查看大文件时可能不如 less 命令灵活,因为 less 允许用户向前和向后滚动浏览文件内容。
- 对于大多数用户来说,less 命令更为常用,因为它提供了更多的功能和选项。但是,more 命令在某些情况下(如简单的文件查看)仍然很有用。
尽管 more 命令在某些方面可能不如 less 命令强大,但它仍然具有一些高级功能和应用场景,使得在某些情况下它仍然是一个有价值的工具。