日志查询4剑客(head、tail、less、more)
概述
- Linux日志文件大,通过cat、vi/vim进行查看,系统可能卡死、内存不足
- 推荐使用不会占用系统太多内存的命令,查看日志:head/tail、less/more
- 故障案例:
日志查询命令使用⭐⭐⭐⭐⭐
head 显示文件的头几行(默认是头十行)
| head选项 | |
| -n num | 显示头num行,默认显示头10行 |
# 案例:显示/etc/passwd 前5行 head -n5 /etc/passwd head -n 5 /etc/passwd head -5 /etc/passwd # 温馨提示: 一般情况下,使用-num即可,如果-num报错或无法使用,则使用-nnum形式
tail 显示文件的后几行(默认是后十行)
| tail选项 | |
| -n num | 显示后num行,默认显示后10行 |
| -f | follow 显示文件末尾的实时更新(一般用于查看日志) |
# 案例:显示/etc/passwd 后5行 tail -n5 /etc/passwd tail -5 /etc/passwd # 案例:查看/var/log/secure 末尾的输入的内容实时更新 tail -f /var/log/secure 同时打开连个窗口,一个查看,一个写入

less 按页显示文件内容
| less选项 | |
| less -N | 显示行号 |
| less快捷方式 | |
| q | 退出 |
| 空格或f | 下一页 |
| b | 上一页(back) |
| G | 最后一行 |
| g | 第一行 |
| 99g | 到第99行 |
| /内容 | 搜索,n继续向下搜索,N继续向上搜索 |
more 按页显示文件内容
more与less的区别:more到达最后一行就退出,less到达最后一行不退出
wc统计⭐⭐⭐⭐⭐
word count/calculate 统计文件中单词数、行数、字节数,未来工作中用来统计行数
| wc选项 | 说明 |
| -l | 统计行数 |
| -w | 统计单词数 |
| -c | 统计字节数 |
# 案例:统计/etc/services文件有多少行 [root@yuan ~]# wc -l /etc/services 11176 /etc/services # 温馨提示:未来wc使用案例 1、一般都是配合其他命令,可以取出想要查询内容在文件中出现的次数 2、还可以放在脚本中进行判断 # 案例:统计系统用户登录错误次数 # 1、过滤出日志中错误信息 grep 'Failed password' /var/log/secure # 2、将过滤出的结果交给wc-l统计次数 grep 'Failed password' /var/log/secure | wc -l grep命令过滤:在文件中找出需要过滤的内容 管道符号 | :将前一个命令的结果交给后面的命令使用
查询命令位置(熟悉)
which(查询命令的位置)⭐
[root@yuan ~]# which wc mkdir sed awk /usr/bin/wc /usr/bin/mkdir /usr/bin/sed /usr/bin/awk
whereis(查询命令的位置及相关文件的位置)
[root@yuan ~]# whereis awk ls awk: /usr/bin/awk /usr/libexec/awk /usr/share/awk /usr/share/man/man1/awk.1.gz ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz
文件比较命令(diff、vimdiff)
未来在服务的配置中,我们需要对比新旧的配置文件,查看修改了哪些内容
# 创建测试文件 vim yuan-a.txt yuan-b.txt 进行编辑,一个文件编辑完成后:w保存 切换到下一个文件:n 切换到上一个文件:N
diff
a append 增加 c 替换修改 d 删除 [root@yuan ~]# cat > yuan-a.txt <<EOF # >:此处为重定向 yuan.txt jiang.txt yuan.txt EOF [root@yuan ~]# cat > yuan-b.txt <<EOF yuan.txt yuan.txt yuan.txt EOF [root@yuan ~]# diff yuan-a.txt yuan-b.txt 2c2 < jiang.txt --- > yuan.txt
vimdiff
vimdiff yuan-a.txt yuan-b.txt

