uptime命令

uptime:已开机时间(年龄)

1
2
3
[root@localhost ~]# uptime
14:18:18 up 2 days, 6:47, 1 user, load average: 5.87, 6.13, 6.52
[root@localhost ~]#
uptime输出字段 描述
14:18:18 表示当前的时间
up 2 days, 6:47 表示系统自启动后到现在的运行时间
1 user 表示当前登录入系统的用户数
load average: 5.87, 6.13, 6.52 表示近期1分钟,5分钟,15分钟内系统CPU的负载平均调度队列长度

who :确定谁在系统中
uptime :了解系统启动时间和忙碌程度
top :列出 资源占用排名靠前的进程
free :了解内存使用情况
vmstat :了解系统负载情况

who命令

命令 who : 列出当前已登录入系统的用户

1
2
3
[root@localhost ~]# who
root pts/5 2021-04-21 13:57 (192.168.1.2)
[root@localhost ~]#

who命令的输出分析

第一列:用户名;
第二列:终端设备的设备文件名
第三列:该用户登录的时间

什么是设备文件

设备在文件系统中有一个文件名,设备文件一般放于目录 /dev下,与普通磁盘文件不同的是设备文件的文件类型属于特殊文件

tty命令

tty命令可以打印出当前终端的设备文件名

1
2
3
[root@localhost ~]# tty
/dev/pts/5
[root@localhost ~]#

who am i命令

who am i命令可以 列出当前终端上的登录用户

1
2
3
[root@localhost ~]# who am i
root pts/5 2021-04-21 13:57 (192.168.1.2)
[root@localhost ~]#

whoami命令

命令whoami仅列出当前终端上的登录用户

1
2
3
[root@localhost ~]# whoami
root
[root@localhost ~]#

passwd更换口令

普通用户

使用 passwd 命令更改自己 的口令,在更改之前,系统会先验证原来的口令,通过口令之后才能修改口令。

超级用户root

修改口令之前不验证旧的口令。
root用户可修改自己的口令,还可强迫设置其它用户口令

例如root用户命令执行如下命令:

1
passwd liu

可以将用户liu的口令强迫设置为某一已知口令。

超级用户无法读取其它用户的口令
当普通用户忘记口令时,超级用户是无法帮该普通用户查出他原先的口令的,此时可请求超级用户把该用户的口令强制设置为一个新的口令。该普通用户使用新的口令登录后,再修改成更新的口令即可,以免让超级用户知道自己的口令。

修改root的口令后不要急于退出当前终端

修改超级用户root的口令后,先不要急于退出当前的终端,先尝试从别的终端登录root用户,如果发现无法登录,则表示设置的口令有误。此时在当前终端重设root用户的口令即可。
如果修改root的口令后直接退出当前终端,然后再重新登录,如果无法登录,那么将无法再次修改root的口令。

口令的设置与验证

口令信息的保存

不保存明码口令
设置口令的时候系统会生成一个随机序列,然后把该随机序列和用户输入的口令通过哈希算法进行计算。得到一个固定长度的哈希值。
系统保存的是生成的这个随机序列,以及计算后的哈希值A。
这样即使你知道随机序列和计算后的哈希值A,也是无法反推出用户的口令的。

图片

口令的验证过程

当用户登录的时候,再次把随机序列和用户口令通过哈希算法进行计算,得到一个哈希值B,然后把这个哈希值B与保存的哈希值A进行比较。如果两个哈希值一样,则表示口令正确。
这样做是安全的,因为无法根据随机值和哈希值,倒推出用户的口令。

这种方式也可以用在其他的软件设计中

Linux计算器bc

功能强大

  • 基本计算器功能
  • 支持变量、函数,条件和循环 等编程 功能(类似 C 语言的小编程语言)
  • 可以进行任意精度的 计算

精度

命令设置缺省精度

打开计算器的命令 描述
bc 缺省精度为小数点后 0 位
bc l 缺省精度 为小数点后 20 位

整数计算

