如何计算触摸 -t 的日期

如何在bash中创建日期类型?
这个日期是什么意思?'1803141400'
touch -t 1803141400 test1 test2

我该如何计算时间?

2个回答

根据touch man page的说明:
  -t STAMP
              use [[CC]YY]MMDDhhmm[.ss] instead of current time

其中:

CC: First two digit of the year
YY: Last two digits of the year
MM: Month (two-digit numeric month)
DD: Day (two-digit numeric day i.e. day of month)
hh: Hour
mm: Minutes
ss: Seconds

[] 表示该字段是可选的

在你的例子中,1803141400 的意思是:2018年3月14日,14点00分,而 touch 将根据这个值来更改 test1test2 文件的时间戳。

$ ls -l test1 test2
-rw-rw-r-- 1 yourUser yourGroup 0 mar 14  2018 test1
-rw-rw-r-- 1 yourUser yourGroup 0 mar 14  2018 test2

-t选项允许用户在使用touch命令创建文件时添加特定的最后访问时间。
其后跟随一个字符串,格式为日期、月份、年份、分钟:秒钟,并且后者使用[[CC]YY]MMDDhhmm[.ss]格式,其中CC、YY和SS是可选的。
例如,要创建带有时间戳的temp文件:
 [guru@guru-Aspire ~]$touch -t 12141105  temp
 [guru@guru-Aspire ~]$ls -l --full-time temp

  -rw-rw-r-- 1 guru guru 0 2014-12-14 11:05:00.000000000 +0530 temp

如您所见,如果您不提供CCYY,它会自动插入当前的CC和YY。
在您的情况下,即touch -t 1803141400 test1 test2
它将创建一个时间戳,
年份-2018(第一对数字)
月份-03(第二对数字)
日期-14(第三对数字)
时间-14:02(第四和第五对数字)
[guru@guru-Aspire ~]$ ls -l --full-time test*
-rw-rw-r-- 1 guru guru 0 2018-03-14 14:00:00.000000000 +0530 test1
-rw-rw-r-- 1 guru guru 0 2018-03-14 14:00:00.000000000 +0530 test2