date命令用于显示linux系统的时间以及日期,并支持自定义格式化输入功能,在类UNIX系统中,日期被存储为一个整数,其大小为自世界标准时间(UTC)1970/1/1/0/0/0起流逝的秒数。
系统时间恢复设置:sudo ntpdate cn.pool.ntp.org
date 命令的用法date [OPTION]... [+FORMAT]常用的option选项如下:
| OPTION | DESCRIPTION |
| -d | 以指定的字符串显示时间 |
| -f | 和-d类似,只是-f后接以指定时间字符串为内容的文件 |
| -r | 查看文件的修改时间,这对文件的修改状态比较很有用 |
| -s | 以指定的字符在设定时间 |
使用实例
xusong666@ubuntu:~/doc/aa$ date Sat May 7 03:25:45 EDT 2022 #####################date -d ################### xusong666@ubuntu:~/doc/aa$ date -d '+ 2 day' Mon May 9 03:26:52 EDT 2022 xusong666@ubuntu:~/doc/aa$ date -d ' 2 day' Mon May 9 03:26:59 EDT 2022 xusong666@ubuntu:~/doc/aa$ date -d ' 2 day ago' Thu May 5 03:27:03 EDT 2022 xusong666@ubuntu:~/doc/aa$ date -d '- 2 day' Thu May 5 03:27:09 EDT 2022 ####################date -f #################### xusong666@ubuntu:~/doc/aa$ cat a.txt 2 day ago xusong666@ubuntu:~/doc/aa$ date -f a.txt Thu May 5 03:27:59 EDT 2022 xusong666@ubuntu:~/doc/aa$ ####################date -r ===================== usong666@ubuntu:~/doc/aa$ ll a.txt -rw-rw-r-- 1 xusong666 xusong666 10 May 7 03:14 a.txt xusong666@ubuntu:~/doc/aa$ date -r a.txt Sat May 7 03:14:56 EDT 2022 ###################date -s ######################## ###日期之间间隔可以用 / 或者 -,时间间隔只可以用: ##修改时间需要管理员root权限 xusong666@ubuntu:~/doc/aa$ date -s '2022/2/2 15:33:23' date: cannot set date: Operation not permitted Wed Feb 2 15:33:23 EST 2022 xusong666@ubuntu:~/doc/aa$ sudo date -s '2022/2/2 15:33:23' [sudo] password for xusong666: Wed Feb 2 15:33:23 EST 2022 xusong666@ubuntu:~/doc/aa$ date Wed Feb 2 15:33:28 EST 2022 # date -s 20001011 #单独修改日期,除了用 / - 分割,还可以不加分隔符 # date -s 10:10:11 #单独修改时间 #如果同时设定日期时间,必须用上面的那种方式 ############################################### xusong666@ubuntu:~/doc/aa$ sudo ntpdate cn.pool.ntp.org #恢复系统时间 7 May 04:08:48 ntpdate[22642]: no servers can be used, exitingFORMAT格式
| FORMAT | DESCRPTION |
| 时间格式化输出 | |
| %H | 24小时制,个位前面会补0,如:01、02、03 |
| %I | 12小时制度,个位前面会补0,如:01、02、03,搭配%p 显示am pm。 |
| %M | 分钟(00…59) |
| %S | 秒(00…61),注意与%s区别 |
| %s | 从 1970 年 1 月 1 日 00:00:00 UTC 到目前为止的秒数 |
| %Z | 显示时区 |
| %T | 以24小时制显示,时,分,秒。15:41:48 |
| %X | 以12小时制显示,时,分,秒。03:41:48 PM |
| 日期格式化输出 | |
| %Y | 完整年份 2022年显示2022 |
| %y | 年份的后两位 2022年显示 22 |
| %m | 月份 (01…12) |
| %d | 日 (01…31),注意%M显示 分钟 |
| %B | 月份英文表达,January;%b则是月份简写 Jan |
| %A | 星期几的英文表达,Monday;%a则是Mon |
| %j | 一年中的第几天 |
| %D | 直接显示日期,02/02/22;月份/日/年(后两位) |
| %x | 直接显示日期,02/02/2022;月份/日/年 |
| %c | 直接显示日期,时间,时区Mon 09 May 2022 05:26:33 PM CST |
| 特殊符号 | |
| % | 直接插入%,等价于%% |
| %t | 插入一个tab |
| %n | 插入换行符 |
FORMAT用于date时间的格式化输出,使用时需要在格式前添加一个"+",如果仅需要显示时间中的有效数字,比如原始格式是01:03:45,我们需要1:3:45,我们可以在format中间加一个"-";如果我们需要显示有效数字的同时保留无意义"0"的位置,可以再format中间加一个"_"。
################################################################# xusong666@ubuntu:~/doc/aa$ date Wed Feb 2 01:04:08 EST 2022 xusong666@ubuntu:~/doc/aa$ date '+%y-%m-%d %H:%M:%S' #format之间的字符会直接输出 22-02-02 01:04:23 xusong666@ubuntu:~/doc/aa$ date '+%y-%-m-%-d %-H:%-M:%-S' 22-2-2 1:4:29 xusong666@ubuntu:~/doc/aa$ date '+%y-%_m-%_d %_H:%_M:%_S' 22- 2- 2 1: 5:14 ################################################################touch命令查询文件时间属性
前面我们可以利用date -r 命令直接读取文件的修改时间属性,之后我们可以通过%s 格式输出UTC时间,这样方便我们比较两个文件的新旧程度。
文件的时间属性包括如下,可以通过stat命令查询:
| 属性 | 描述 |
| ATime (acess time) 文件的最近访问时间 | 读取文件就会改变这个时间 |
| MTime(modify time) 文件修改时间 | 写入文件就会修改这个时间 |
| CTime(change time)文件属性修改时间 | 当文件的目录被修改,或者文件的所有者,权限等被修改时 |
xusong666@ubuntu:~/doc/aa$ stat a.txt File: ‘a.txt’ Size: 10 Blocks: 8 IO Block: 4096 regular file Device: 801h/2049d Inode: 1975334 Links: 1 Access: (0664/-rw-rw-r--) Uid: ( 1000/xusong666) Gid: ( 1000/xusong666) Access: 2022-05-07 03:14:58.635003837 -0400 Modify: 2022-05-07 03:14:56.367003787 -0400 Change: 2022-05-07 03:14:56.367003787 -0400 Birth: -
touch不仅可以创建文件,还可以对其进行时间属性的修改。
格式:touch 选项 文件名
| 选项 | 描述 |
| -a | 改变acess time,访问时间 |
| -d | 使用指定的日期时间字符串,来修改时间属性 |
| -m | 改变修改时间 |
| -r | 使用包含特定时间字符串的文件来修改时间 |
| -t | use [[CC]YY]MMDDhhmm[.ss] 格式的时间修改时间 |
############################################################################## xusong666@ubuntu:~/doc/aa$ stat a.txt File: ‘a.txt’ Size: 10 Blocks: 8 IO Block: 4096 regular file Device: 801h/2049d Inode: 1975334 Links: 1 Access: (0664/-rw-rw-r--) Uid: ( 1000/xusong666) Gid: ( 1000/xusong666) Access: 2022-05-07 03:14:58.635003837 -0400 Modify: 2022-05-07 03:14:56.367003787 -0400 Change: 2022-05-07 03:14:56.367003787 -0400 Birth: - xusong666@ubuntu:~/doc/aa$ xusong666@ubuntu:~/doc/aa$ touch -m -d "2010-10-10 10:10:10" a.txt xusong666@ubuntu:~/doc/aa$ stat a.txt File: ‘a.txt’ Size: 10 Blocks: 8 IO Block: 4096 regular file Device: 801h/2049d Inode: 1975334 Links: 1 Access: (0664/-rw-rw-r--) Uid: ( 1000/xusong666) Gid: ( 1000/xusong666) Access: 2022-05-07 03:14:58.635003837 -0400 Modify: 2010-10-10 10:10:10.000000000 -0400 Change: 2022-02-02 01:35:28.404042035 -0500 Birth: - xusong666@ubuntu:~/doc/aa$ touch -m -t 201605201315.50 a.txt xusong666@ubuntu:~/doc/aa$ stat a.txt File: ‘a.txt’ Size: 10 Blocks: 8 IO Block: 4096 regular file Device: 801h/2049d Inode: 1975334 Links: 1 Access: (0777/-rwxrwxrwx) Uid: ( 1000/xusong666) Gid: ( 1000/xusong666) Access: 2022-05-20 13:15:00.000000000 +0800 Modify: 2016-05-20 13:15:50.000000000 +0800 Change: 2022-05-07 16:54:22.237202758 +0800 Birth: - xusong666@ubuntu:~/doc/aa$