1
2
3
4
5
6
7
8
[root@localhost ~]# bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
1+2*3/4
2

小数计算

1
2
3
4
5
6
7
8
[root@localhost ~]# bc -l
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
1+2*3/4
2.50000000000000000000

可以通过设置scale自行决定精度(小数点位数)

设置bc计算器精确到小数点后10000位

1
scale=10000

计算效果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@localhost ~]# bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
1+2*3/4
2
scale=10000
1+2*3/4
2.500000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000000000000000000000000000000000000000000000000000\

设置bc计算器精确到小数点后30位

1
2
3
4
scale=30 #输入
1+2*3/4 #输入
2.500000000000000000000000000000 #计算结果

bc编程

使用变量

查看变量

在bc中输入变量的值,然后在输入变量名即可查看该变量的值:

1
2
p=20
p

运行效果

1
2
3
p=20
p
20

使用变量进行计算

1
2
3
4
5
6
7
8
9
[root@localhost ~]# bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
p=20 #输入
(2+p)*p #输入
440 #计算结果

使用循环

求1~128的阶乘,在bc中分别输入如下三行代码:

1
2
3
s=1;          
for(i=1;i<128;i++) s*=i;
s

计算结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@localhost ~]# bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
s=1; #输入
for(i=1;i<128;i++) s*=i; #输入
s #输入
30126600184576595448099770775270596923241649186736217990533469005966\
67207618480809067860692097713761984609779945772783965563851033300772\
32629777308785186998250027066179124412259762176000000000000000000000\
0000000000

cal打印当前月的日历

直接输入cal命令会打印当前月的日历,
运行效果

1
2
3
4
5
6
7
8
9
10
[root@localhost ~]# cal
四月 2021
日 一 二 三 四 五 六
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30

[root@localhost ~]#

cal year打印指定年的日历

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
[root@localhost ~]# cal 2020
2020

一月 二月 三月
日 一 二 三 四 五 六 日 一 二 三 四 五 六 日 一 二 三 四 五 六
1 2 3 4 1 1 2 3 4 5 6 7
5 6 7 8 9 10 11 2 3 4 5 6 7 8 8 9 10 11 12 13 14
12 13 14 15 16 17 18 9 10 11 12 13 14 15 15 16 17 18 19 20 21
19 20 21 22 23 24 25 16 17 18 19 20 21 22 22 23 24 25 26 27 28
26 27 28 29 30 31 23 24 25 26 27 28 29 29 30 31

四月 五月 六月
日 一 二 三 四 五 六 日 一 二 三 四 五 六 日 一 二 三 四 五 六
1 2 3 4 1 2 1 2 3 4 5 6
5 6 7 8 9 10 11 3 4 5 6 7 8 9 7 8 9 10 11 12 13
12 13 14 15 16 17 18 10 11 12 13 14 15 16 14 15 16 17 18 19 20
19 20 21 22 23 24 25 17 18 19 20 21 22 23 21 22 23 24 25 26 27
26 27 28 29 30 24 25 26 27 28 29 30 28 29 30
31
七月 八月 九月
日 一 二 三 四 五 六 日 一 二 三 四 五 六 日 一 二 三 四 五 六
1 2 3 4 1 1 2 3 4 5
5 6 7 8 9 10 11 2 3 4 5 6 7 8 6 7 8 9 10 11 12
12 13 14 15 16 17 18 9 10 11 12 13 14 15 13 14 15 16 17 18 19
19 20 21 22 23 24 25 16 17 18 19 20 21 22 20 21 22 23 24 25 26
26 27 28 29 30 31 23 24 25 26 27 28 29 27 28 29 30
30 31
十月 十一月 十二月
日 一 二 三 四 五 六 日 一 二 三 四 五 六 日 一 二 三 四 五 六
1 2 3 1 2 3 4 5 6 7 1 2 3 4 5
4 5 6 7 8 9 10 8 9 10 11 12 13 14 6 7 8 9 10 11 12
11 12 13 14 15 16 17 15 16 17 18 19 20 21 13 14 15 16 17 18 19
18 19 20 21 22 23 24 22 23 24 25 26 27 28 20 21 22 23 24 25 26
25 26 27 28 29 30 31 29 30 27 28 29 30 31


