Calculate Date Difference Between Last Modified Date of a File and NOW Using Shell Script

datekshshellshell-script

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?

Best Answer

lastUpdate="$(stat -c %Y myLogFile.log)"
now="$(date +%s)"
let diff="${now}-${lastUpdate}"
Related Question