Linux系统中date命令

1、基本用法,直接运行date 显示系统当前时间

[root@linuxprobe test]# date
Mon Oct  5 20:03:00 CST 2020

2、以特定格式输出,如“年-月-日 时:分:秒”

[root@linuxprobe test]# date "+%Y-%m-%d %H:%M:%S"
2020-10-05 20:06:21

3、以date输出的日期记录文件名

[root@linuxprobe test]# dd if=/dev/zero bs=1024 count=100000 of=test.txt ##创建测试文件
100000+0 records in
100000+0 records out
102400000 bytes (102 MB, 98 MiB) copied, 0.16962 s, 604 MB/s
[root@linuxprobe test]# ll -h   ## 查看测试文件 
total 98M
-rw-r--r--. 1 root root 98M Oct  5 20:11 test.txt
[root@linuxprobe test]# zip `date "+%Y-%m-%d-%H-%M-%S"`.zip test.txt  ## 以当前日期命名压缩文件
  adding: test.txt (deflated 100%)
[root@linuxprobe test]# ll -h   ## 查看压缩结果
total 98M
-rw-r--r--. 1 root root 98K Oct  5 20:11 2020-10-05-20-11-25.zip
-rw-r--r--. 1 root root 98M Oct  5 20:11 test.txt
[root@linuxprobe test]# tar -czvf `date "+%Y-%m-%d-%H-%M-%S"`.tar.gz *   ## 以当前日期命名打包压缩文件
2020-10-05-20-11-25.zip
test.txt
[root@linuxprobe test]# ll -h  ## 查看打包压缩文件
total 98M
-rw-r--r--. 1 root root 98K Oct  5 20:11 2020-10-05-20-11-25.zip
-rw-r--r--. 1 root root 98K Oct  5 20:12 2020-10-05-20-12-36.tar.gz
-rw-r--r--. 1 root root 98M Oct  5 20:11 test.txt

4、date -s 设置系统时间

[root@linuxprobe test]# ls
[root@linuxprobe test]# touch a.txt
[root@linuxprobe test]# ll -h ## 查看文件修改时间
total 0
-rw-r--r--. 1 root root 0 Oct  5 20:17 a.txt
[root@linuxprobe test]# date "+%Y-%m-%d %H:%M:%S"  ## 查看系统当前时间 
2020-10-05 20:18:59
[root@linuxprobe test]# date -s "20200101 1:00:00" ## 设定系统时间
Wed Jan  1 01:00:00 CST 2020
[root@linuxprobe test]# ll -h  ## 查看a.txt 时间变化
total 0
-rw-r--r--. 1 root root 0 Oct  5  2020 a.txt
[root@linuxprobe test]# date "+%Y-%m-%d %H:%M:%S"  ## 查看系统当前时间
2020-01-01 01:00:12
[root@linuxprobe test]# touch b.txt  
[root@linuxprobe test]# ll -h  ## 查看b.txt时间变化
total 0
-rw-r--r--. 1 root root 0 Oct  5  2020 a.txt
-rw-r--r--. 1 root root 0 Jan  1 01:00 b.txt

  5、记录程序运行时间

[root@linuxprobe test]# dd if=/dev/zero bs=1024 count=6000000 of=test.txt ##创建测试数据
6000000+0 records in
6000000+0 records out
6144000000 bytes (6.1 GB, 5.7 GiB) copied, 47.6395 s, 129 MB/s
[root@linuxprobe test]# ll -h  ## 查看大小
total 5.8G
-rw-r--r--. 1 root root 5.8G Jan  1 01:07 test.txt
[root@linuxprobe test]# vim timetest.sh  ## 编辑测试脚本  
[root@linuxprobe test]# cat timetest.sh
#!/bin/bash
start=$(date +%s)
cp test.txt test2.txt
end=$(date +%s)
time=$(( end - start ))
echo "$time S used!" |tee $0time.log
[root@linuxprobe test]# bash timetest.sh  ## 运行程序
34 S used!
[root@linuxprobe test]# cat timetest.shtime.log
34 S used!
## %s 总秒数。起算时间为1970-01-01 00:00:00 UTC

参考:https://www.cnblogs.com/asxe/p/9317811.html
原文地址:https://www.cnblogs.com/liujiaxin2018/p/13771464.html