[root@localhost ~]#

cal month year打印指定年月的日历

1
2
3
4
5
6
7
8
9
10
[root@localhost ~]# cal 6 2020
六月 2020
日 一 二 三 四 五 六
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30

[root@localhost ~]#

ntpdate

NTP协议

NTP是什么

NTP 是网络时间协议(Network Time Protocol),它用来同步网络设备【如计算机、手机】的时间的协议。

NTP实现什么目的

目的很简单,就是为了提供准确时间。因为我们的手表、手机、电脑等设备,经常会跑着跑着时间就出现了误差,或快或慢的少几秒,时间长了甚至误差过分钟。

NTP服务器列表

Windows系统上自带的两个:time.windows.com 和 time.nist.gov
MacOS上自带的两个:time.apple.com 和 time.asia.apple.com
NTP授时快速域名服务:cn.ntp.org.cn

1、阿里云授时服务器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#NTP服务器
ntp.aliyun.com
ntp1.aliyun.com
ntp2.aliyun.com
ntp3.aliyun.com
ntp4.aliyun.com
ntp5.aliyun.com
ntp6.aliyun.com
ntp7.aliyun.com
#Time服务器
time1.aliyun.com
time2.aliyun.com
time3.aliyun.com
time4.aliyun.com
time5.aliyun.com
time6.aliyun.com
time7.aliyun.com

2、国内大学授时服务器

1
2
3
4
5
6
s1c.time.edu.cn       北京大学
s2m.time.edu.cn 北京大学
s1b.time.edu.cn 清华大学
s1e.time.edu.cn 清华大学
s2a.time.edu.cn 清华大学
s2b.time.edu.cn 清华大学

3、国外授时服务器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#苹果提供的授时服务器   
time1.apple.com
time2.apple.com
time3.apple.com
time4.apple.com
time5.apple.com
time6.apple.com
time7.apple.com

#Google提供的授时服务器
time1.google.com
time2.google.com
time3.google.com
time4.google.com

通过 NTP 协议校对系统时间

1
ntpdate

如果没有安装ntpdate,查找安装包:

1
yum list ntpdate

安装ntpdate:

1
yum install ntpdate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@localhost ~]# yum list ntpdate
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
可安装的软件包
ntpdate.aarch64 4.2.6p5-29.el7.centos.2 base
[root@localhost ~]# yum install ntpdate
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 ntpdate.aarch64.0.4.2.6p5-29.el7.centos.2 将被 安装
--> 解决依赖关系完成

依赖关系解决
......

查询时间ntpdate -q NTP服务器

1
ntpdate -q 0.pool.ntp.org

运行效果:

1
2
3
4
5
6
7
[root@localhost ~]# ntpdate -q 0.pool.ntp.org
server 139.199.214.202, stratum 2, offset 0.225052, delay 0.06584
server 162.159.200.1, stratum 0, offset 0.000000, delay 0.00000
server 185.209.85.222, stratum 2, offset 0.224910, delay 0.31339
server 5.79.108.34, stratum 3, offset 0.219270, delay 0.26381
20 Apr 21:32:30 ntpdate[22284]: adjust time server 139.199.214.202 offset 0.225052 sec
[root@localhost ~]#

使用阿里云

1
ntpdate -q ntp.aliyun.com

运行效果:

1
2
3
4
[root@localhost ~]# ntpdate -q ntp.aliyun.com
server 203.107.6.88, stratum 2, offset 0.210540, delay 0.09094
20 Apr 21:33:05 ntpdate[22518]: adjust time server 203.107.6.88 offset 0.210540 sec
[root@localhost ~]#

