如何使用带有 date 命令的文件引用来获取上一个日期?

Rah*_*til 4 date

我正在尝试获取参考文件的上一个日期。

我试过的:

[rahul@testsrv]$ date +%F -r /tmp/ftpbkp.log
2013-08-27

[rahul@testsrv]$ date +%F -r /tmp/ftpbkp.log -d "1 day ago"
date: the options to specify dates for printing are mutually exclusive
Try `date --help' for more information.
Run Code Online (Sandbox Code Playgroud)

Sté*_*las 5

警告说明:

$ date -r ~/a
Sun 28 Oct 23:12:00 GMT 2012
$ LC_ALL=C date -r ~/a
Sun Oct 28 23:12:00 GMT 2012
Run Code Online (Sandbox Code Playgroud)

作为输出,date以用户的本地格式输出日期。尽管-dGNU 的输入对date格式更加挑剔:

$ date -d "$(date -r ~/a) - 1 day"
date: invalid date ‘Sun 28 Oct 23:12:00 GMT 2012 - 1 day’
Run Code Online (Sandbox Code Playgroud)

将语言环境修复为 C 可以解决该问题:

$ export LC_ALL=C
$ date -d "$(date -r ~/a) - 1 day"
Sun Oct 28 00:12:00 BST 2012
Run Code Online (Sandbox Code Playgroud)

但请注意日期仍然是 2012-10-28,尽管现在是夏令时。那是因为在英国的那个日期前 24 小时,我们还是同一天。

现在,如果你想要前一天,你必须写:

date -d "$(date -r /tmp/file.ref +'%F -1 day')" +%F
Run Code Online (Sandbox Code Playgroud)