查看文件内容
1. cat
用法:cat [选项]...[文件]...
选项:
-n 显示行号,包括空白行
-b 显示行号,空白行不显示行号
eg:
[root@qll ~]# cat /etc/passwd
[root@qll ~]# cat -n /etc/passwd
[root@qll ~]# cat -b /etc/passwd
2. more
more命令和cat的功能一样都是查看文件里的内容,但有所不同的是more可以按页来查看文件的内容,还支持直接跳转行等功能。
Enter键:向下n行,需要定义。默认为1行
空格键:向下滚动一屏
Ctrl+B:返回上一屏
= 输出当前行的行号
V 调用vi编辑器
!命令 调用Shell,并执行命令
q 退出more
eg:
[root@qll ~]# more /var/log/messages
3. less
描述:分页查看文件内容,
操作:
空格(下一页)
方向键(上下回翻)
q键(退出查看)
eg:
[root@qll ~]# less /var/log/mingongge.log
4. head
用法:head [选项]...[文件]...
选项:
-c nK 显示文件前nKB的内容。
-n 显示文件前n行的内容。
eg:
[root@qll ~]# head -c 2k /var/log/messages
#查看文件的前2KB的内容
[root@qll ~]# head -n 15 /var/log/messages
#查看文件的前15行的内容
5. tail
用法:tail [选项]...[文件]...
选项:
-c nK 显示文件末尾nKB的内容。
-n 显示文件末尾n行的内容。
-f 动态显示文件内容,常用于查看日志,按 Ctrl+C 组合键退出。
eg:
[root@qll ~]# tail -c 2KB /var/log/messages
#查看文件末尾2KB的内容
[root@qll ~]# tail -n 15 /var/log/messages
#查看文件末尾15行的内容
[root@qll ~]# tail -f /var/log/messages
#实时动态查看文件内容
6. grep
用法:grep [选项] 匹配模式 [文件]...
常用选项:
-i 忽略大小写。
-v 取反匹配
eg:
[root@qll ~]# grep root /etc/passwd
#在passwd文件中过滤出包含root
的行。
[root@qll ~]# grep -i ROOT /etc/passwd
#过滤出包含ROOT
的行(不区分大小写)
[root@qll ~]# grep -v root /etc/passwd
#过滤出不包含root
的行。