这是我参与11月更文挑战的第14天,活动详情查看:2021最后一次更文挑战
Linux 小知识 | 文件查看命令
本文介绍文件查看的常用命令
catlesstailheadgrep
| 命令 | 功能 |
|---|---|
| cat 文件名 | 查看小文件内容 |
| less -N 文件名 | 分屏显示大文件内容 |
| head -n 文件名 | 查看文件的前一部分 |
| tail -n 文件名 | 查看文件的最后部分 |
| grep 关键字 文件名 | 根据关键字搜索文本文件内容 |
cat 示例
[root@VM-8-10-centos touch-test]# touch small.txt
[root@VM-8-10-centos touch-test]# vim small.txt
[root@VM-8-10-centos touch-test]# cat small.txt
鹅鹅鹅,
曲项向天歌。
白毛浮绿水,
红掌拨清波。
[root@VM-8-10-centos touch-test]# cat -n small.txt #显示行号
1 鹅鹅鹅,
2 曲项向天歌。
3 白毛浮绿水,
4 红掌拨清波。
less 示例
[root@VM-8-10-centos touch-test]# less big.txt
[root@VM-8-10-centos touch-test]# less -N big.txt # 增加行号
# q 退出
tail 命令
# 查看文件的最后部分
# -数字 显示文件的最后几行
# -f 循环读取文档最后10行
# -n <行数> 显示文件的内容,从指定行至文件末尾
# -c 显示最后指定的字节数
[root@VM-8-10-centos touch-test]# tail big.txt
# 展示最后10行
呦呦鹿鸣,食野之苹。
我有嘉宾,鼓瑟吹笙。
明明如月,何时可掇?
忧从中来,不可断绝。
越陌度阡,枉用相存。
契阔谈讌,心念旧恩。
月明星稀,乌鹊南飞。
绕树三匝,何枝可依?
山不厌高,海不厌深。
周公吐哺,天下归心。
[root@VM-8-10-centos touch-test]# tail -3 big.txt # 显示最后3行
绕树三匝,何枝可依?
山不厌高,海不厌深。
周公吐哺,天下归心。
[root@VM-8-10-centos touch-test]# tail -f big.txt # 动态显示最后10行,实时的, 退出Ctrl+c
[root@VM-8-10-centos touch-test]# tail -4f big.txt # 动态显示最后4行
[root@VM-8-10-centos touch-test]# tail -n+2 big.txt # 从第二行到末尾
[root@VM-8-10-centos touch-test]# tail -c 45 big.txt # 显示文件最后45个字符
?不厌深。
周公吐哺,天下归心。
head 命令
查看文件的前一部分 命令参数与tail 类似
grep 命令
文本查找
# 语法 grep [参数选项] 关键字文件 # 根据关键词,搜索文本文件内容
[root@VM-8-10-centos touch-test]# cat small.txt
鹅鹅鹅,
曲项向天歌。
白毛浮绿水,
红掌拨清波。
[root@VM-8-10-centos touch-test]# grep 毛 small.txt
白毛浮绿水,
[root@VM-8-10-centos touch-test]# grep -n 毛 small.txt # 显示行号
3:白毛浮绿水,
[root@VM-8-10-centos touch-test]# grep -i 毛 small.txt # 忽略大小写
白毛浮绿水,
[root@VM-8-10-centos touch-test]# grep -v 毛 small.txt # 查找不含毛的行信息
鹅鹅鹅,
曲项向天歌。
红掌拨清波
进程查找
ps -ef | grep sshd # 在进程中查找含有sshd进程信息
[root@VM-8-10-centos touch-test]# ps -ef | grep bash
root 995 1 0 9月15 ? 00:02:18 /bin/bash /usr/local/sa/agent/watchdog.sh
root 3140164 3140163 0 11月05 pts/1 00:00:00 -bash
root 3149180 3140164 0 00:26 pts/1 00:00:00 grep --color=auto bash
# 排除掉有grep的bash进程信息
[root@VM-8-10-centos touch-test]# ps -ef | grep bash | grep -v "grep"
root 995 1 0 9月15 ? 00:02:18 /bin/bash /usr/local/sa/agent/watchdog.sh
root 3140164 3140163 0 11月05 pts/1 00:00:00 -bash
# 求个数
[root@VM-8-10-centos touch-test]# ps -ef | grep -c bash
3