I need to create a script that based on the creation time,
if the file is older then 5 minutes then execute some stuff, if not exit.
I thought to get the creation time and minutes like this.
CreationTime=$(stat -c %y /tmp/test.log | awk -F" " '{ print $1,$2 }')
Here i need to make the difference between Creation time and actual time
if [ $CreationTime -gt 5 ]
then
echo "Bad News" | /usr/sbin/sendmail -v email@mydomain.com
else
echo "Exit, everything is ok"
fi
export filename=/path/to/file/filename
tdiff=$(( `date +%s` - `stat -c %Y $filename` )) # time in seconds of "now" minus the file mod time in seconds
if [ $tdiff -ge 300 ] '; then
# 300 seconds = five minutes, change to 360 or another number to suit your idea of 'five minutes old' in seconds
echo "oops $filename is older than five minutes"
fi
Normally stat stores last accessed time, last modified time and last changed time , but not the creation time. stat -c %Y returns last modified. Maybe it was done because of the meaning and actual use for this value. If creating a copy, should it include creation time? If restoring a file from backup, what should be the value used for the restored file? If creation time is going to be critical to whatever use then a storing system like indexing system, or version control system, etc. needs to be implemented. Not sure if your operating system would want to take such a performance hit just for getting creation time value.