Linux知识库
Linux基础知识!
Linux格式化硬盘以及格式化分区
linux基础常用知识
linux更换yum源
linux挂载windows SMB共享目录
linux开机启动级别
linux网卡配置
linux配置SMB服务
CentOS 8 firewalld配置
Linux软件管理:RPM软件包与YUM管理
Linux用户与组概念
Linux文件与目录权限基础
Linux计划任务管理
Linux中文本文件内容的查看
Linux基础-服务(service)和systemd管理服务
Vim编辑器基本操作
apt更换国内源
Linux程序服务!
Code-server在线编程服务器部署
Cloudreve网盘搭建
Emby多媒体影音系统搭建
chevereto图床搭建教程
Frp内网穿透服务器搭建
FTP服务器搭建
gitlab服务器搭建
Plex搭建
syncthing同步服务器搭建
Wordpress博客搭建
Typecho博客搭建
zabbix搭建(失败)
Aria2离线下载服务器搭建
KMS激活服务器搭建
CentOS搭建NTP服务
Grafana部署与Zabbix集成,部署开源IT系统监控平台
Linux应用环境搭建配置!
nginx编译安装
PHP8编译安装
Linux 关闭selinux
yum安装redis数据库
宝塔环境无法删除.user.ini
宝塔面板安装
mysql8.0.29编译安装
nginx安装(yum方式)
Linux系统基础知识
Linux命令行基础知识
使用Cerbot申请免费证书
ModSecurity 安装编译与连接nginx
Zabbix6.4.8安装部署
本文档使用 MrDoc 发布
-
+
首页
Linux中文本文件内容的查看
之前我们了解了Linux中的文件有纯文本文件、二进制执行文件、数据文件和链接文件等。今天主要学习文本文件的内容查看。 ## 一、文本文件 用于记录纯文本类型内容的文件,类似于windows中的.txt文件,但是因为在linux中不是以后缀名区分,因此linux中的文本文件可以是各种后缀。 例如程序的配置文件、日志log、文档说明文件等等。 ## 二、文件内容的命令 ### 2.1 cat命令 Linux中常见简单查看文件内容的命令,格式命令:`cat -[AbEnTv] 文件名`,从第一行开始显示文件内容到屏幕上。 ```linux # 默认不加参数显示全部文件内容 [root@localhost ~]# cat test.txt Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 11180/nginx: master tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 772974/sshd tcp6 0 0 :::33060 :::* LISTEN 8828/mysqld tcp6 0 0 :::3306 :::* LISTEN 8828/mysqld tcp6 0 0 :::80 :::* LISTEN 11180/nginx: master tcp6 0 0 :::22 :::* LISTEN 772974/sshd # 输出行号 [root@localhost ~]# cat -n test.txt 1 Active Internet connections (only servers) 2 Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name 3 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 11180/nginx: master 4 tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 772974/sshd 5 tcp6 0 0 :::33060 :::* LISTEN 8828/mysqld 6 tcp6 0 0 :::3306 :::* LISTEN 8828/mysqld 7 tcp6 0 0 :::80 :::* LISTEN 11180/nginx: master 8 tcp6 0 0 :::22 :::* LISTEN 772974/sshd ``` `cat`适用于小文件的快速查看显示。 ### 2.2 tac命令 与`cat`相反,从最后一行开始显示内容。 ```linux [root@localhost ~]# tac test.txt tcp6 0 0 :::22 :::* LISTEN 772974/sshd tcp6 0 0 :::80 :::* LISTEN 11180/nginx: master tcp6 0 0 :::3306 :::* LISTEN 8828/mysqld tcp6 0 0 :::33060 :::* LISTEN 8828/mysqld tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 772974/sshd tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 11180/nginx: master Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name Active Internet connections (only servers) ``` ### 2.3 nl 将输出内容自动加上行号,与`cat -n `类似吧,不过可以设置显示行号的格式。 ```linux [root@localhost ~]# nl test.txt 1 Active Internet connections (only servers) 2 Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name 3 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 11180/nginx: master 4 tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 772974/sshd 5 tcp6 0 0 :::33060 :::* LISTEN 8828/mysqld 6 tcp6 0 0 :::3306 :::* LISTEN 8828/mysqld 7 tcp6 0 0 :::80 :::* LISTEN 11180/nginx: master 8 tcp6 0 0 :::22 :::* LISTEN 772974/sshd ``` ### 2.4 more命令 `more`用于一页一页读取大文件使用,支持翻页等查看方式。 ```linux ##################### Grafana Configuration Defaults ##################### # # Do not modify this file in grafana installs # # possible values : production, development app_mode = production # instance name, defaults to HOSTNAME environment variable value or hostname if HOSTNAME var is empty instance_name = ${HOSTNAME} #################################### Paths ############################### [paths] # Path to where grafana can store temp files, sessions, and the sqlite3 db (if that is used) data = data # Temporary files in `data` directory older than given duration will be removed temp_data_lifetime = 24h # Directory where grafana can store logs logs = data/log # Directory where grafana will automatically scan and look for plugins plugins = data/plugins # folder that contains provisioning config files that grafana will apply on startup and while running. provisioning = conf/provisioning #################################### Server ############################## [server] # Protocol (http, https, h2, socket) protocol = http # The ip address to bind to, empty will bind to all interfaces http_addr = --更多--(2%) ``` 使用`more 文件名`查看文件时会在最后一行显示当前查看的百分比,使用快捷键进行翻页和退出。 - 空格键,下一页 - Enter回车键,下一行 - /字符串,向下查找字符串 - :f,立刻显示当前文件名和行数 - q,退出查看 - b,上一页 `more`适用于多行文件的查看。 ### 2.5 less命令 与上面的`more`类似,翻页查看,可以使用`pageup`和`pagedown`键进行上下翻页。 ```linux ##################### Grafana Configuration Defaults ##################### # # Do not modify this file in grafana installs # # possible values : production, development app_mode = production # instance name, defaults to HOSTNAME environment variable value or hostname if HOSTNAME var is empty instance_name = ${HOSTNAME} #################################### Paths ############################### [paths] # Path to where grafana can store temp files, sessions, and the sqlite3 db (if that is used) data = data # Temporary files in `data` directory older than given duration will be removed temp_data_lifetime = 24h # Directory where grafana can store logs logs = data/log # Directory where grafana will automatically scan and look for plugins plugins = data/plugins # folder that contains provisioning config files that grafana will apply on startup and while running. provisioning = conf/provisioning #################################### Server ############################## [server] # Protocol (http, https, h2, socket) protocol = http # The ip address to bind to, empty will bind to all interfaces http_addr = defaults.ini ``` - 空格键,向下翻页 - pageup,向上翻页 - pagedown,向下翻页 - q,退出查看 - /字符串,向下查找 - ?字符串,向上查找 ### 2.6 head命令 当有时候文件太多了我们只想要查看最开始的几行时使用`head`命令。格式为`head [-n number] 文件`,默认显示十行。 ```linux [root@localhost ~]# head defaults.ini ##################### Grafana Configuration Defaults ##################### # # Do not modify this file in grafana installs # # possible values : production, development app_mode = production # instance name, defaults to HOSTNAME environment variable value or hostname if HOSTNAME var is empty instance_name = ${HOSTNAME} # 通过-n 100指定前几行 head -n 100 defaults.ini ``` 这个有时候也是非常方便的查看内容。 ### 2.7 tail命令 `tail`命令查看最末尾的几行,可以用于一些日志文件的查看,因为一般日志文件新的行在末尾,我们可以通过`tail [-n number] 文件`查看最新的日志,结合一些其他参数可以实现实时查看日志。 默认显示末尾十行。参数: - -n ,显示的行数 - -f,持续检测最后的指定行数,可以实现日志的实时刷新查看 ```linux [root@localhost ~]# tail -n 20 defaults.ini interval_day = MM/DD interval_month = YYYY-MM interval_year = YYYY # Experimental feature use_browser_locale = false # Default timezone for user preferences. Options are 'browser' for the browser local timezone or a timezone name from IANA Time Zone database, e.g. 'UTC' or 'Europe/Amsterdam' etc. default_timezone = browser [expressions] # Enable or disable the expressions functionality. enabled = true [geomap] # Set the JSON configuration for the default basemap default_baselayer_config = # Enable or disable loading other base map layers enable_custom_baselayers = true ``` 查看指定的开头和指定末尾的行数使用`head`或`tail`命令。 ## 三、创建新文本文件 使用`touch`创建一个新的文件。 ```linux touch test.txt ``` 总结:小文件查看使用`cat`即可,大文件分页查看可以使用`more`或者`less`,如果是只想要查看文件的开头几行或者末尾几行可以使用`head`或者`tail`,创建文件使用`touch`。
Chuck
2024年1月16日 16:26
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
Markdown文件
分享
链接
类型
密码
更新密码