linux学习之路:date命令

linux的date命令, 查看和设置系统时间

1、命令格式

  date 【选项】 时间

2、命令参数

  -d  显示STRING指定的时间

  -f  类似–date参数显示DATEFILE文件中的每行时间
    -r 显示文件的最后修改时间

      -R 查看当前时区

  -s 设置时间为STRING

3、命令功能

  显示或设置时间

4、使用范例

   实例一:通过date更改系统时间

   命令:date -s "2015-12-07 15:17:22"

[root@CentOS-study data]# date
Sun Dec  7 15:14:24 CST 2014
[root@CentOS-study data]# date -s "2015-12-07 15:17:22"
Mon Dec  7 15:17:22 CST 2015
[root@CentOS-study data]# hwclock -w 

  说明:时间信息写入bios,防止重启计算机之后,修改的时间被bios的时间覆盖

   实例二:显示pc.txt的修改时间
   命令:date -r pc.txt

[root@CentOS-study data]# date -r pc.txt
Tue Nov 24 20:16:27 CST 2015

   实例三:定时同步时间(在/etc/crontab中添加)
   命令:/usr/sbin/ntpdate 210.72.145.44 > /dev/null 2>&1

[root@CentOS-study data]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

/usr/sbin/ntpdate 210.72.145.44 > /dev/null 2>&1
# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed

[root@CentOS-study data]# 

   实例四:手动同步时间

        1.  安装ntpdate工具

             # yum -y install ntp ntpdate

        2.  设置系统时间与网络时间同步

            # ntpdate cn.pool.ntp.org

        3.  将系统时间写入硬件时间

           # hwclock --systohc

    

   

 

posted @ 2015-12-07 15:45  大白虾  阅读(213)  评论(0)    收藏  举报