Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 清除文件而不更改其时间戳_Linux_File_Timestamp - Fatal编程技术网

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

Linux 清除文件而不更改其时间戳,linux,file,timestamp,Linux,File,Timestamp,是否可以使用标准Linux命令清除保留时间戳的文件?例如: echo”“>文件名 将文本文件转换为空,这对我来说没问题。但是我需要保持时间戳不变 是一篇好文章。希望能有所帮助 添加: 抱歉,请阅读,您需要的是零文件,而不是副本Touch可以创建零个带有所需时间戳的文件 范例 To set the date to 7:30 am 1st October 2015 touch /t 2015 10 01 07 30 00 MyFile.txt 您可以使用触摸屏执行以下操作: #!/bi

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

echo”“>文件名

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

是一篇好文章。希望能有所帮助

添加:

抱歉,请阅读,您需要的是零文件,而不是副本Touch可以创建零个带有所需时间戳的文件

范例

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

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

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

通过使用date记录时间戳字符串并将其传递回touch,可以跳过tmp文件

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

要补充mmond的回答(由于声誉不足,无法发表评论),请不要忘记本地化。要处理任何本地化问题,答案应为:

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

虽然我仍然认为不写入文件系统更干净、更快,但-r标志并不像我想象的那样普遍。例如,OSX Lion的达尔文shell不支持它。