1.日志查询四剑客
查看日志不要使用,cat或vim,vi命令。未来的日志文件。有的日志可达:40G或以上.
如果使用cat查看,会刷屏根本停不下来
如果使用vi/vim查看,从磁盘中加载到内存,占用系统内存,很容易导致系统内存不足.
1.1head
head选项 | |
---|
-n num | 显示头num行,默认显示头10行. |
案例01 显示/etc/passwd 的前5行
head -n5 /etc/passwd
head -n 5 /etc/passwd
head -5 /etc/passwd
1.2tail
tail选项 | |
---|
-n num | 显示最后num行,默认显示最后10行. |
-f | follow显示文件末尾的实时更新( 一般用于查看日志) tailf = tail -f |
案例02 显示/etc/passwd 的后5行
tail -n5 /etc/passwd
tail -5 /etc/passwd
案例03 查看/var/log/secure末尾的实时更新
tail -f /var/log/secure
1.3less
less选项与快捷方式 | 说明 |
---|
q | 退出 |
空格或f | 下一页 |
b | 上一页 (back) |
G | 最后1行 |
g | 第1行 |
99g | 到第99行 |
/内容 | 搜索,n继续向下搜索,N继续向上搜索 |
选项 | |
less -N | 显示行号 |
1.4more
more功能没有less多.
2.WC统计
#案例 04 统计/etc/services文件有多少行
wc -l /etc/services
11176 /etc/services
案例05 统计系统用户登录错误次数
#1. 过滤出日志中错误信息
grep 'Failed password' /var/log/secure #过滤
Jul 19 09:32:44 oldboy-83-lnb-v2 sshd[7082]:
Failed password for root from 10.0.0.1 port
65157 ssh2
Jul 19 09:32:55 oldboy-83-lnb-v2 sshd[7082]:
Failed password for root from 10.0.0.1 port
65157 ssh2
#2. 交给wc -l 统计次数
grep命令的结果传递给wc -l命令。
grep 'Failed password' /var/log/secure | wc -l
3.命令查询位置
3.1which
查询命令的位置
[root@oldboy-king-v3 ~]# which wc mkdir sed awk
ping ip shutdown
/usr/bin/wc
/usr/bin/mkdir
/usr/bin/sed
/usr/bin/awk
/usr/bin/ping
/usr/sbin/ip
/usr/sbin/shutdown
3.2whereis
查询命令及相关文件的位置
[root@oldboy-king-v3 ~]# whereis awk
[root@oldboy-king-v3 ~]# whereis awk
awk: /usr/bin/awk /usr/libexec/awk
/usr/share/awk /usr/share/man/man1/awk.1.gz
[root@oldboy-king-v3 ~]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz
[root@oldboy-king-v3 ~]# whereis -b ls
ls: /usr/bin/ls
[root@oldboy-king-v3 ~]# whereis -b ls sed awk
ls: /usr/bin/ls
sed: /usr/bin/sed
awk: /usr/bin/awk /usr/libexec/awk
/usr/share/awk
4.文件比较命令
未来在服务的配置中,我们需要对比下新旧的配置文件,查看修改了哪些内容。
这时候我们需要进行文件的对比操作,可以通过diff,vimdiff命令实现。
4.1diff
[root@oldboy-king-v3 ~]# diff oldboy.txt oldboygai.txt
1a2
> lidao.txt
a append 增加
c 替换,修改
d 删除
4.2vimdiff
vimdiff oldboy.txt oldboy-gai.txt

5.排序去重组合
5.1 sort-排序
sort选项 | 说明 |
---|
-n | number把要排序的内容当做是 数字 ,按照数字大小进行排序,默认是升序排序(小大). |
-k | 指定某一列,根据某一列进行排序. |
-r | reverse逆序排序 |
-t | 指定分隔符,只能指定1个字符. 默认是空格. |
案例06 对文件中的数字进行排序
[root@oldboy-king-v3 oldboy]# cat day005-06-
sort.txt
99
1
10
23
3
0000
02
9
19
[root@oldboy-king-v3 oldboy]# sort day005-06-
sort.txt
0000
02
1
10
19
23
3
9
[root@oldboy-king-v3 oldboy]# sort -n day005-
06-sort.txt
0000
1
02
3
9
10
19
23
99
5.2uniq
cat >/oldboy/uniq.txt<<EOF
oldboy
oldboy
oldboy
oldboy
oldboy
oldboy
lidao
lidao
lidao
lidao
lidao
smile
smile
smile
smile
blood
blood
blood
blood
EOF
uniq -c uniq.txt
6 oldboy
5 lidao
4 smile
4 blood
6.日前组合
6.1date
设置或查看系统的日期,时间命令。
未来主要用于查看日期或去日期
date选项 | |
---|
+ | 以xxxx格式显示日期与时间 |
| %F 年-月-日 %Y-%m-%d |
| %w 周几 |
| +%T %H:%M:%S 时:分:秒 |
-d | 根据说明修改时间 |
-s | 修改时间 |
6.2ntpdate同步时间的命令
#修改系统时间,让系统时间不同步.
date -s '20221111'
#安装时间同步命令
yum install -y ntpdate
#进行时间同步
ntpd 没开门ate ntp1.aliyun.com
19 Jul 15:26:13 ntpdate[7937]: step time server
120.25.115.20 offset -9880664.037056 sec
#最后检查
date命令查看时间
提示offset xxx sec表示同步成功
ntp1.aliyun.com 是阿里云的时间服务器之一.未来也可以自
己搭建.
ntp1��ntp7
6.3修改时区同步
#修改时区
timedatectl set-timezone Asia/Shanghai
#查看时区
[root@oldboy-lnb-king-v3 ~]# timedatectl status
Local time: 三 2022-11-30 15:53:44 CST
Universal time: 三 2022-11-30 07:53:44 UTC
RTC time: 三 2022-11-30 07:53:43
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: n/a
NTP synchronized: no
RTC in local TZ: no
DST active: n/a