如何在日常实践中使用date命令
在本文中,我们将学习Linux中的“date”命令,以及如何在日常使用中结合一些实际示例来使用“date”命令。“date”命令用于打印或更改系统日期和时间。
通用语法
[root@localhost ~]# date [OPTION]... [+FORMAT] [root@localhost ~]# date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
Date命令有以下用法
我们可以打印系统上的日期和时间
我们可以以给定的格式显示日期和时间。
我们可以从文件中读取日期
我们可以显示世界标准时间。
我们可以设置系统日期和时间。
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
从给定字符串中显示日期和时间。
以下命令示例将接收一个输入,并使用“-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命令和更多选项,我们可以找出创建文件的日期和时间,以及日志文件或不同格式的文件中的日期和时间。但是,我们可以根据需要将日期和时间从一个时区转换为另一个时区。
广告