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.
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.
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