回答の更新

回答の更新

これは調査しやすい質問のように聞こえます。しかし、実際にはそうではありません。私は私が出会ったUbuntu Stack Exchangeのウェブサイトで次の強力な推薦記事をフォローしていました。ただし、この提案はRed Hat Enterprise Linux ESバージョン4では機能しません。私はそれが動作することを望んでいましたが、以下の説明のように失敗しました。

これは提案:具体的にはポスターおすすめ

既存の変更時間に基づいてファイルを変更するには、次のようにします。

touch -d "$(date -R -r ファイル名) - 2 時間" ファイル名

これはRedhatでは動作しません。マイナス記号は無視され、私が入力したように時間が2日前に設定されます。

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

マイナス記号を使用してもプラス記号を使用しても、日付は将来調整されます。

これはいくつかのタッチバージョンのバグですか?

現在のタイムスタンプに基づいてファイルのタイムスタンプを調整する他の方法はありますか?

ベストアンサー1

回答の更新

私はこの宝石を偶然見つけました。

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

送信者info coreutils touch invocation(@don_crisstiに感謝):

'-r ファイル'

'--参照=ファイル'

 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

おすすめ記事