运行效果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@localhost ~]# ntpdate -q cn.ntp.org.cn
server 2001:da8:9000::130, stratum 1, offset 0.257669, delay 0.10617
server 2001:da8:9000::81, stratum 1, offset 0.256599, delay 0.10426
server 111.230.50.201, stratum 3, offset 0.266627, delay 0.05635
server 120.25.115.20, stratum 2, offset 0.263829, delay 0.05809
server 203.107.6.88, stratum 2, offset 0.267893, delay 0.08539
server 182.92.12.11, stratum 2, offset 0.264843, delay 0.08183
server 120.25.108.11, stratum 2, offset 0.263255, delay 0.05913
server 120.197.116.202, stratum 1, offset 0.257536, delay 0.08014
server 119.29.26.206, stratum 3, offset 0.262630, delay 0.05463
server 106.75.185.63, stratum 2, offset 0.262619, delay 0.05322
server 223.113.120.195, stratum 3, offset 0.259761, delay 0.07321
server 223.113.97.98, stratum 0, offset 0.000000, delay 0.00000
server 223.65.211.46, stratum 0, offset 0.000000, delay 0.00000
server 36.154.179.82, stratum 5, offset 0.260445, delay 0.07410
server 223.113.103.191, stratum 3, offset 0.182134, delay 0.07329
server 58.220.133.132, stratum 3, offset 0.264724, delay 0.08644
server 114.67.237.130, stratum 2, offset 0.261922, delay 0.08417
server 114.67.103.73, stratum 0, offset 0.000000, delay 0.00000
server 140.143.99.185, stratum 3, offset 0.266090, delay 0.06126
20 Apr 21:31:13 ntpdate[21731]: adjust time server 120.197.116.202 offset 0.257536 sec
[root@localhost ~]#

设置时间ntpdate NTP服务器

1
ntpdate ntp.aliyun.com

运行结果:

1
2
3
[root@localhost ~]# ntpdate ntp.aliyun.com
20 Apr 21:39:16 ntpdate[25139]: adjust time server 203.107.6.88 offset 0.026031 sec
[root@localhost ~]#

参考资料

https://www.jianshu.com/p/28864ab7fdd9

读取系统日期和时间命令date

1
date

运行结果:

1
2
3
[root@localhost ~]# date
2021年 04月 20日 星期二 17:38:08 CST
[root@localhost ~]#

定制date输出格式

1
date "+%Y-%m-%d %H:%M:%S Day %j"

运行结果:

1
2
3
[root@localhost ~]# date "+%Y-%m-%d %H:%M:%S Day %j"
2021-04-20 17:41:05 Day 110
[root@localhost ~]#

110 指 的是今天是今年的 第 110 天,

date格式控制字符串

第一个字符必须为+%Y代表年号,%m代表月份,%M 代表分钟。%H表示小时
%s表示秒坐标(从 UTC1970 开始),或者叫时间戳常用于计算时间间隔。

1
date "+%s"

运行结果

1
2
3
[root@localhost ~]# date "+%s"
1618912024
[root@localhost ~]#

man date

Linux 命令 往往有很多选项和复杂的功能,通过 man date 查阅联机手册

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
DATE(1)                                                                         FSF                                                                        DATE(1)

NAME
date - 打印或设置系统日期和时间

总览
date [选项]... [+格式]
date [选项] [MMDDhhmm[[CC]YY][.ss]]

描述
根据指定格式显示当前时间或设置系统时间.

-d, --date=STRING
显示由 STRING 指定的时间, 而不是当前时间

-f, --file=DATEFILE
显示 DATEFILE 中每一行指定的时间, 如同将 DATEFILE 中的每行作为 --date 的参数一样

-I, --iso-8601[=TIMESPEC] 按照 ISO-8601 的日期/时间格式输出时间.