排序去重组合(sort、uniq)⭐⭐⭐⭐⭐
sort--排序
sort匹配规则⭐⭐⭐
sort默认是对照字符的ASCII码进行比较 sort -t '.' -rn -k2,2 -k4,4 /yuan/a.txt -k2,2:仅使用第2个字段作为主排序键 -k4,4:仅使用第4个字段作为次排序键 sort -t '.' -rn -k2 -k4 /yuan/a.txt -k2:使用从第2个字段开始,到行尾的所有字段作为主排序键 -k4:使用从第4个字段开始,到行尾的所有字段作为次排序键
sort参数⭐⭐⭐
| sort选项 | 说明 |
| -n | number 按照ASCII码数字大小进行排序(升序) |
| -k | key(关键字段) 按照某一列(字段)进行排序 |
| -r | reverse 逆序排序 |
| -t | 指定分隔符(只能指定一个字符,默认是空格) |
基本数字排序⭐⭐⭐⭐⭐
# 对文件中的数字进行排序 [root@yuan ~]# cat /yuan/a.txt 99 60 1 000 43 03 6 [root@yuan ~]# sort /yuan/a.txt 000 03 1 43 6 60 99 [root@yuan ~]# sort -n /yuan/a.txt 000 1 03 6 43 60 99
基于文件中某一列进行排序⭐⭐⭐⭐⭐
# 案例:对文件中的第二列按从大到小排序 [root@yuan ~]# cat /yuan/b.txt yuan 21 xiao 18 jiang 20 [root@yuan ~]# sort -rnk2 /yuan/b.txt yuan 21 # -rnk2 k2必须放在最后 jiang 20 xiao 18 # 企业面试题:取出/etc/目录下大小最大的前五个并按从大到小排序 [root@yuan ~]# ll /etc/ |sort -rnk5 |head -5 -rw-r--r--. 1 root root 670293 6月 7 2013 services -rw-r--r--. 1 root root 12288 9月 18 16:15 aliases.db -rw-r--r--. 1 root root 7274 9月 18 21:05 kdump.conf -rw-------. 1 tss tss 7046 8月 4 2017 tcsd.conf -rw-r--r--. 1 root root 6545 4月 1 2020 protocols
基于分隔符进行排序⭐
# 对passwd文件的第三列进行排序(逆序) [root@yuan ~]# cp /etc/passwd . # 不要对passwd文件直接操作,复制到当前目录 [root@yuan ~]# sort -t ':' -rnk3 /root/passwd polkitd:x:999:998:User for polkitd:/:/sbin/nologin systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin abrt:x:173:173::/etc/abrt:/sbin/nologin
多列排序
# 案例:对第二行进行从大到小排序,如果第二列有重复,则对第四列排序 [root@yuan ~]# cat >/yuan/a.txt <<EOF yuan 18 linux 2000 xiao 20 linux 8000 jiang 20 linux 6000 yuan1 40 linux 8000 yuan2 20 linux 5000
EOF [root@yuan ~]# sort -rn -k2 -k4 /yuan/a.txt yuan1 40 linux 8000 xiao 20 linux 8000 jiang 20 linux 6000 yuan2 20 linux 5000 yuan 18 linux 2000 # 面试题:以.为分隔符对第3列和第4列排序 [root@yuan ~]# cat > /yuan/a.txt <<EOF 192.168.3.1 00:0F:AF:81:19:1F 192.168.3.21 00:0F:AF:85:6C:25 192.168.3.30 00:0F:AF:85:70:42 192.168.2.30 00:0F:AF:85:55:DE 192.168.2.21 00:0F:AF:85:6C:09 192.168.2.22 00:0F:AF:85:5C:41 192.168.0.15 00:0F:AF:85:6C:F6
192.168.0.30 00:0F:AF:86:6C:F6 EOF [root@yuan ~]# sort -t '.' -rn -k3,3 -k4,4 /yuan/a.txt 192.168.3.3 00:0F:AF:85:70:42 192.168.3.21 00:0F:AF:85:6C:25 192.168.3.1 00:0F:AF:81:19:1F 192.168.2.22 00:0F:AF:85:5C:41 192.168.2.21 00:0F:AF:85:6C:09 192.168.2.20 00:0F:AF:85:55:DE 192.168.0.15 00:0F:AF:85:6C:F6
192.168.0.30 00:0F:AF:86:6C:F6
指定分隔符,多列排序的时候容易出现排序失误,需要手动设置排序的范围
sort -t '.' -rn -k3,3 -k4,4 /yuan/a.txt
-k3,3 表示仅对第3列排序 -k4,4 表示仅对第4列排序
uniq--去重(unique独一无二)
uniq只能对相邻的行进行合并(去重),如果不相邻,需要用sort命令调整为相邻
| uniq选项 | |
| -c | 去重并显示次数(重复次数) |
[root@yuan ~]# cat /yuan/a.txt yuan yuan xiao xiao yuan yuan [root@yuan ~]# uniq -c /yuan/a.txt 2 yuan 2 xiao 2 yuan [root@yuan ~]# sort /yuan/a.txt |uniq -c 2 xiao 4 yuan
日期组合⭐⭐
在Linux中我们需要日常查看系统的时间,保证整个网站所有服务器的系统时间一致
date⭐⭐
| date选项 | 说明 |
| + | 以xxxx格式显示日期与时间 |
| %F 年-月-日 2025-10-29 | |
| %Y-%m-%d 年-月-日 2025-10-29 %y-%m-%d 年-月-日 25-10-29 |
|
| %T 时:分:秒 21:27:59 | |
| %H:%M:%S 时:分:秒 21:28:35 | |
| %w 周几 3 | |
| -d | 根据说明修改时间 |
| -s | 修改时间 |
按照指定格式显示时间或日期
# 案例:按照指定格式显示当前日期 年-月-日 [root@yuan ~]# date +%F 2024-09-20 # 案例:按照指定格式显示当前日期 年-月-日_周几 [root@yuan ~]# date +%F_%w 2024-09-20_5
按照说明显示指定时间或日期
# 案例:显示1天前时间 [root@yuan ~]# date -d '-1day' 2024年 09月 19日 星期四 20:35:55 CST # 案例:显示1天前的日期 按照年-月-日_周几_小时 格式显示 [root@yuan ~]# date -d '-1day' +%F_%w_%H 2024-09-19_4_20
手动修改时间
[root@yuan ~]# date -s '20201111 11:11:11' 2020年 11月 11日 星期三 11:11:11 CST [root@yuan ~]# date -s '20221010' 2022年 10月 10日 星期一 00:00:00 CST
应用案例
# 案例:创建一个以时间命名的压缩包 [root@yuan ~]# touch /root/`date +%F`.tar.gz [root@yuan ~]# ll /root/ -rw-r--r--. 1 root root 0 9月 20 21:31 2024-09-20.tar.gz
ntpdate同步时间的命令⭐⭐
- 用date -s命令修改时间,让系统时间不同步
- ntpdate 同步时间命令
- ntpdate ntp1.aliyun.com

修改时区⭐⭐⭐⭐
创建软连接⭐⭐⭐⭐
[root@yuan ~]# ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime [root@yuan ~]# ll /etc/localtime lrwxrwxrwx 1 root root 33 Oct 29 21:38 /etc/localtime -> /usr/share/zoneinfo/Asia/Shanghai
timedatectl⭐⭐
[root@yuan ~]# timedatectl list-timezones |grep Shanghai Asia/Shanghai [root@yuan ~]# timedatectl set-timezone Asia/Shanghai [root@yuan ~]# timedatectl status timedatectl status Local time: Wed 2025-10-29 21:39:46 CST Universal time: Wed 2025-10-29 13:39:46 UTC RTC time: Wed 2025-10-29 13:39:46 Time zone: Asia/Shanghai (CST, +0800) NTP enabled: n/a NTP synchronized: no RTC in local TZ: no DST active: n/a

浙公网安备 33010602011771号