更新答案

更新答案

這聽起來是一個很容易研究的問題。事實並非如此。我正在關注我偶然遇到的 Ubuntu Stack Exchange 網站上的以下強烈推薦的帖子。但該建議在 Red Hat Enterprise Linux ES 版本 4 上不起作用。

這是建議: 具體來說,海報推薦

如果您想相對於其現有修改時間修改文件,則可以執行以下操作:

touch -d "$(date -R -r 檔名) - 2 小時" 檔名

這在 Redhat 下對我不起作用。減號被忽略,時間提前兩天設置,就像我輸入的一樣

touch -d "$(date -R -r filename) + 2 hours" filename

例如:

$ ls -al test
-rw-r-----  1 sc1478 dev 5 Oct 27 12:59 test

$ touch -d "$(date -R -r test) - 8 days" test

$ ls -al test
-rw-r-----  1 sc1478 dev 5 Nov  4  2016 test

$ touch -d "$(date -R -r test) + 8 days" test

$ ls -al test 
-rw-r-----  1 sc1478 dev 5 Nov 12  2016 test

無論我使用減號還是加號,日期都會向前調整。

這是某些版本的 touch 中的錯誤嗎?

是否有另一種方法來調整文件相對於其當前時間戳記的時間戳記?

答案1

更新答案

我偶然發現了這顆寶石

touch -r filename -d '+8 days' filename

來自info coreutils touch invocation(謝謝@don_crissti):

'-r 檔'

'--reference=文件'

 Use the times of the reference FILE instead of the current time.
 If this option is combined with the '--date=TIME' ('-d TIME')
 option, the reference FILE's time is the origin for any relative
 TIMEs given, but is otherwise ignored.  For example, '-r foo -d
 '-5 seconds'' specifies a time stamp equal to five seconds before
 the corresponding time stamp for 'foo'.  If FILE is a symbolic
 link, the reference timestamp is taken from the target of the
 symlink, unless '-h' was also in effect.

如果你想要變數擴展,你可以-d像這樣在參數周圍使用軟引號。

DAYS=8
touch -r filename -d "+${DAYS} days" filename

樣本:

$ ls -l foo.bar
-rw-r--r-- 1 usr usr 69414810 Nov 10  2016 foo.bar
$ TEST=100
$ touch -r foo.bar -d "+${TEST} days" foo.bar
$ ls -l foo.bar 
-rw-r--r-- 1 usr usr 69414810 Feb 24  2017 foo.bar

相關內容