Master Linux File Viewing: cat, more, less, head, tail & grep Explained
This guide introduces essential Linux commands—cat, more, less, head, tail, and grep—for viewing and searching file contents, explaining their options, typical usage examples, and key differences to help users efficiently navigate and inspect files from the terminal.
View File Content
1. cat
描述:查看文件内容
用法:cat [选项]...[文件]...
选项:
-n 显示行号,包括空白行
-b 显示行号,空白行不显示行号 [root@qll ~]# cat /etc/passwd
[root@qll ~]# cat -n /etc/passwd
[root@qll ~]# cat -b /etc/passwd2. more
more命令和cat的功能一样都是查看文件里的内容,但有所不同的是more可以按页来查看文件的内容,还支持直接跳转行等功能。
Common usage:
Enter key: scroll down n lines (default 1)
Space key: scroll down one screen
Ctrl+B: return to previous screen
=: display current line number
V: invoke vi editor
! command: invoke shell and execute command
q: quit more
[root@qll ~]# more /var/log/messages3. less
描述:分页查看文件内容,空格(下一页)、方向键(上下回翻)、q键(退出查看)。
[root@qll ~]# less /var/log/mingongge.log4. head
描述:查看文件头部内容,默认显示前10行。
用法:head [选项]...[文件]...
选项:
-c nK 显示文件前nKB的内容。
-n 显示文件前n行的内容。 [root@qll ~]# head -c 2k /var/log/messages # view first 2KB
[root@qll ~]# head -n 15 /var/log/messages # view first 15 lines5. tail
描述:查看文件的尾部内容,默认显示末尾10行。
用法:tail [选项]...[文件]...
选项:
-c nK 显示文件末尾nKB的内容。
-n 显示文件末尾n行的内容。
-f 动态显示文件内容,常用于查看日志,按 Ctrl+C 组合键退出。 [root@qll ~]# tail -c 2KB /var/log/messages # view last 2KB
[root@qll ~]# tail -n 15 /var/log/messages # view last 15 lines
[root@qll ~]# tail -f /var/log/messages # real‑time view6. grep
描述:查找关键词并打印匹配的行。
用法:grep [选项] 匹配模式 [文件]...
常用选项:
-i 忽略大小写。
-v 取反匹配 [root@qll ~]# grep root /etc/passwd # lines containing "root"
[root@qll ~]# grep -i ROOT /etc/passwd # case‑insensitive
[root@qll ~]# grep -v root /etc/passwd # exclude lines with "root"Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
