如何在日常實踐中使用date命令


在本文中,我們將學習Linux中的“date”命令,以及如何在日常使用中結合一些實際示例來使用“date”命令。“date”命令用於列印或更改系統日期和時間。

通用語法

[root@localhost ~]# date [OPTION]... [+FORMAT]
[root@localhost ~]# date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]

Date命令有以下用法

  • 我們可以列印系統上的日期和時間

  • 我們可以以給定的格式顯示日期和時間。

  • 我們可以從檔案中讀取日期

  • 我們可以顯示世界標準時間。

  • 我們可以設定系統日期和時間。

從給定字串中顯示日期和時間。

以下命令示例將接收一個輸入,並使用“-date”選項顯示給定的字串。

[root@localhost ~]# date --date=”6/15/2016”
Wed Jun 15 15:36:30 IST 2016

使用“-file”選項從檔案讀取日期。此命令將讀取檔案內容中的日期並顯示日期。例如,如果我們有一個日誌檔案,並且想要顯示日誌檔案中的日期,我們可以使用此命令。

此外,我們可以建立一個檔案並在該檔案中保留一些日期,以便此命令讀取該檔案並顯示檔案中作為輸出的日期。

我建立了一個包含以下資料的檔案:

[root@localhost ~]# vi samplelog.txt
File contents
04 jun 1978
03 aug 1980
24 feb 2004
22 dec 2007
[root@localhost ~]# date --file=samplelog.txt
The output will be
Sat Jun 24 00:00:00 IST 1978
Sat Aug 30 00:00:00 IST 1980
Sat Feb 14 00:00:00 IST 2004
Tue Dec 11 00:00:00 IST 2007

顯示過去日期

以下命令和選項將用於列印過去的日期。例如,如果我們想要昨天或一個月前或一年前的日期。

[root@localhost ~]# date --date='1 day ago'
Tue Jun 14 16:01:31 IST 2016
[root@localhost ~]# date --date='yesterday'
Tue Jun 14 16:03:01 IST 2016
[root@localhost ~]# date --date='1 month ago'
Sun May 15 16:03:35 IST 2016
[root@localhost ~]# date --date='1 year ago'
Mon Jun 15 16:03:57 IST 2015

使用“-s”選項更改系統日期和時間

“-s”是date命令用於設定日期和時間的選項。

$ date -s "Tue June 15 04:08:37 IST 2016"
Wed Jun 15 04:08:37 IST 2016

使用“-u”選項列印世界標準時間

這稱為世界標準時間(UT),它類似於一個標準,基於地球自轉——格林威治標準時間(GMT)。

[root@localhost ~]# date
Wed Jun 15 04:10:29 IST 2016
[root@localhost ~]# date -u
Tue Jun 14 22:40:31 UTC 2016

使用“-r”選項顯示檔案修改時間戳

我們還可以使用date命令和“-r”選項驗證檔案修改的日期

[root@localhost ~]# date -r samplelog.txt
Wed Jun 15 15:45:37 IST 2016

將給定的日期和時間轉換為我們的時區

我們可以使用date命令和“-d”選項將日期轉換為我們的或當前時區。

[root@localhost ~]# date -d '2016-05-20 18:00 CST'
Sat May 21 05:30:00 IST 2016

您可以使用date命令檢查的其他選項列在下面

FORMAT控制輸出。解釋的序列是:

%%    a literal %
%a    locale's abbreviated weekday name (e.g., Sun)
%A    locale's full weekday name (e.g., Sunday)
%b    locale's abbreviated month name (e.g., Jan)
%B    locale's full month name (e.g., January)
%c    locale's date and time (e.g., Thu Mar 3 23:05:25 2005)
%C    century; like %Y, except omit last two digits (e.g., 20)
%d    day of month (e.g, 01)
%D    date; same as %m/%d/%y
%e    day of month, space padded; same as %_d
%F    full date; same as %Y-%m-%d
%g    last two digits of year of ISO week number (see %G)
%G    year of ISO week number (see %V); normally useful only with %V
%h    same as %b
%H    hour (00..23)
%I    hour (01..12)
%j    day of year (001..366)
%k    hour ( 0..23)
%l    hour ( 1..12)
%m    month (01..12)
%M    minute (00..59)
%n    a newline
%N    nanoseconds (000000000..999999999)
%p    locale's equivalent of either AM or PM; blank if not known
%P    like %p, but lower case
%r    locale's 12-hour clock time (e.g., 11:11:04 PM)
%R    24-hour hour and minute; same as %H:%M
%s    seconds since 1970-01-01 00:00:00 UTC
%S    second (00..60)
%t    a tab
%T    time; same as %H:%M:%S
%u    day of week (1..7); 1 is Monday
%U    week number of year, with Sunday as first day of week (00..53)
%V    ISO week number, with Monday as first day of week (01..53)
%w    day of week (0..6); 0 is Sunday
%W    week number of year, with Monday as first day of week (00..53)
%x    locale's date representation (e.g., 12/31/99)
%X    locale's time representation (e.g., 23:13:48)
%y    last two digits of year (00..99)
%Y    year
%z    +hhmm numeric timezone (e.g., -0400)
%:z   +hh:mm numeric timezone (e.g., -04:00)
%::z  +hh:mm:ss numeric time zone (e.g., -04:00:00)
By default, date pads numeric fields with zeroes.
The following optional flags may follow `%':
-    (hyphen) do not pad the field
_    (underscore) pad with spaces
0    (zero) pad with zeros
^    use upper case if possible
#    use opposite case if possible
After any flags comes an optional field width, as a decimal number; then an optional modifier, which is either
E to use the locale's alternate representations if available, or O to use the locale's alternate numeric symbols if available.

透過使用date命令和更多選項,我們可以找出建立檔案的日期和時間,以及日誌檔案或不同格式的檔案中的日期和時間。但是,我們可以根據需要將日期和時間從一個時區轉換為另一個時區。

更新於:2019年10月18日

239 次檢視

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.