linux时间

linux有两个时间:

    系统时间和硬件时间

系统时间:


最简单的使用date命令显示当前系统的时间,命令行输入date。

# date

Thu Jun  2 17:11:48 CST 2016

date的具体使用方式:

    1. date格式显示:date [OPTION]… [+FORMAT]

参考如下参数,使用方式例如:

# date +%y

16

# date +%y%m%d

160602

# date +%y%m%d%H%M%S

160602163503

如上例子一般用在自动备份时,使备份文件的名字上加上日期和时间以便查看。其他格式使用方法类似

格式中和可以加入其他字符和符号,例如:

# date +%y-%m-%d

16-06-02

# date +%yY-%mM-%dD

16Y-06M-02D

       %%     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, space padded ( 0..23); same as %_H

       %l     hour, space padded ( 1..12); same as %_I

       %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 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)

       %:::z  numeric  time  zone  with  :  to necessary precision (e.g., -04,

              +05:30)

    2. 时间设置:date [-u|–utc|–universal] [MMDDhhmm[[CC]YY][.ss]]

先解释一下后面部分

[MMDDhhmm[[CC]YY][.ss]]

 月日时分  年     .秒

其中年和秒可以省略

例如:

date 06021712

设定时间为6月2日17点12分

date 0602171216.30 等价于date 060217122016.30

设定时间为16年6月2日17点12分30秒

硬件时间:


    首先思考一个问题,为什么linux关机后时间依然在走?电脑的主板上有一块电池为主板的晶体振荡器可保证主板上的时间在系统关闭之后依然再走,下次开机时系统时间就读取硬件时钟作为系统的时间。

   这里还需要考虑一个问题,而硬件时间和系统时间是单独走的。上面提到的date修改的是系统时间,但不能同步到硬件时间,所以关机之后系统时间失效,此时如何让硬件时间同步系统时间,这个就需要一个命令hwclock

hwlcock -w 

如果需要将硬件时间同步到系统时间也是可以的:

hwlcock -s

原创文章,作者:Net20_天意,如若转载,请注明出处:http://www.178linux.com/17378

(0)
Net20_天意Net20_天意
上一篇 2016-06-03 12:46
下一篇 2016-06-03 12:46

相关推荐

  • 马哥教育网络班第19期+第9周课程练习

    1、写一个脚本,判断当前系统上所有用户的shell是否为可登录shell(即用户的shell不是/sbin/nologin);分别这两类用户的个数;通过字符串比较来实现; 2、写一个脚本     (1) 获取当前主机的主机名,保存于hostname变量中;     (2) 判断此变量的值是否为localhost,如果是…

    Linux干货 2016-09-19
  • linux一周学习总结

          对于linux,之前也完全没有接触过,完全零基础小白。来到马哥以后,进入学习也有一周时间 ,一周里老师带我们学习了很多指令,下面,我就自己的理解和老师讲授的内容对linux中的一些指令做一个简单的小总结。 一、关于SHELL Shell     查询当前系统使用了哪个shell   &n…

    2017-07-15
  • 脚本数组及yum软件包管理器

    一、数组 变量:存储单个元素的内存空间 数组:存储多个元素的连续的内存空间,相当于多个变量的集合。 数组名和索引索引:编号从0开始,属于数值索引注意:索引可支持使用自定义的格式,而不仅是数值格式,即为关联索引,bash4.0版本之后开始支持。bash的数组支持稀疏格式(索引不连续) 1.定义数组声明数组:    declare …

    Linux干货 2016-08-25
  • 日志收集工具EFK之fluent部署手稿

    Edit EFK fluent部署安装 1. 环境介绍 Centos 6.5 64bit 2. 安装 td-agent介绍 fluent 考虑到灵活可扩展性,使用Ruby编写,部分功能考虑性能使用C语言编写。普通用户安装操作Ruby daemon还是有一定难度的。考虑到flunt的上手难度, fluent专门发布了稳定发布包,就也是所谓的td-agent. …

    Linux干货 2016-03-24
  • 马哥教育网络班第21期+第三周课程作业

    1. 列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。 who | awk  '{print $1}'|uniq 2.列出最后登录到当前系统的用户的相关信息 last | head -1   3. 读取当前系统上被用户当做其默认shell最多的那个shell cat /etc/pa…

    Linux干货 2016-07-26
  • 正则表达式与例题

    正则表达式与例题  一BRE 和ERE 由一类特殊字符及文本字符所编写的模式,其中有些字符(元字符)不表示字符字面意义,而表示控制或通配的功能 程序支持:grep ;sed ;  awk ; vim ; less ; nginx ; varnish 元字符分类:字符匹配  匹配次数  位置锚定  分组 二.基…

    Linux干货 2017-07-29