TIMESPEC=`date' (或者不指定时)仅输出日期,等于 `hours', `minutes', 或`seconds' 时按照指定精度输出日期及时间.

-r, --reference=FILE
显示 FILE 的最后修改时间

-R, --rfc-822
根据 RFC-822 指定格式输出日期

-s, --set=STRING
根据 STRING 设置时间

-u, --utc, --universal
显示或设置全球时间(格林威治时间)

--help 显示本帮助文件并退出

--version
显示版本信息并退出


格式 FORMAT 控制着输出格式. 仅当选项指定为全球时间时本格式才有效。 分别解释如下:

%% 文本的 %

%a 当前区域的星期几的简写 (Sun..Sat)

%A 当前区域的星期几的全称 (不同长度) (Sunday..Saturday)

%b 当前区域的月份的简写 (Jan..Dec)

%B 当前区域的月份的全称(变长) (January..December)

%c 当前区域的日期和时间 (Sat Nov 04 12:02:33 EST 1989)

%d (月份中的)几号(用两位表示) (01..31)

%D 日期(按照 月/日期/年 格式显示) (mm/dd/yy)

%e (月份中的)几号(去零表示) ( 1..31)

%h 同 %b

%H 小时(按 24 小时制显示,用两位表示) (00..23)

%I 小时(按 12 小时制显示,用两位表示) (01..12)

%j (一年中的)第几天(用三位表示) (001..366)

%k 小时(按 24 小时制显示,去零显示) ( 0..23)

%l 小时(按 12 小时制显示,去零表示) ( 1..12)

%m 月份(用两位表示) (01..12)

%M 分钟数(用两位表示) (00..59)

%n 换行

%p 当前时间是上午 AM 还是下午 PM

%r 时间,按 12 小时制显示 (hh:mm:ss [A/P]M)

%s 从 1970年1月1日0点0分0秒到现在历经的秒数 (GNU扩充)

%S 秒数(用两位表示)(00..60)

%t 水平方向的 tab 制表符

%T 时间,按 24 小时制显示(hh:mm:ss)

%U (一年中的)第几个星期,以星期天作为一周的开始(用两位表示) (00..53)

%V (一年中的)第几个星期,以星期一作为一周的开始(用两位表示) (01..52)

%w 用数字表示星期几 (0..6); 0 代表星期天

%W (一年中的)第几个星期,以星期一作为一周的开始(用两位表示) (00..53)

%x 按照 (mm/dd/yy) 格式显示当前日期

%X 按照 (%H:%M:%S) 格式显示当前时间

%y 年的后两位数字 (00..99)

%Y 年(用 4 位表示) (1970...)





%z 按照 RFC-822 中指定的数字时区显示(如, -0500) (为非标准扩充)

%Z 时区(例如, EDT (美国东部时区)), 如果不能决定是哪个时区则为空

默认情况下,用 0 填充数据的空缺部分. GNU 的 date 命令能分辨在 `%'和数字指示之间的以下修改.

`-' (连接号) 不进行填充 `_' (下划线) 用空格进行填充

BUG报告
请向<bug-sh-utils@gnu.org>报告BUG.

参考
关于 date 的详细说明是个 Texinfo 手册. 如果在你的计算机上已经成功安装了 info 和 date 程序,你可以使用

info date

命令访问完全手册.

date -d String

显示字符串对应的时间

1
2
3
[root@localhost ~]# date -d 2014-2-7
2014年 02月 07日 星期五 00:00:00 CST
[root@localhost ~]#

date -f 日期文件

解析日期文件中的每个日期

1
2
3
4
[root@localhost Linux_Test]# cat date_test.txt 
2020-01-24 04:05:06
2021/4/25
[root@localhost Linux_Test]#

显示date_test.txt文件中每行日期对应的具体时间:

1
2
3
4
[root@localhost Linux_Test]# date -f date_test.txt 
2020年 01月 24日 星期五 04:05:06 CST
2021年 04月 25日 星期日 00:00:00 CST
[root@localhost Linux_Test]#

date -r 文件

显示文件最新更改时间。

1
2
3
4
5
[root@localhost Linux_Test]# ls
date_test.txt vi_replaceAllTest.txt
[root@localhost Linux_Test]# date -r vi_replaceAllTest.txt
2021年 04月 14日 星期三 01:48:41 CST
[root@localhost Linux_Test]#

date -R

