cat、less、tail、wc命令查看文件或目录信息
cat显示文件内容及重定向
- 描述
cat命令可以查看文件的内容、连接文件、创建一个或多个文件和重定向输出到终端或指定文件
- 用法
cat [选项] [文件]
- 显示文件行号,包括空白行
[root@test shell]# cat -n file1
1 hello
2
3 world
- 显示文件行号,但空白行不显示行号
[root@test shell]# cat -b file1
1 hello
2 world
- 每一行的结尾显示“$“字符,常用于需要将多行内容转换成一行的情况
[root@test shell]# cat -e file1
hello$
$
world$
- 单个文件重定向
[root@test shell]# cat file1 > file
[root@test shell]# cat file
hello
world
- 多个文件连接后重定向
[root@test shell]# cat file1
hello
world
[root@test shell]# cat file2
linux
[root@test shell]# cat file1 file2 > file
[root@test shell]# cat file
hello
world
linux
more、less命令分页显示文件
more
描述:分页查看文件内容,通过空格键查看下一页,q键则退出查看(退出后,屏幕仍有)
[root@test ~]# more /root/install.log
less
描述:分页查看文件内容,空格(下一页)、方向键(上下回翻)、q键退出查看(退出后,屏幕不再显示)
[root@test ~]# less /root/install.log
head、tail查看文件头部或尾部
head
- 描述
查看文件头部内容,默认显示前10行
- 用法
head [选项] [文件]
- 选项
-c nK 显示文件前 nKB 的内容
-n 显示文件前 n行 的内容
- 实例
查看文件的前 2KB 的内容
[root@test ~]# head -c 2K /root/install.log
查看文件的前20行的内容
[root@test ~]# head -n 20 /root/install.log
tail
- 描述
查看文件的尾部内容,默认显示末尾 10行
- 用法
tail [选项] [文件]
- 选项
-c nK 显示文件末尾 nKB 的内容
-n 显示文件末尾 n行 的内容
-f 动态显示文件内容,常用于查看日志,按Ctrl+C组合键退出
- 实例
查看文件末尾 2KB 的内容
[root@test ~]# tail -c 2K /root/install.log
查看文件末尾 20行的内容
[root@test ~]# tail -20 /root/install.log
实时动态查看文件内容
[root@test ~]# tail -f /var/log/messages
WC
- 描述
显示文件的行、单词与字节统计信息
- 用法
wc [选项] [文件]
- 选项
-c 显示文件字节统计信息
-l 显示文件行数统计信息
-w 显示文件单词统计信息
- 实例
依次显示文件的行数、单词数、字节数
[root@test ~]# wc /etc/fstab
9 42 313 /etc/fstab
显示文件的字节信息
[root@test ~]# wc -c /etc/fstab
313 /etc/fstab
显示文件行数
[root@test ~]# wc -l /etc/fstab
9 /etc/fstab
显示文件的单词个数
[root@test ~]# wc -w /etc/fstab
42 /etc/fstab