清除文件而不更改其时间戳

Ale*_*x F 3 linux timestamp file

是否可以使用标准Linux命令清除保留其时间戳的文件?例如:

echo"">文件名

将文本文件转换为空,这对我来说没问题.但我需要保持时间戳不变.

Has*_*kun 7

您可以使用触摸执行以下操作:

#!/bin/sh
TMPFILE=`mktemp`
#save the timestamp
touch -r file-name $TMPFILE
> file_name
#restore the timestamp after truncation
touch -r $TMPFILE file-name
rm $TMPFILE
Run Code Online (Sandbox Code Playgroud)