1
2
3
[root@localhost Linux_Test]# date -R
Tue, 20 Apr 2021 18:25:59 +0800
[root@localhost Linux_Test]#

date -s 日期时间字符串

根据传入的日期时间字符串设置系统的时间

date -u

显示格林威治时间

1
2
3
[root@localhost Linux_Test]# date -u
2021年 04月 20日 星期二 10:28:52 UTC
[root@localhost Linux_Test]#

如何在markdown表格中填写Linux管道符号

问题描述

例如我想在表格中写命令:

1
last -f /var/log/btmp | more

但是命令中的管道符|和markdown表格的分隔符相同。
这就会造成,解析后的markdown把管道符错误的识别成单元格的分隔符的情况:

错误示例

/var/log子目录 描述
/var/log/alternatives.log 更新替代信息都记录在这个文件中。
/var/log/btmp 记录所有失败登录信息。使用last命令可以查看btmp文件。例如,”last -f /var/log/btmp
/var/log/user.log 记录所有等级用户信息的日志。

使用html转义符

解决方案,使用html转义符,竖线|的html转义符为&#124;
也就是说把上面的命令改成如下即可:

1
last -f /var/log/btmp &#124; more
/var/log子目录 描述
/var/log/alternatives.log 更新替代信息都记录在这个文件中。
/var/log/btmp 记录所有失败登录信息。使用last命令可以查看btmp文件。例如,”last -f /var/log/btmp &#124; more”。
/var/log/user.log 记录所有等级用户信息的日志。

参考资料

https://www.cnblogs.com/xcsn/p/3559624.html

不递归查找 find -maxdepth 1

例如只查找当前目录下的文件:
(比StartWriting.bat新,比HexoS.bat旧的文件)

1
find . -maxdepth 1 -newer StartWriting.bat \! \( -newer HexoS.bat \)

运行效果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[root@localhost exam]# ls -ltr --full-time
总用量 3640
-rw-r--r--. 1 root root 422 2021-03-19 21:12:12.566962591 +0800 StartWriting.bat
drwxr-xr-x. 2 root root 4096 2021-03-19 21:12:12.566962591 +0800 scaffolds
-rw-r--r--. 1 root root 817 2021-03-19 21:12:12.566962591 +0800 package.json
-rw-r--r--. 1 root root 376 2021-03-19 21:12:12.566962591 +0800 HexoSTest.bat
-rw-r--r--. 1 root root 576 2021-03-19 21:12:12.566962591 +0800 HexoD.bat
-rw-r--r--. 1 root root 158 2021-03-19 21:12:12.566962591 +0800 FM.properties
-rw-r--r--. 1 root root 3093 2021-03-19 21:12:12.566962591 +0800 _config.yml
drwxr-xr-x. 3 root root 4096 2021-03-19 21:12:12.636962591 +0800 themes
drwxr-xr-x. 9 root root 4096 2021-03-19 21:12:12.636962591 +0800 source
-rwxr--r--. 1 root root 174 2021-03-19 21:16:37.816962490 +0800 hexos.sh
drwxr-xr-x. 308 root root 8192 2021-03-19 21:19:12.036962431 +0800 node_modules
-rw-r--r--. 1 root root 170424 2021-03-19 21:19:12.746962431 +0800 package-lock.json
-rw-r--r--. 1 root root 3498022 2021-03-22 20:29:10.927984900 +0800 db.json
-rw-r--r--. 1 root root 431 2021-03-22 20:52:44.997984360 +0800 HexoS.bat
[root@localhost exam]# find . -maxdepth 1 -newer StartWriting.bat \! \( -newer HexoS.bat \)
./HexoS.bat
./source
./themes
./hexos.sh
./node_modules
./db.json
./package-lock.json
[root@localhost exam]#

Matcher类

Matcher对象是一个状态机器,它依据Pattern对象做为匹配模式对字符串展开匹配检查,此类的实例用于多个并发线程是不安全的

