文章目录
15.时间相关的指令
- 语法:
date [OPTION]... [+FORMAT]
- 功能: date 指定格式显示时间: date +%Y:%m:%d
1.在显示方面,使用者可以设定欲显示的格式,格式设定为一个加号后接数个标记,其中常用的标记列表如下
- %H : 小时(00…23)
- %M : 分钟(00…59)
- %S : 秒(00…61)
- %X : 相当于 %H:%M:%S
- %d : 日 (01…31)
- %m :月份 (01…12)
- %Y : 完整年份 (0000…9999)
- %F : 相当于 %Y-%m-%d
2.在设定时间方面
- date -s //设置当前时间,只有root权限才能设置,其他只能查看。
- date -s 20080523 //设置成20080523,这样会把具体时间设置成空00:00:00 date -s 01:01:01 //设置具体时间,不会对日期做更改
- date -s “01:01:01 2008-05-23″ //这样可以设置全部时间
- date -s “01:01:01 20080523″ //这样可以设置全部时间
- date -s “2008-05-23 01:01:01″ //这样可以设置全部时间
3.时间戳
- 时间->时间戳:date +%s
- 时间戳->时间:date -d@1508749502
Unix时间戳(英文为Unix epoch, Unix time, POSIX time 或 Unix timestamp)是从1970年1月1日(UTC/GMT的 午夜)开始所经过的秒数,不考虑闰秒
示例:
[root@VM-12-2-centos dir1]# date +%Y/%m/%d
2023/04/24
[root@VM-12-2-centos dir1]# date +%Y-%m-%d
2023-04-24
[root@VM-12-2-centos dir1]# date +%H:%M:%S
08:47:18
[root@VM-12-2-centos dir1]# date +%H-%M-%S
08-47-31
[root@VM-12-2-centos dir1]# date +%s
1682297308 👉当前时间的时间戳
[root@VM-12-2-centos dir1]# date -d @1682297308
Mon Apr 24 08:48:28 CST 2023 👉 时间戳转换为时间
[root@VM-12-2-centos dir1]# date +%Y-%m-%d/%H:%M:%S -d @1682297308
2023-04-24/08:48:28 👆按特定的格式把一个时间戳转换为时间
16.Cal指令
- 语法:
cal [参数][月份][年份]
- 功能: 用于查看日历等时间信息,如只有一个参数,则表示年份(1-9999),如有两个参数,则表示月份和年份
- 常用选项:
- -3 显示系统前一个月,当前月,下一个月的月历
- -j 显示在当年中的第几天(一年日期按天算,从1月1号算起,默认显示当前月在一年中的天数)
- -y 显示当前年份的日历
示例:(cal指令默认显示本月的日历)
[root@VM-12-2-centos dir1]# cal
April 2023
Su Mo Tu We Th Fr Sa
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@VM-12-2-centos dir1]# cal -3
March 2023 April 2023 May 2023
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 1 1 2 3 4 5 6
5 6 7 8 9 10 11 2 3 4 5 6 7 8 7 8 9 10 11 12 13
12 13 14 15 16 17 18 9 10 11 12 13 14 15 14 15 16 17 18 19 20
19 20 21 22 23 24 25 16 17 18 19 20 21 22 21 22 23 24 25 26 27
26 27 28 29 30 31 23 24 25 26 27 28 29 28 29 30 31
30
[root@VM-12-2-centos dir1]#
- 补充:sort(排序)
- 功能: 依次比较每一行的每个字母,按照ASCII码值排序,默认 升序
- 选项:
- -r reverse 按逆序排列
示例:
[root@VM-12-2-centos dir1]# cat test.txt
abdddddddd
cddddd
ffffffffffccc
edffffff
rrrrrrrr
ooooooo
uuuuuu
nnnnnnn
ddddddddd
[root@VM-12-2-centos dir1]# sort test.txt
abdddddddd
cddddd
ddddddddd
edffffff
ffffffffffccc
nnnnnnn
ooooooo
rrrrrrrr
uuuuuu
[root@VM-12-2-centos dir1]# sort -r test.txt
uuuuuu
rrrrrrrr
ooooooo
nnnnnnn
ffffffffffccc
edffffff
ddddddddd
cddddd
abdddddddd
[root@VM-12-2-centos dir1]#
- 补充:uniq(去重)
示例:
[root@VM-12-2-centos dir1]# cat test.txt
abdddddddd
aaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaa
cddddd
ffffffffffccc
edffffff
rrrrrrrr
ooooooo
ooooooo
uuuuuu
uuuuuu
nnnnnnn
ddddddddd
[root@VM-12-2-centos dir1]# sort test.txt | uniq
aaaaaaaaaaaaaaaa
abdddddddd
cddddd
ddddddddd
edffffff
ffffffffffccc
nnnnnnn
ooooooo
rrrrrrrr
uuuuuu
[root@VM-12-2-centos dir1]#
17.find指令(非常重要)- name
- 语法:
find pathname -options
- 功能: 用于在文件树种查找文件,并作出相应的处理(可能访问磁盘)
- 常用选项:
- -name 按照文件名查找文件。
- 搜索三剑客:
- find
- which 搜索指令
- whereis 搜索文档/程序
- 补充:alias
(相当于给一个指令起别名)
alias myls = 'ls -a -l -i -n'
👉 ls -a -l -i -n 可以用 myls 代替
which myls
👉 可以看到这个指令的内容
18.grep指令
(行过滤工具)
- 语法:
grep [选项] '搜寻字符串' 文件
(没有引号、 “ ” 或者 ‘ ’ 都可以,如果要搜索的字符串中带空格,最好带上引号) - 功能: 在文件中搜索字符串,将找到的行打印出来
- 常用选项:
- -i :忽略大小写的不同,所以大小写视为相同
- -n :顺便输出行号
- -v :反向选择,亦即显示出没有 ‘搜寻字符串’ 内容的那一行
示例:
[root@VM-12-2-centos dir1]# cat test.txt
abdddddddd
aaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaa
cddddd
ffffffffffccc
edffffff
rrrrrrrr
ooooooo
ooooooo
uuuuuu
uuuuuu
nnnnnnn
ddddddddd
[root@VM-12-2-centos dir1]# grep 'aa' test.txt
aaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaa
[root@VM-12-2-centos dir1]# grep 'd' test.txt
abdddddddd
cddddd
edffffff
ddddddddd
[root@VM-12-2-centos dir1]#
- 补充:Tab键
例如:输入 ca 后按下 Tab键,将搜索并显示所有 ca 开头的指令。即Tab键具有辅助命令补全的功能。
[root@VM-12-2-centos ~]# ca
cacertdir_rehash cache_repair cairo-sphinx callgrind_annotate case
cache_check cache_restore cal callgrind_control cat
cache_dump cache_writeback ca-legacy capsh catchsegv
cache_metadata_size cagent_tools caller captoinfo catman
19.zip/unzip指令
- 语法:
zip 压缩文件.zip 目录或文件
- 功能: 将目录或文件压缩成zip格式
- 常用选项:
- -r 递归处理,将指定目录下的所有文件和子目录一并处理
示例:
- 压缩文件
[root@VM-12-2-centos dir1]# ll
total 28
-rw-r--r-- 1 root root 22914 Apr 23 23:07 myfile.txt
-rw-r--r-- 1 root root 132 Apr 24 09:05 test.txt
[root@VM-12-2-centos dir1]# zip test.zip test.txt 👉压缩
adding: test.txt (deflated 67%)
[root@VM-12-2-centos dir1]# ll
total 32
-rw-r--r-- 1 root root 22914 Apr 23 23:07 myfile.txt
-rw-r--r-- 1 root root 132 Apr 24 09:05 test.txt
-rw-r--r-- 1 root root 210 Apr 24 09:13 test.zip 👉压缩后得到的文件
[root@VM-12-2-centos dir1]#
- 压缩目录
[root@VM-12-2-centos ~]# ll
total 12
drwxr-xr-x 2 root root 4096 Apr 24 09:13 dir1
drwxr-xr-x 2 root root 4096 Apr 20 15:13 dir2
drwxr-xr-x 3 root root 4096 Apr 21 11:11 dir3
[root@VM-12-2-centos ~]# zip -r dir.zip dir1 👉压缩
adding: dir1/ (stored 0%)
adding: dir1/test.zip (stored 0%)
adding: dir1/test.txt (deflated 67%)
adding: dir1/myfile.txt (deflated 90%)
[root@VM-12-2-centos ~]# ll
total 16
drwxr-xr-x 2 root root 4096 Apr 24 09:13 dir1
drwxr-xr-x 2 root root 4096 Apr 20 15:13 dir2
drwxr-xr-x 3 root root 4096 Apr 21 11:11 dir3
-rw-r--r-- 1 root root 3268 Apr 24 09:16 dir.zip 👉 压缩一个目录后得到的一个压缩文件
[root@VM-12-2-centos ~]#
20.tar指令(重要):打包/解包,不打开它,直接看内容
- 语法:
tar [-cxtzjvf] 文件与目录 ....
- 选项:
- -c :建立一个压缩文件的参数指令(create 的意思);
- -x :解开一个压缩文件的参数指令
- -t :查看 tarfile 里面的文件
- -z :是否同时具有 gzip 的属性?亦即是否需要用 gzip 压缩?
- -j :是否同时具有 bzip2 的属性?亦即是否需要用 bzip2 压缩?
- -v :压缩的过程中显示文件。这个常用,但不建议用在背景执行过程。
- -f :使用档名,请留意,在 f 之后要立即接档名,不要再加参数!
- -C : 解压到指定目录
压缩 :tar -czf dst scr
解压 :tar -xzf dst scr
示例:
[root@VM-12-2-centos dir1]# tar -czf myfile.tgz myfile.txt →压缩
[root@VM-12-2-centos dir1]# ll
total 36
-rw-r--r-- 1 root root 2550 Apr 26 10:30 myfile.tgz → 压缩结果
-rw-r--r-- 1 root root 22914 Apr 23 23:07 myfile.txt
-rw-r--r-- 1 root root 132 Apr 24 09:05 test.txt
-rw-r--r-- 1 root root 210 Apr 24 09:13 test.zip
[root@VM-12-2-centos dir1]# rm myfile.txt → 删除原被压缩的文件
rm: remove regular file ‘myfile.txt’? y
[root@VM-12-2-centos dir1]# ll
total 12
-rw-r--r-- 1 root root 2550 Apr 26 10:30 myfile.tgz
-rw-r--r-- 1 root root 132 Apr 24 09:05 test.txt
-rw-r--r-- 1 root root 210 Apr 24 09:13 test.zip
[root@VM-12-2-centos dir1]# tar -xzf myfile.tgz → 解压
[root@VM-12-2-centos dir1]# ll
total 36
-rw-r--r-- 1 root root 2550 Apr 26 10:30 myfile.tgz
-rw-r--r-- 1 root root 22914 Apr 23 23:07 myfile.txt → 解压结果
-rw-r--r-- 1 root root 132 Apr 24 09:05 test.txt
-rw-r--r-- 1 root root 210 Apr 24 09:13 test.zip
直接查看内容,不解压:tar -ztvf file
示例:
[root@VM-12-2-centos dir1]# tar -ztvf myfile.tgz
-rw-r--r-- root/root 22914 2023-04-23 23:07 myfile.txt
zip 和 tar 默认压缩/解压到当前路径
zip -d / tar -c 选项后可跟压缩到指定的路径
21.bc指令
bc命令可以很方便的进行浮点运算(相当于Linux中的计算器)
示例:
[root@VM-12-2-centos ~]# 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'.
12.3+12.3 //输入运算
24.6 //输出结果
12.4-1.2 //输入运算
11.2 //输出结果
^C
(interrupt) Exiting bc.
[root@VM-12-2-centos ~]#
22.uname –r指令:
- 语法:
uname [选项]
- 功能: uname用来获取电脑和操作系统的相关信息。
- 常用选项:
-a或–all 详细输出所有信息,依次为内核名称,主机名,内核版本号,内核版本,硬件名,处理器类型,硬件平台类型,操作系统名称
补充说明:uname可显示linux主机所用的操作系统的版本、硬件的名称等基本信息。
23.重要的几个热键[Tab],[ctrl]-c, [ctrl]-d
Tab—具有『命令补全』和『档案补齐』的功能
Ctrl+c—让当前的程序『停掉』
Ctrl+d—通常代表着:『键盘输入结束(End Of File, EOF 戒 End OfInput)』的意思;另外,也可以用来取代exit(退出登录)
↑ ↓可以查看历史指令
history(command):可以查看历史所有输入的指令
24.关机指令
- 语法:
shutdown [选项]
- 常见选项:
- -h : 将系统的服务停掉后,立即关机。
- -r : 在将系统的服务停掉之后就重新启动
- -t sec : -t 后面加秒数,亦即『过几秒后关机』的意思
(云服务器没必要关机)
END