views:

27

answers:

2

Is it possible to clear a file preserving its timestamp, using standard Linux commands? For example:

echo "" > file-name

converts the text file to empty, this is OK for me. But I need to keep the timestamp unchanged.

+1  A: 

Here is the nice article. Hope it help.

ADDED:

Sorry, just read, that you need the zero file, not the copy. Touch could create zero files with needed time stamps.

Example

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

You can do the following using touch:

#!/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
Hasturkun
Great! Thank you.
Alex Farber