测试过程中常用的linux命令之【查看文件指定行的内容】
发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,在开展测试工作的过程中,通常要接触到服务器,对于linux服务器,总结一些常用的命令。准备工作为了能直观展示命令结果,使用脚本创建一个文件,在显示文件内容的同时,也直观的显示行号。#!/bin/bas
千家信息网最后更新 2026年11月03日测试过程中常用的linux命令之【查看文件指定行的内容】
在开展测试工作的过程中,通常要接触到服务器,对于linux服务器,总结一些常用的命令。
准备工作
为了能直观展示命令结果,使用脚本创建一个文件,在显示文件内容的同时,也直观的显示行号。
#!/bin/bashFileName=TestFile.logtouch ./$FileNamei=1while [ $i -le $1 ]do echo "the line number is $i" >> $FileName let "i=$i+1"done
| 命令 | 选项 | 示例 |
| head | -n,显示行数 | [root@Durian scripts]# head -n 5 TestFile.log the line number is 1the line number is 2the line number is 3the line number is 4the line number is 5 #显示文件的前5行 |
[root@Durian scripts]# head -n -6 TestFile.log the line number is 1the line number is 2the line number is 3the line number is 4the line number is 5the line number is 6the line number is 7the line number is 8the line number is 9the line number is 10the line number is 11the line number is 12the line number is 13the line number is 14 #截去后6行,显示剩余内容 | ||
[root@Durian scripts]# head TestFile.log the line number is 1the line number is 2the line number is 3the line number is 4the line number is 5the line number is 6the line number is 7the line number is 8the line number is 9the line number is 10 #当没有选项参数时,默认显示前10行 | ||
| -v ,在首行打印文件名称 | [root@Durian scripts]# head -n 5 -v TestFile.log ==> TestFile.log <==the line number is 1the line number is 2the line number is 3the line number is 4the line number is 5 #在首行打印文件名称 | |
| tail | -n,显示行数 | [root@Durian scripts]# tail -n 4 TestFile.log the line number is 17the line number is 18the line number is 19the line number is 20 #查看messages文件的最后4行内容 |
[root@Durian scripts]# tail -n +5 TestFile.log the line number is 5the line number is 6the line number is 7the line number is 8the line number is 9the line number is 10the line number is 11the line number is 12the line number is 13the line number is 14the line number is 15the line number is 16the line number is 17the line number is 18the line number is 19the line number is 20 #截去前4行,显示剩余内容 | ||
[root@Durian scripts]# tail TestFile.log the line number is 11the line number is 12the line number is 13the line number is 14the line number is 15the line number is 16the line number is 17the line number is 18the line number is 19the line number is 20 #当没有选项参数时,默认显示最后10行 | ||
| -f | tail -f /var/log/messages #当文件内容有更新时,动态的显示最新的内容 | |
| -v ,在首行打印文件名称 | [root@Durian scripts]# tail -v -n 3 TestFile.log ==> TestFile.log <==the line number is 18the line number is 19the line number is 20 #在首行打印文件名称 | |
[root@Durian scripts]# head -n 15 TestFile.log |tail -n 5the line number is 11the line number is 12the line number is 13the line number is 14the line number is 15 #查看中间行第11~15行的内容 | ||
| sed | -n,与p一起使用 | [root@Durian scripts]# sed -n '10p' TestFile.log the line number is 10#查看第10行的内容 |
[root@Durian scripts]# sed -n '11,14p' TestFile.log the line number is 11the line number is 12the line number is 13the line number is 14 #查看第11~14行的内容 | ||
| vi | [root@Durian scripts]# vi +12 TestFile.log #文件打开后,光标直接定位在第12行 | |
| more | [root@Durian scripts]# more -4 TestFile.log the line number is 1the line number is 2the line number is 3the line number is 4--More--(19%) #显示前4行 | |
[root@Durian scripts]# more +4 TestFile.log the line number is 4the line number is 5the line number is 6the line number is 7the line number is 8the line number is 9the line number is 10the line number is 11the line number is 12the line number is 13the line number is 14the line number is 15the line number is 16the line number is 17the line number is 18the line number is 19the line number is 20 #截去前3行,显示剩余内容 | ||
| less | [root@Durian scripts]# less +4 TestFile.lo #截去前3行,显示剩余内容 |
内容
文件
文件名
剩余
命令
直观
参数
服务器
工作
服务
常用
过程
测试
光标
动态
同时
示例
结果
脚本
行号
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网科技园
今日互联网科技
互联网科技卖菜
中资互联网科技
紫鲸互联网科技
互联网金融科技创新
互联网科技论坛
互联网科技汛
互联网科技素材视频竖屏
科技赋能互联网健康
恒银科技与互联网金融
全球顶尖互联网科技公司
互联网公司科技股
世界互联网科技公司市值排名
佛山市翊博互联网络科技有限公司
朱啸虎曾投资哪些互联网科技企业
上海京穆互联网科技有限公司
广东工业互联网大赛深圳学泰科技
从那以后互联网科技有限公司
前海云享互联网科技
花魁咖啡
互联网金融 金融科技
美国互联网科技
互联网金融科技创新
哈尔滨新华互联网科技学院
互联网金融科技公司干啥的
互联网还是高科技吗
互联网科技革命什么开始
福建互联网展示科技馆设计
陕西彩京云互联网科技有限公司