03-date命令

date:查看或修改系统时间命令

选项
+ 以指定格式显示时间日期,比如:+%F、+%Y%m%d
-d 根据说明修改时间
-s 修改时间

案例1:查看时间:

[root@linux-87-01 ~]# date
Wed May 3 22:06:17 CST 2023
[root@linux-87-01 ~]#

案例2:显示年月日

[root@linux-87-01 ~]# date +%F
2023-05-03
[root@linux-87-01 ~]# date +%Y%m%d
20230503
[root@linux-87-01 ~]# date +%Y年%m月%d日
2023年05月03日

案例3:手动修改时间

date -s '20230503 11:11:02'
date -s '20220510'

3、ntpdate时间同步命令

ntpdate可以连接指定的时间服务器,如果本地时间与时间服务器不同,ntpdate命令会自动修改本地时间。

3.1 安装ntpdate

[root@linux-87-01 ~]# yum install -y ntpdate

3.2 ntpdate使用

阿里云的时间服务器:ntp1.aliyun.com

微软的时间服务器:time.windows.com

[root@linux-87-01 ~]# ntpdate ntp1.aliyun.com
4 May 21:11:24 ntpdate[1972]: step time server 120.25.115.20 offset 10703458.938006 sec
[root@linux-87-01 ~]#

4、时区修改与查看

修改时区命令:timedatectl

查看时区:timedatectl status

[root@linux-87-01 ~]# timedatectl status
    Local time: Thu 2023-05-04 21:18:58 CST
Universal time: Thu 2023-05-04 13:18:58 UTC
      RTC time: Thu 2023-05-04 13:18:58
      Time zone: Asia/Shanghai (CST, +0800)
    NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
    DST active: n/a
[root@linux-87-01 ~]#

修改时区:timedatectl set-timezone 时区

[root@linux-87-01 ~]# timedatectl set-timezone 
Display all 426 possibilities? (y or n)

按tab键就可以查看有哪些时区。

5、关于时间的企业应用

date工作中应用:一般用于创建包含日期的文件或目录。

案例1:在/tmp/目录中创建以当前日期命名的文件bak-2023-05-04.txt

[root@linux-87-01 ~]# touch /tmp/bak-`date +%F`.txt
[root@linux-87-01 ~]# ls /tmp/
bak-2023-05-04.txt

反引号“:

反引号里面写命令,则优先执行命令,把输出结果留下

案例2:在/backup/目录下创建名为:bak-dir-2023-05-04_周几的目录

[root@linux-87-01 ~]# mkdir -p /backup/bak-dir-`date +%F_%w`
[root@linux-87-01 ~]# ll /backup/
total 0
drwxr-xr-x. 2 root root 6 May 4 21:34 bak-dir-2023-05-04_4
[root@linux-87-01 ~]#

02-wc命令与排序去重

1、wc命令

wc:word count/calcuate,统计文件中单词情况,大小,行数

选项:

-l 统计行数

例如:

[root@linux-87-01 ~]# wc -l /etc/services 
11176 /etc/services
[root@linux-87-01 ~]# wc /etc/services
11176 61033 670293 /etc/services

一般配合其他命令使用

2、查询命令位置

which、shereis

例如:查询ls命令存放的位置

[root@linux-87-01 ~]# 
[root@linux-87-01 ~]# which ls
alias ls='ls --color=auto'
/usr/bin/ls
[root@linux-87-01 ~]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz
[root@linux-87-01 ~]#

3、文件比较命令

diff、vimdiff

准备两个文件

seq:用于生成数字序列

[root@linux-87-01 ~]# seq 10 > 1.txt
[root@linux-87-01 ~]# seq 10 > 2.txt
[root@linux-87-01 ~]# vim 1.txt
1
2
3
aa
aa
6
7
8
9
10
​

比较两个文件:

使用diff命令:

[root@linux-87-01 ~]# diff 1.txt 2.txt 
4,5c4,5
< aa
< aa
---
> 4
> 5

使用vimdiff:

[root@linux-87-01 ~]# vimdiff 1.txt 2.txt

4、排序组合去重

sort:排序

uniq:去重并统计次数

