【liunx】date命令总结
命令简介:
date 根据给定格式显示日期或设置系统日期时间。print or set the system date and time
指令所在路径:/bin/date
命令语法:
date [OPTION]... [+FORMAT]
date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
命令参数:
|
参数 |
描述 |
|
-d |
显示字符串描述的时间 |
|
-f |
显示DATEFILE文件中的每行时间 |
|
-r |
显示文件的最后修改时间 |
|
-R |
以RFC-2822兼容日期格式显示时间 |
|
-rfc-2822 |
以RFC-2822兼容日期格式显示时间 |
|
-s |
设置时间为string |
|
-u |
显示或设定为Coordinated Universal Time时间格式 |
|
--help |
显示date命令的帮助信息 |
|
--version |
显示date命令的版本信息 |
Format参数格式
要说写这位程序的 David MacKenzie老兄,真是事无巨细啊,居然整了这么多格式参数,佩服佩服。
|
参数 |
描述 |
|
%% |
显示字符% |
|
%a |
星期几的缩写(Sun..Sat) |
|
%A |
星期几的完整名称(Sunday...Saturday) |
|
%b |
月份的缩写(Jan..Dec) |
|
%B |
月份的完整名称(January..December) |
|
%c |
日期与时间。只输入date指令也会显示同样的结果 |
|
%C |
世纪(年份除100后去整) [00-99] |
|
%d |
日期(以01-31来表示)。 |
|
%D |
日期(含年月日)。 |
|
%e |
一个月的第几天 ( 1..31) |
|
%F |
日期,同%Y-%m-%d |
|
%g |
年份(yy) |
|
%G |
年份(yyyy) |
|
%h |
同%b |
|
%H |
小时(00..23) |
|
%I |
小时(01..12) |
|
%j |
一年的第几天(001..366) |
|
%k |
小时( 0..23) |
|
%l |
小时( 1..12) |
|
%m |
月份(01..12) |
|
%M |
分钟(00..59) |
|
%n |
换行 |
|
%N |
纳秒(000000000..999999999) |
|
%p |
AM or PM |
|
%P |
am or pm |
|
%r |
12小时制时间(hh:mm:ss [AP]M) |
|
%R |
24小时制时间(hh:mm) |
|
%s |
从00:00:00 1970-01-01 UTC开始的秒数 |
|
%S |
秒(00..60) |
|
%t |
制表符 |
|
%T |
24小时制时间(hh:mm:ss) |
|
%u |
一周的第几天(1..7); 1 表示星期一 |
|
%U |
一年的第几周,周日为每周的第一天(00..53) |
|
%V |
一年的第几周,周一为每周的第一天 (01..53) |
|
%w |
一周的第几天 (0..6); 0 代表周日 |
|
%W |
一年的第几周,周一为每周的第一天(00..53) |
|
%x |
日期(mm/dd/yy) |
|
%X |
时间(%H:%M:%S) |
|
%y |
年份(00..99) |
|
%Y |
年份 (1970…) |
|
%z |
RFC-2822 风格数字格式时区(-0500) |
|
%Z |
时区(e.g., EDT), 无法确定时区则为空 |
2:运用-d参数
|
1
2
3
4
5
6
7
8
|
[root@Gin scripts]# dateSun Jan 29 10:46:03 CST 2017[root@Gin scripts]# date -d nowSun Jan 29 10:46:07 CST 2017[root@Gin scripts]# date -d 'next monday'Mon Jan 30 00:00:00 CST 2017[root@Gin scripts]# date -d yesterday +%Y%m%d20170128 |
3:显示文件中的时间
|
1
2
3
4
5
6
7
8
9
10
11
|
[root@Gin scripts]# cat >date.txt2013-11-17 10:54:002013-11-22 11:11:11[root@Gin scripts]# more date.txt2013-11-17 10:54:002013-11-22 11:11:11[root@Gin scripts]# date -f date.txtSun Nov 17 10:54:00 CST 2013Fri Nov 22 11:11:11 CST 2013 |
4:显示文件最后修改的时间
|
1
2
|
[root@Gin scripts]# date -r date.txtSun Jan 29 10:49:11 CST 2017 |
5:按各种格式显示当前日期时间
这个命令的格式参数实在是太多了,在此没有必要每一个参数都尝试一遍,大家可以对照Format参数表,自己敲一敲命令实践一下。
|
1
2
3
4
5
6
7
8
9
10
11
12
|
[root@Gin scripts]# date +%Y2017[root@Gin scripts]# date +%m01[root@Gin scripts]# date +%D01/29/17[root@Gin scripts]# date '+%Y-%m-%d'2017-01-29[root@Gin scripts]# date +%Y-%m-%d2017-01-29[root@Gin scripts]# date +%m/%d/%y01/29/17 |
6: 设置系统时间
|
1
2
3
4
|
[root@Gin scripts]# date -s "2016-11-11 00:00:00"Fri Nov 11 00:00:00 CST 2016[root@Gin scripts]# dateFri Nov 11 00:00:05 CST 2016 |
7:请给出如下格式的date命令,如:11-02-26。再给出实现按周输出,如:周六为6,请分别给出命令
|
1
2
3
4
5
6
|
[root@Gin scripts]# date +%y-%m-%d17-01-29[root@Gin scripts]# date +%F2017-01-29[root@Gin scripts]# date "+%Y-%m-%d %H:%M:%S"2017-01-29 10:57:02 |
生产场景中常用时间格式来打包数据:
|
1
2
|
[root@Andy andy]# tar zcvf etc-$(date +%F).tar.gz /etc[root@Andy andy]# tar zcvf etc-`date +%F`.tar.gz /etc |
解析命令方法:反引号,或$(),如上
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[root@Andy andy]# date +%w ##显示周,0-63显示过去与未来时间:[root@Andy andy]# date +%F2016-11-16[root@Andy andy]# date +%F -d "-1day"2016-11-15[root@Andy andy]# date +%F -d "+2day"2016-11-18[root@Andy andy]# date +%F -d "+24hour"2016-11-17[root@Andy andy]# date +%F-%H -d "+2hour" 2016-11-16-14
|
浙公网安备 33010602011771号