一 tail命令 -- 查看某个文件的尾部
linux tail命令用途是依照要求将指定的文件的最后部分输出到标准设备,通常是终端,通俗来讲,就是把某个文档文件的最后几行显示在终端上,假设该文档文件有更新,tail会自己主动刷新,确保你看到最新的档案内容。
tail命令用来看文件的结尾。
tail 命令语法:
tail -f filename
说明:监视filename文件的尾部内容(默认10行,相当于增加参数 -n 10,即 tail -f -n 10 filename),刷新显示在屏幕上。退出,按下Ctrl+C。---- 随着文件新增,持续输出新增的内容,一般用来看实时日志。
tail -n 20 filename
说明:显示filename最后20行。 -n 指定输出多少行。
tail -1 filename 显示filename文档的 最后1行内容
tail -5 filename 显示filename文档的 最后5行内容
[root@localhost log]# tail -n 20 websender.log 显示文档的最后20行内容,会自动退出。
[root@localhost log]# tail -f -n 20 websender.log 显示文档的最后20行内容,文档有更新,自动刷新,按Ctrl+C 退出。
[root@localhost log]# tail -n 20 -f websender.log 显示文档的最后20行内容,文档有更新,自动刷新,按Ctrl+C 退出。
二 head命令 -- 输出文件的头部内容
head命令用来显示文档文件的开头至标准输出中,默认head命令打印文件的开头10行。
[root@localhost log]# head websender.log 显示文档的开头10行,会自动退出。
[root@localhost log]# head -n 5 websender.log 显示文档的开头5行,会自动退出。
输出前3行
[root@fastdfs linshidir]# head -n 3 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@fastdfs linshidir]#
[root@fastdfs linshidir]# head -n3 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@fastdfs linshidir]#
输出前8个字符
[root@fastdfs linshidir]# head -c 8 /etc/passwd
root:x:0[root@fastdfs linshidir]#
[root@fastdfs linshidir]# head -c8 /etc/passwd
root:x:0[root@fastdfs linshidir]#
输出前8个字符优化写法
root:x:0[root@fastdfs linshidir]# head -c 8 /etc/passwd;echo
root:x:0
[root@fastdfs linshidir]# head -c8 /etc/passwd;echo
root:x:0
[root@fastdfs linshidir]#
三 more命令
more命令 分页显示文件内容,一次显示一屏文本,满屏后停下来,并且在屏幕的底部出现一个提示信息,给出至今已显示的该文件的百分比,方便逐页阅读。
按回车键往下一行显示,按空格键往下一页显示,按b键就会往回(back)一页显示。
[root@localhost log]# more websender.log 一次显示一屏文本
[root@localhost log]# more -5 websender.log 每次显示5行,不清屏
[root@localhost log]# more -p -5 websender.log 每次显示5行,清屏显示,eg:
四 cat命令
从第一行开始显示文件内容
我都是用这个命令查看配置文件,或者是日志文件,但是有点需要注意,cat命令会把整个文件输出到终端,如果文件内容非常多,建议使用grep进行过滤,或者直接用less或more命令。
cat命令用法:
cat filename
五 less命令
与more相似,但支持向前翻页
less命令用法:
less filename
六 grep 命令
grep命令非常常用,尤其是在问题排查的时候,需要用grep从大量的数据中筛选出一些我想要的。grep 也支持正则表达式匹配。
grep命令基本用法:
grep "abc" file 从file中筛选出包含 abc 的行
xxx | grep “abc” 和管道配合使用,从前一个命令的结果中,筛选出包含abc的行