I am trying to calculate the time elapsed since the log file was last updated.
I guess following commands will be used
lastUpdate=$(date -r myLogFile.log)
now=$(date)
How can I subtract them and get result for number of seconds elapsed?
lastUpdate="$(stat -c %Y myLogFile.log)"
now="$(date +%s)"
let diff="${now}-${lastUpdate}"
compare the two outputs to get the number of seconds between now and the modified date
stat -c %Y file vs date +%s
You're almost there! Just tell date to use a format on which computation is easy.
lastUpdate=$(date -r myLogFile.log +%s)
now=$(date +%s)
file_age=$((now - lastUpdate))
One liner:
stat -c %Y /path/to/file | echo `expr $(date +%s) - $(cat)`
+%s(seconds from EPOC) ofdateoutput formatting option.