linux中使用head命令和tail命令查看文件中的指定行
linux-dash
A beautiful web dashboard for Linux
项目地址:https://gitcode.com/gh_mirrors/li/linux-dash

·
在Linux中,最常见读取文件命令是cat、more和less,但若使用tail和head命令,它能更为方便的实现精确读取。
而且每次看日志只想看最后几行执行有没有成功,最好的方式就是:
一、head 命令实例
1、默认,显示开头前10行。
1 | head /etc/passwd |
2、显示开头前k行。(以下3行效果一样)
1 2 3 | head -k /etc/passwd(最常见用法) head -n k /etc/passwd head -n +k /etc/passwd |
3、显示多文件开头前k行,并且不显示文件名的文件头。
1 | head -q -n k file1 file2 file3 |
4、除最后k行外,显示剩余全部内容。
1 | head -n -k /log/syncxxx.log |
二、tail 命令实例
1、默认,显示最后10 行。
1 | tail /etc/passwd |
2、显示最后k行。(以下3行效果一样)
1 2 3 | tail -k etc/passwd (最常见用法) tail -n k /etc/passwd tail -n -k /etc/passwd |
3、显示最后k行,并且不显示文件名的文件头。
1 | tail -q -n k file1 file2 file3 |
4、从开头第k字节处开始输出。
1 | tail -n +k /etc/passwd |
5、参数-f使tail不停地去读最新的内容,因此有实时监视的效果,用Ctrl+c来终止。
1 | tail -f /var/log/messages |
以上只是一些简单实例,若使用sed命令,甚至是awk命令那就更强大了!




A beautiful web dashboard for Linux
最近提交(Master分支:5 个月前 )
186a802e
added ecosystem file for PM2 4 年前
5def40a3
Add host customization support for the NodeJS version 4 年前
更多推荐
所有评论(0)