2017年1月30日 星期一

Linux 時間管理常用指令收集

1. date

[說明]

查看或設定時間。


[範例]

查看時間

cy@cyvm:~$ date
公曆 20十七年 一月 卅日 週一 十六時47分廿四秒

查看時間,顯示成英文格式

cy@cyvm:~$ LANG=en_US ; date
Mon Jan 30 16:53:18 CST 2017

cy@cyvm:~$ LANG=en ; date
Mon Jan 30 16:53:31 CST 2017

查看時間,格式化顯示時間

cy@cyvm:~$ date +"%Y-%m-%d %H:%M:%S"
2017-01-30 17:05:53
cy@cyvm:~$ date +"%s"
1485767170

  • 一般常用年月日時分秒組合數字格式
    %Ffull date; same as %Y-%m-%d
    %Ttime; same as %H:%M:%S
    %Yyear
    %mmonth (01..12)
    %dday of month (e.g., 01)
    %Hhour (00..23)
    %Mminute (00..59)
    %Ssecond (00..60)
    %Nnanoseconds (000000000..999999999)

  • 月份文字
    %Blocale's full month name (e.g., January)
    %blocale's abbreviated month name (e.g., Jan)
    %hsame as %b

  • 星期文字
    %Alocale's full weekday name (e.g., Sunday)
    %alocale's abbreviated weekday name (e.g., Sun)

  • 時區
    %z+hhmm numeric time zone (e.g., -0400)
    %:z+hh:mm numeric time zone (e.g., -04:00)
    %::z+hh:mm:ss numeric time zone (e.g., -04:00:00)
    %:::znumeric time zone with : to necessary precision (e.g., -04, +05:30)
    %Zalphabetic time zone abbreviation (e.g., EDT)

  • UNIX Timestamp (Epoch time)
    %sseconds since 1970-01-01 00:00:00 UTC

  • 年或週的週期日數
    %jday of year (001..366)
    %uday of week (1..7); 1 is Monday

  • 週數
    %Uweek number of year, with Sunday as first day of week (00..53)
    %Wweek number of year, with Monday as first day of week (00..53)

  • ISO week number
    %Gyear of ISO week number (see %V); normally useful only with %V
    %glast two digits of year of ISO week number (see %G)
    %VISO week number, with Monday as first day of week (01..53)

  • 世紀
    %Ccentury; like %Y, except omit last two digits (e.g., 20)
    %ylast two digits of year (00..99)

  • 12小時制
    %Ihour (01..12)
    %plocale's equivalent of either AM or PM; blank if not known
    %Plike %p, but lower case

  • 地區限定格式
    %clocale's date and time (e.g., Thu Mar 3 23:05:25 2005)
    %xlocale's date representation (e.g., 12/31/99)
    %Xlocale's time representation (e.g., 23:13:48)
    %rlocale's 12-hour clock time (e.g., 11:11:04 PM)

  • 特殊文字
    %%a literal %
    %na newline
    %ta tab

  • 不常用或有其他替代方案的格式
    %Ddate; same as %m/%d/%y
    %R24-hour hour and minute; same as %H:%M
    %eday of month, space padded; same as %_d
    %khour, space padded ( 0..23); same as %_H
    %lhour, space padded ( 1..12); same as %_I
    %wday of week (0..6); 0 is Sunday


設定時間

*注意!操作此需要 root 權限。

cy@cyvm:~$ sudo date +"%Y-%m-%d %H:%M:%S" -s "2016-10-05 12:24:36"
2016-10-05 12:24:36



2. ntpdate

[說明]

經由 NTP (Network Time Protocol,RFC 1305),設定系統時間。


[範例]

設定時間

*注意!操作此需要 root 權限。

cy@cyvm:~$ sudo ntpdate -s time.stdtime.gov.tw



3. hwclock

[說明]

在 Linux 下操作時間,一般都只是寫入作業系統,若要同步寫入硬體(RTC, real-time clock),需要使用此指令。


[範例]

查看時間

cy@cyvm:~$ sudo hwclock -r
Mon Jan 30 05:35:39 2017  .262043 seconds

設定時間

cy@cyvm:~$ sudo hwclock -w

沒有留言:

張貼留言