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

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

echo“”>文件名

将文本文件转换为空,这对我来说可以。 但是我需要保持时间戳不变。

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

#!/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 

您可以通过使用日期来跳过tmp文件来记录时间戳字符串并将其传回。

 #!/bin/sh # Save the timestamp STAMP=`date -r file_name` > file_name # Restore the timestamp touch -d "$STAMP" file_name 

这是好文章。 希望它有帮助。

添加:

对不起,刚刚阅读,你需要零文件,而不是副本。 触摸可以创建零文件与所需的时间戳。

  To set the date to 7:30 am 1st October 2015 touch /t 2015 10 01 07 30 00 MyFile.txt 

为了补充mmond的答案(由于名誉不足而无法评论),请不要忘记本地化。 要使用任何本地化,答案应该是:

 #!/bin/sh # Save the timestamp STAMP=`LANG= date -r file_name` > file_name # Restore the timestamp touch -d "$STAMP" file_name