方法 描述
String group(int group) Returns the input subsequence captured by the given group during the previous match operation.
String group(String name) Returns the input subsequence captured by the given named-capturing group during the previous match operation.
int start(int group) Returns the start index of the subsequence captured by the given group during the previous match operation.
int end(int group) Returns the offset after the last character of the subsequence captured by the given group during the previous match operation.
int end(String name) Returns the offset after the last character of the subsequence captured by the given named-capturing group during the previous match operation.
boolean find(int start) Resets this matcher and then attempts to find the next subsequence of the input sequence that matches the pattern, starting at the specified index.
int groupCount() Returns the number of capturing groups in this matcher’s pattern.
boolean hasAnchoringBounds() Queries the anchoring of region bounds for this matcher.
boolean hasTransparentBounds() Queries the transparency of region bounds for this matcher.
boolean hitEnd() Returns true if the end of input was hit by the search engine in the last match operation performed by this matcher.
boolean lookingAt() Attempts to match the input sequence, starting at the beginning of the region, against the pattern.
boolean matches() Attempts to match the entire region against the pattern.
Pattern pattern() Returns the pattern that is interpreted by this matcher.
static String quoteReplacement(String s) Returns a literal replacement String for the specified String.
Matcher region(int start, int end) Sets the limits of this matcher’s region.
int regionEnd() Reports the end index (exclusive) of this matcher’s region.
int regionStart() Reports the start index of this matcher’s region.
String replaceAll(String replacement) Replaces every subsequence of the input sequence that matches the pattern with the given replacement string.
String replaceAll(Function<MatchResult,​String> replacer) Replaces every subsequence of the input sequence that matches the pattern with the result of applying the given replacer function to the match result of this matcher corresponding to that subsequence.
String replaceFirst(String replacement) Replaces the first subsequence of the input sequence that matches the pattern with the given replacement string.
String replaceFirst(Function<MatchResult,​String> replacer) Replaces the first subsequence of the input sequence that matches the pattern with the result of applying the given replacer function to the match result of this matcher corresponding to that subsequence.
boolean requireEnd() Returns true if more input could change a positive match into a negative one.
Matcher reset() Resets this matcher.
Matcher reset(CharSequence input) Resets this matcher with a new input sequence.
Stream<MatchResult> results() Returns a stream of match results for each subsequence of the input sequence that matches the pattern.
int start(String name) Returns the start index of the subsequence captured by the given named-capturing group during the previous match operation.
MatchResult toMatchResult() Returns the match state of this matcher as a MatchResult.
String toString() Returns the string representation of this matcher.
Matcher useAnchoringBounds(boolean b) Sets the anchoring of region bounds for this matcher.
Matcher usePattern(Pattern newPattern) Changes the Pattern that this Matcher uses to find matches with.
Matcher useTransparentBounds(boolean b) Sets the transparency of region bounds for this matcher.
方法 描述
boolean find() Attempts to find the next subsequence of the input sequence that matches the pattern.
int end() Returns the offset after the last character matched.
Matcher appendReplacement(StringBuffer sb, String replacement) Implements a non-terminal append-and-replace step.
Matcher appendReplacement(StringBuilder sb, String replacement) Implements a non-terminal append-and-replace step.
StringBuffer appendTail(StringBuffer sb) Implements a terminal append-and-replace step.
StringBuilder appendTail(StringBuilder sb) Implements a terminal append-and-replace step.
int start() Returns the start index of the previous match.
String group() Returns the input subsequence matched by the previous match.

小结

  • find()方法匹配子串的时候都会记下本次成功匹配子串的开始位置结束位置
  • appendReplacement(sb,replacement)方法先获取所有从上次匹配的结束位置,到本次匹配结束位置的所有字符串,然后替换其中匹配的部分,最后追加到sb中。
  • appendReplacement(sb)方法把结束位置到文本结尾的所有字符追加到sb中。
  • 使用appendReplace()和appendTail()方法可以更加灵活的进行替换操作,注意要结合find()方法使用。

参考资料

http://www.51gjie.com/java/759.html