4.1 sort排序

sort选项:

sort选项
-n number,把要排序的内容当作是数字,按照数字大小进行排序,默认是升序(小-->大)
-k 指定某一列,根据某一列进行排序
-r reverse,逆序排序
-t 指定分隔符,只能指定1个字符,默认是空格

案例1:sort简单使用

测试文件内容如下:

[root@linux-87-01 ~]# cat sort.txt 
4
5
9
91
22
1
10
5
6
11
7
1.11
[root@linux-87-01 ~]#

直接使用sort命令排序:

[root@linux-87-01 ~]# sort sort.txt 
1
10
11
1.11
22
4
5
5
6
7
9
91

默认是按字母\字符排序,所以要使用-n选项,按数字排序:

[root@linux-87-01 ~]# sort -n sort.txt 
1
1.11
4
5
5
6
7
9
10
11
22
91

从大到小排序:

[root@linux-87-01 ~]# sort -nr sort.txt 
91
22
11
10
9
7
6
5
5
4
1.11
1

案例2:按指定某一列排序

[root@linux-87-01 ~]# cat test.txt 
1 a 15
4 b 82
7 w 14
5 c 10

按第3列按数字逆序排序:

[root@linux-87-01 ~]# sort -rn -k3 test.txt 
4 b 82
1 a 15
7 w 14
5 c 10

案例3:ps命令是用于查看Linux进程信息的命令。ps aux用于查看进程较为详细的信息

需求:对ps aux结果中内存的列进行排序,取出前5个。

# 1. ps aux
第3列是cpu使用情况,第4列是内存使用情况
# 2. 使用管道(|),将ps aux的结果传给sort,然后再使用管道传给head命令
[root@linux-87-01 ~]# ps aux | sort -rn -k 4 | head -5

案例4:指定分隔符进行排序

需求:对passwd文件的第3列进行逆序排序

[root@linux-87-01 ~]# cp /etc/passwd .
[root@linux-87-01 ~]# sort -t ':' -rnk3 passwd

4.2 uniq去重

uniq:去重

选项:-c 去重并显示重复的次数

准备1.txt文件如下:

[root@linux-87-01 ~]# cat > 1.txt <<EOF
> a
> a
> b
> b
> 1
> 1
> 2
> 2
> 2
> 3
> EOF

uniq命令使用:

[root@linux-87-01 ~]# uniq -c 1.txt 
    2 a
    2 b
    2 1
    3 2
    1 3
[root@linux-87-01 ~]#

4.3 实战案例-日志分析

/var/log/secure日志,记录用户登录的情况

通过分析secure文件中的ip部分(secure.txt),分析每个ip出现的次数,取出前20个。

secure.txt只含有ip信息。

sort  secure.txt | uniq -c | sort -rnk1 | head -20

01-Linux快捷键

Linux快捷键可能与Windows的一些软件的快捷键有冲突。

Linux部分快捷键:

(1)移动光标类

ctrl + a :光标移动到行首

ctrl + e :光标移动到行尾

(2)删除(剪切)类

Ctrl + u :把光标所在位置到行首的内容删除(剪切)

Ctrl + k :把光标所在位置到行尾的内容删除(剪切)

Ctrl + y :黏贴

(3)功能性快捷键

Ctrl + l :清屏(l:小写字母L),相当于clear命令

Ctrl + c :取消当前的命令(c:cancel)

(4)Linux与Windows快捷键

Ctrl + s : Linux(锁屏) Windows(保存)

Ctrl + q :Linux(解锁) Windows(没有这个命令)

Ctrl + z :Linux(让当前正在运行的命令,后台挂起) Windows(撤销)

(5)命令补全键

按tab键可以补全命令

4、关机重启命令

(1)关机命令

shutdown :关机

shutdown -c :取消关机

shutdown -h : shutdown -h now表示立刻关机,shutdown -h 10表示10分钟后关机

halt:关机,关闭系统,需要手动切断电源

poweroff:关机

init 0:关机(0:数字0)

(2)重启命令

shutdown -r :重启,reboot,shutdown -r now或shutdown -r 10

reboot:重启

init 6:重启