简单命令介绍(一)

命令列表:date、clock、timedatectl

实验环境:

[root@centos7 ~]#cat /etc/redhat-release 
CentOS Linux release 7.5.1804 (Core)

1.1 date

用途:显示和设置系统时间

语法:  

Usage: date [OPTION]... [+FORMAT]
or: date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]

常用选项:

-d:显示字符串所指的日期与时间。字符串前后必须加上双引号;

-s:根据字符串设置日期和时间。同样,字符串前后加上双引号;

-u:显示utc时间,默认date显示的是CST时间;

--help:查看帮助

常用格式:

 
%d   day of month (e.g., 01)
%F   full date; same as %Y-%m-%d
%H   hour (00..23)
%m   month (01..12)
%M   minute (00..59)
%s   seconds since 1970-01-01 00:00:00 UTC
%S   second (00..60)
%Y   year
%T   time; same as %H:%M:%S
 

实例1:以年、月、日格式显示

[root@centos7 ~ 23:40:53]$date +%F
2018-07-27
[root@centos7 ~ 23:53:12]$date +"%Y-%m-%d"
2018-07-27
[root@centos7 ~ 23:53:41]$

实例2:以小时、分钟、秒显示

[root@centos7 ~ 23:53:41]$date +%T
23:54:31
[root@centos7 ~ 23:54:31]$date +"%H:%M:%S"
23:54:50
[root@centos7 ~ 23:54:50]$ 

实例3:计算当前时间到1970-1-1(unix的诞生日)的秒,并转换

[root@centos7 ~ 23:54:50]$date +%s
1532706943
[root@centos7 ~ 23:55:43]$date -d @1532706943
Fri Jul 27 23:55:43 CST 2018
[root@centos7 ~ 23:56:28]$ 

实例4:加减操作

date -d "+1 day" +%Y%m%d   #显示明天的日期                     ##其中"+"可以省略,下同
date -d "-1 day" +%Y%m%d   #显示昨天的日期 
date -d "-1 month" +%Y%m%d #显示上一月的日期 
date -d "+1 month" +%Y%m%d #显示下一月的日期 
date -d "-1 year" +%Y%m%d  #显示前一年的日期 
date -d "+1 year" +%Y%m%d  #显示下一年的日期

1.2 clock 等同于hwclock

用途:

query or set the hardware clock (RTC)

查询或设置硬件时钟

语法:

hwclock [function] [option...]

常见选项:

-s, --hctosys: set the system time from the hardware clock
以硬件时钟为准,校正系统时钟,硬件时间覆盖系统时间。
-w, --systohc :set the hardware clock from the current system time
以系统时钟为准,校正硬件时钟,系统时间覆盖硬件时间。

实例1:硬件时间覆盖系统时间

[root@centos7 ~ 00:00:00]$date
Mon Mar 20 00:00:03 CST 2017
[root@centos7 ~ 00:00:03]$clock Sat 28 Jul 2018 12:10:51 AM CST -0.282693 second [root@centos7 ~ 00:00:11]$clock -s
[root@centos7
~ 00:11:39]$date Sat Jul 28 00:11:40 CST 2018

实例2:系统时间覆盖硬件时间

[root@centos7 ~ 08:20:32]$date
Wed Feb 22 08:20:35 CST 2012

[root@centos7 ~ 08:20:36]$clock
Sat 28 Jul 2018 12:14:40 AM CST  -0.741463 seconds

[root@centos7 ~ 08:20:40]$clock -w
[root@centos7 ~ 08:20:57]$clock
Wed 22 Feb 2012 08:20:59 AM CST  -0.196626 second

1.3 timedatectl

用途:Query or change system time and date settings.

在centos7中使用timedatectl来设置时区(在centos6中使用tzselect来设置时区)

语法:

timedatectl [OPTIONS...] COMMAND ...

常用命令:

  status                   Show current time settings
  set-time TIME            Set system time
  set-timezone ZONE        Set system time zone
  list-timezones           Show known time zones

实例:设置时区为亚洲上海

[root@centos7 ~ 08:23:50]$timedatectl                     ###查看系统时间的当前状态
      Local time: Wed 2012-02-22 08:26:48 CST
  Universal time: Wed 2012-02-22 00:26:48 UTC
        RTC time: Wed 2012-02-22 00:26:48
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: no
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a
[root@centos7 ~ 08:26:48]$timedatectl list-timezones           ###查看支持的时区
[root@centos7 ~ 08:26:48]$timedatectl set-timezone Asia/Shanghai    ###设置时区为亚洲上海

猜你喜欢

转载自www.cnblogs.com/yuanhonli-xin/p/9380389.html