zoukankan      html  css  js  c++  java
  • linux date命令使用

    linux 时间相关命令小结

    作者:xingxingge 发布于:2014-1-7 20:24 Tuesday 分类:Linux Shell 编辑

    简单总结下linux中时间相关命令date,hwclock,clock的用法

    1. date命令:


    (1)修改系统时间:
    格式:date MMDDHHMMYYYY.SS
    [~/shell/file_operation]#date 051023342013.30 
    2013年 05月 10日 星期五 23:34:30 CST

    [~/shell/file_operation]#date -s "2013/05/10"
    2013年 05月 10日 星期五 00:00:00 CST

    [~/shell/file_operation]#date -s "23:58:20"   
    2013年 05月 10日 星期五 23:58:20 CST

    [~/shell/file_operation]#date -s "2013/05/10 23:59:10"
    2013年 05月 10日 星期五 23:59:10 CST

    (2)查看系统时间

    把当前时间转换为时间戳格式:
    [~/shell/file_operation]#date +%s
    1368201689

    读取一个时间(-d选项),转换为时间戳:
    [~/shell/file_operation]#date  -d "2013/5/13 00:03:23" +%s
    1368374603

    把时间戳转换为时间格式:
    [~/shell/file_operation]#date -d "@1368196522"                           
    2013年 05月 10日 星期五 22:35:22 CST

    得到昨天这个时候的时间,输出时间和时间戳
    [~/shell/file_operation]#date --date="-1 days" +%y/%m/%d-%H:%M:%S--%s
    13/05/12-00:13:21--1368288801

    显示两年后的现在:
    [~/shell/file_operation]#date --date="+2 years" +%y/%m/%d-%H:%M:%S--%s      
    15/05/11-00:15:20--1431274520

    查看任意事件字符串表示的时间(格式化显示):
    [~/shell/tty]# date --date=" 2014年 01月 07日 星期二 20:19:49 CST" +%y/%m/%d-%H:%M:%S--%s   
    14/01/07-20:14:00--1389096840

    (3)文件相关时间
    读取文件最后修改时间:
    [~/shell/file_operation]#date -r 1_delete_file.sh 
    2013年 05月 10日 星期五 22:35:22 CST
    [~/shell/file_operation]#date -r 1_delete_file.sh  +%s
    1368196522

    从文件中的行读取时间:
    [~/shell/file_operation]#date -r 1_delete_file.sh  > temp
    [~/shell/file_operation]#date -f temp
    2013年 05月 11日 星期六 20:13:00 CST

    2.clock与hwclock其实是一回事(与硬件时间相关):


    [~/shell/file_operation]#clock --help
    hwclock - query and set the hardware clock (RTC)
    Usage: hwclock [function] [options...]
    Functions:
      -h | --help         show this help
      -r | --show         read hardware clock and print result(从cmos读取时间并显示)
           --set          set the rtc to the time given with --date
      -s | --hctosys      set the system time from the hardware clock(把cmos时间写入系统)


      -w | --systohc      set the hardware clock to the current system time(把系统时间写入bios)
    Options: 
      -u | --utc          the hardware clock is kept in UTC
           --localtime    the hardware clock is kept in local time
      -f | --rtc=path     special /dev/... file to use instead of default
           --directisa    access the ISA bus directly instead of /dev/rtc
           --badyear      ignore rtc's year because the bios is broken
           --date         specifies the time to which to set the hardware clock
           --epoch=year   specifies the year which is the beginning of the 
                          hardware clock's epoch value
           --noadjfile    do not access /etc/adjtime. Requires the use of
                          either --utc or --localtime
           --adjfile=path specifies the path to the adjust file (default is
                          /etc/adjtime)
           --test         do everything except actually updating the hardware
                          clock or anything else
      -D | --debug        debug mode
    linux操作系统有系统时钟和硬件时钟,系统时钟由 linux内核保持,硬件时钟由cmos维护。所以,一般使用date修改时钟后,需要使用hwclock -w命令把系统时钟写入硬件(因为硬件时间可能是不准的)。linux重启后,内核就和硬件时钟同步,从而使程序按照准确时钟运行。

    3.查看并修改时区:
    oracle[~]$date -R
    Sat, 15 Jun 2013 15:58:11 +0800
    (1)通过设置TZ环境变量实现:tzselect

    [~]#tzselect 
    Please identify a location so that time zone rules can be set correctly.
    Please select a continent or ocean.
     1) Africa
     2) Americas
     3) Antarctica
     4) Arctic Ocean
     5) Asia
     6) Atlantic Ocean
     7) Australia
     8) Europe
     9) Indian Ocean
    10) Pacific Ocean
    11) none - I want to specify the time zone using the Posix TZ format.
    #? 5
    Please select a country.
     1) Afghanistan           18) Israel                35) Palestine
     2) Armenia               19) Japan                 36) Philippines
     3) Azerbaijan            20) Jordan                37) Qatar
     4) Bahrain               21) Kazakhstan            38) Russia
     5) Bangladesh            22) Korea (North)         39) Saudi Arabia
     6) Bhutan                23) Korea (South)         40) Singapore
     7) Brunei                24) Kuwait                41) Sri Lanka
     8) Cambodia              25) Kyrgyzstan            42) Syria
     9) China                 26) Laos                  43) Taiwan
    10) Cyprus                27) Lebanon               44) Tajikistan
    11) East Timor            28) Macau                 45) Thailand
    12) Georgia               29) Malaysia              46) Turkmenistan
    13) Hong Kong             30) Mongolia              47) United Arab Emirates
    14) India                 31) Myanmar (Burma)       48) Uzbekistan
    15) Indonesia             32) Nepal                 49) Vietnam
    16) Iran                  33) Oman                  50) Yemen
    17) Iraq                  34) Pakistan
    #? 9
    Please select one of the following time zone regions.
    1) east China - Beijing, Guangdong, Shanghai, etc.
    2) Heilongjiang (except Mohe), Jilin
    3) central China - Sichuan, Yunnan, Guangxi, Shaanxi, Guizhou, etc.
    4) most of Tibet & Xinjiang
    5) west Tibet & Xinjiang
    #? 1


    The following information has been given:


            China
            east China - Beijing, Guangdong, Shanghai, etc.


    Therefore TZ='Asia/Shanghai' will be used.
    Local time is now:      Sat Jun 15 15:52:30 CST 2013.
    Universal Time is now:  Sat Jun 15 07:52:30 UTC 2013.
    Is the above information OK?
    1) Yes
    2) No
    #? 1


    You can make this change permanent for yourself by appending the line
            TZ='Asia/Shanghai'; export TZ
    to the file '.profile' in your home directory; then log out and log in again.


    Here is that TZ value again, this time on standard output so that you
    can use the /usr/bin/tzselect command in shell scripts:
    Asia/Shanghai
    永久设置,可以参照上面办法,设置环境变量,也可以复制时区文件:
    cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

    5.检查某个时区对应的时间

     

     

     

    5.脚本展示时间命令运用:


    需求:每天晚上2点执行执行脚本,删除两天前目录/u01/diag/rdbms/hx/hx/trace下的*.trm文件和*.trc文件。
    计划任务可以这样:
    [~/shell/file_operation]#crontab -l
    0  2  *  * * /root/shell/oracle.sh
    这里删除有两种情况:
    (1)严格按照两天时间(48小时)计算删除

    点击(此处)折叠或打开

    1. /root/shell/oracle.sh脚本如下:
    2. #!/bin/bash
    3. cd /u01/diag/rdbms/hx/hx/trace/
    4. NOW=$(date +%s)
    5. for FILE in $(ls *.trc *.trm)
    6.   do
    7.   FILE_CHANGE_DATE=$(date -r $FILE +%s)
    8.   if (( $NOW-$FILE_CHANGE_DATE >= 172800 ))
    9.     then
    10.     rm $FILE -rvf
    11.    fi
    12. done
    (2)按日期算,删除前两天的:

    点击(此处)折叠或打开

    1. /root/shell/oracle.sh脚本如下:
    2. #!/bin/bash
    3. cd /u01/diag/rdbms/hx/hx/trace/
    4. NOW=$(date --date="-2 hours" +%s)
    5. for FILE in $(ls *.trc *.trm)
    6.   do
    7.   FILE_CHANGE_DATE=$(date -r $FILE +%s)
    8.   if (( $NOW-$FILE_CHANGE_DATE >= 172800 ))
    9.     then
    10.     rm $FILE -rvf
    11.    fi
    12. done    
  • 相关阅读:
    20172322 《程序设计与数据结构》第六周学习总结
    20172322 2018-2019-1 10月19日课上测试报告
    20172322 《程序设计与数据结构》第五周学习总结
    20172322 《程序设计与数据结构》第四周学习总结
    172322 2018-2019-1 《Java软件结构与数据结构》实验一报告
    20172322 《程序设计与数据结构》第三周学习总结
    20172322 《程序设计与数据结构》第二周学习总结
    20172303 20172322 2017-2018-2 暑假作业 结对编程项目-舒尔特方格(增补:计时器的加入与页面优化)
    20172322 《程序设计与数据结构》第一周学习总结
    20172322 2017-2018-2《程序设计与数据结构》(上)课程总结
  • 原文地址:https://www.cnblogs.com/xingxingge/p/14861649.html
Copyright © 2011-2022 走看看