cat 和 less 命令用于查看文件的内容,关于如何编辑文件内容,我们将在后面的章节中介绍。
为什么查看文件内容需要两个不同的命令呢?事实上,这两个命令各有所长, 下面我们就来介绍如何在合适的情况下选择正确的命令。
以 /var/log 目录下的 syslog 文件为例,首先进入该目录
mateo21@mateo21-desktop:~$ cd /var/log
我们在目录结构中提到过,/var 是一个用来存放系统运行产生的文件,日志就属于此类文件,他记录系统运行留下的痕迹。syslog 文件记录了当前主机最近的运行情况。
cat: 一次性输出所有内容
使用 cat 加要显示的文件名作为参数,该命令会一次性将文件的所有内容输出到控制台,以 syslog 为例:
mateo21@mateo21-desktop:/var/log$ cat syslog Nov 14 00:44:23 mateo21-desktop syslogd 1.4.1#21ubuntu3: restart. Nov 14 00:44:23 mateo21-desktop anacron[6725]: Job `cron.daily' terminated Nov 14 00:44:23 mateo21-desktop anacron[6725]: Normal exit (1 job run) Nov 14 00:44:25 mateo21-desktop NetworkManager: <info> eth1: link timed out. Nov 14 00:44:51 mateo21-desktop NetworkManager: <info> eth1: link timed out. Nov 14 00:45:08 mateo21-desktop NetworkManager: <debug> [1194997508.332093] nm_device_802_11_wireless_get_activation_ap(): Forcing AP 'WIFI' Nov 14 00:45:08 mateo21-desktop NetworkManager: <info> User Switch: /org/freedesktop/NetworkManager/Devices/eth1 / WIFI Nov 14 00:45:08 mateo21-desktop NetworkManager: <info> Deactivating device eth1. Nov 14 00:45:08 mateo21-desktop NetworkManager: <info> Activation (eth1): cancelling... Nov 14 00:45:08 mateo21-desktop NetworkManager: <info> Activation (eth1) cancellation handler scheduled... Nov 14 00:45:08 mateo21-desktop NetworkManager: <info> Activation (eth1): waiting for device to cancel activation. Nov 14 00:45:08 mateo21-desktop NetworkManager: <info> Activation (eth1) cancellation handled. Nov 14 00:45:08 mateo21-desktop NetworkManager: <info> Activation (eth1): cancelled. Nov 14 00:45:08 mateo21-desktop NetworkManager: <info> SUP: sending command 'DISABLE_NETWORK 0' Nov 14 00:45:08 mateo21-desktop NetworkManager: <info> SUP: response was 'OK' Nov 14 00:45:08 mateo21-desktop NetworkManager: <info> SUP: sending command 'AP_SCAN 0' Nov 14 00:45:16 mateo21-desktop NetworkManager: nm_act_request_get_ap: assertion `req != NULL' failed Nov 14 00:45:16 mateo21-desktop NetworkManager: nm_act_request_get_stage: assertion `req != NULL' failed Nov 14 00:45:16 mateo21-desktop NetworkManager: <info> eth1: link timed out. Nov 14 00:45:21 mateo21-desktop NetworkManager: nm_act_request_get_ap: assertion `req != NULL' failed Nov 14 00:45:21 mateo21-desktop NetworkManager: ap_is_auth_required: assertion `ap != NULL' failed Nov 14 00:45:21 mateo21-desktop NetworkManager: <info> Activation (eth1/wireless): association took too long (>120s), asking for new key. Nov 14 00:45:21 mateo21-desktop NetworkManager: nm_dbus_get_user_key_for_network assertion `req != NULL' failed Nov 14 00:47:45 mateo21-desktop init: tty4 main process (4517) killed by TERM signal Nov 14 00:47:45 mateo21-desktop init: tty5 main process (4518) killed by TERM signal Nov 14 00:47:45 mateo21-desktop init: tty2 main process (4520) killed by TERM signal Nov 14 00:47:45 mateo21-desktop init: tty3 main process (4522) killed by TERM signal Nov 14 00:47:45 mateo21-desktop init: tty1 main process (4524) killed by TERM signal Nov 14 00:47:45 mateo21-desktop init: tty6 main process (4525) killed by TERM signal Nov 14 00:47:46 mateo21-desktop avahi-daemon[5390]: Got SIGTERM, quitting. Nov 14 00:47:48 mateo21-desktop exiting on signal 15 Nov 14 00:48:42 mateo21-desktop syslogd 1.4.1#21ubuntu3: restart. ...
我们可以看到,当文件内容过多时,会导致溢出屏幕,需要拖动滚动条才能看到文件开头。更有甚者,如果文件开头内容实在太多,由于控制台容量有限,很有可能滚动条拖到同也无法看到文件的开头部分。
由此可见,cat 并不适合显示过大的文件比如日志类文件。cat 是一个很基本的命令,并没有什么特别实用的参数。如果一定要列一个的话,-n 可以用于在显示内容是添加行号,如下图所示
mateo21@mateo21-desktop:/var/log$ cat -n syslog 1 Nov 14 00:44:23 mateo21-desktop syslogd 1.4.1#21ubuntu3: restart. 2 Nov 14 00:44:23 mateo21-desktop anacron[6725]: Job `cron.daily' terminated 3 Nov 14 00:44:23 mateo21-desktop anacron[6725]: Normal exit (1 job run) 4 Nov 14 00:44:25 mateo21-desktop NetworkManager: <info> eth1: link timed out. 5 Nov 14 00:44:51 mateo21-desktop NetworkManager: <info> eth1: link timed out. 6 Nov 14 00:45:08 mateo21-desktop NetworkManager: <debug> [1194997508.332093] ...
less: 分页显示内容
前面说 cat 不适合显示内容过多的文件,比如日志。那我们用什么命令查看日志呢?答案就是 less 了。less 会从头开始一页一页的显示文件内容。与 cat 类似,less 后加文件名作为参数,可以进入阅读模式,其中可以通过空格键向后翻页,而 b 键向前翻页。
注:这里存在一个和 less 相似的命令 more , 区别在于 more 是一个更老的命令,比 less 少一些功能,比如 more 无法向前翻页。所以,一般用 less 代替 more, 这里提到是为了下次看见别人用时能够认识
mateo21@mateo21-desktop:/var/log$ less syslog Nov 14 00:44:23 mateo21-desktop syslogd 1.4.1#21ubuntu3: restart. Nov 14 00:44:23 mateo21-desktop anacron[6725]: Job `cron.daily' terminated Nov 14 00:44:23 mateo21-desktop anacron[6725]: Normal exit (1 job run) Nov 14 00:44:25 mateo21-desktop NetworkManager: <info> eth1: link timed out. Nov 14 00:44:51 mateo21-desktop NetworkManager: <info> eth1: link timed out. Nov 14 00:45:08 mateo21-desktop NetworkManager: <debug> [1194997508.332093] nm_device_802_11_wireless_get_activation_ap(): Forcing AP 'WIFI' Nov 14 00:45:08 mateo21-desktop NetworkManager: <info> User Switch: /org/freedesktop/NetworkManager/Devices/eth1 / WIFI Nov 14 00:45:08 mateo21-desktop NetworkManager: <info> Deactivating device eth1. Nov 14 00:45:08 mateo21-desktop NetworkManager: <info> Activation (eth1): cancelling... Nov 14 00:45:08 mateo21-desktop NetworkManager: <info> Activation (eth1) cancellation handler scheduled... Nov 14 00:45:08 mateo21-desktop NetworkManager: <info> Activation (eth1): waiting for device to cancel activation. Nov 14 00:45:08 mateo21-desktop NetworkManager: <info> Activation (eth1) cancellation handled. Nov 14 00:45:08 mateo21-desktop NetworkManager: <info> Activation (eth1): cancelled. Nov 14 00:45:08 mateo21-desktop NetworkManager: <info> SUP: sending command 'DISABLE_NETWORK 0'
除了前后翻页,less 显示文件后,还有其他一些快捷键用于查看不同页面中的内容
方向键 ↑ ↓ : 用于显示下一行 (也可以用Enter),或向上翻一行 (也可以用 y),也就是逐行添加显示。
d : 向下翻半页
u : 向上翻半页
q : 退出阅读模式
接下来是几个比较高级的快捷查找方式。less 常用于查看服务器日志,而我们查看日志往往是因为服务器出现了一些问题,如果有过类似经验,应该能理解这里快捷方式的重要性。
= :在控制台底部显示你在文档中的位置 (当前页显示了文档中的第x行到第y行和截止目前显示了文档的x%)
h : 显示帮助(这里写的所有快捷方式都来自帮助),进入后按 q 退出
/ :在 / 之后加上想查找的内容,按下回车键,可以在文件中搜索匹配的内容(支持正则表达式,正则表达式之后会有专门的章节)
n :在用 / 进行搜索时,按 n 可以跳到下一个结果
N:与 n 相反,跳到上一个搜索结果