第四篇:Linux目录结构命令

日志查询4剑客(head、tail、less、more)

  概述

  • Linux日志文件大,通过cat、vi/vim进行查看,系统可能卡死、内存不足
  • 推荐使用不会占用系统太多内存的命令,查看日志:head/tail、less/more
  • 故障案例:

  日志查询命令使用⭐⭐⭐⭐⭐

 1)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形式

 2)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
        同时打开连个窗口,一个查看,一个写入

 3)less 按页显示文件内容

less选项  
less -N 显示行号
less快捷方式  
q 退出
空格或f 下一页
b 上一页(back)
G 最后一行
g 第一行
99g 到第99行
/内容 搜索,n继续向下搜索,N继续向上搜索

 4)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

文件比较命令

未来在服务的配置中,我们需要对比新旧的配置文件,查看修改了哪些内容

# 创建测试文件
vim yuan-a.txt yuan-b.txt
进行编辑,一个文件编辑完成后:w保存
切换到下一个文件:n  切换到上一个文件:N

  diff

复制代码
[root@yuan ~]# cat yuan-a.txt
yuan.txt
jiang.txt
yuan.txt
yuan.txt
yuan.txt
[root@yuan ~]# cat yuan-b.txt
yuan.txt
yuan.txt
yuan.txt
yuan.txt
yuan.txt
[root@yuan ~]# diff yuan-a.txt yuan-b.txt
2c2
< jiang.txt
---
> yuan.txt

a append 增加
c 替换修改
d 删除 
复制代码

  vimdiff

vimdiff yuan-a.txt yuan-b.txt

排序去重组合(sort、uniq)⭐⭐⭐⭐⭐

  sort--排序

sort默认是对照字符的ASCII码进行比较

sort选项 说明
-n number  按照数字大小进行排序(升序)
-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      # sort默认是对照ASCII码进行比较
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 
yuan 18 linux 2000
xiao 20 linux 8000
jiang 20 linux 6000
yuan1 40 linux 8000
yuan2 20 linux 5000
[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
192.168.3.1 00:0F:AF:81:19:1F
192.168.3.2 00:0F:AF:85:6C:25
192.168.3.3 00:0F:AF:85:70:42
192.168.2.20 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.151 00:0F:AF:85:6C:F6

[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.2 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.151 00:0F:AF:85: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  年-月-日
  %Y-%m-%d  年-月-日
  %T  时:分:秒
  %H:%M:%S  时:分:秒
  %w 周几
-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

  ntpdate同步时间的命令

  • 用date -s命令修改时间,让系统时间不同步
  • ntpdate 同步时间命令

  修改时区

  应用实战

date应用场景:企业备份的时候,给压缩包加上时间

特殊符号(``):反引号里面的命令会被优先执行

# 案例:创建一个以时间命名的文件
[root@yuan ~]# touch /root/`date +%F`.txt
[root@yuan ~]# ll /root/
总用量 8
-rw-r--r--. 1 root root    0 9月  20 21:31 2024-09-20.txt

 

posted @   猿小姜  阅读(40)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示