How to get time since file was last modified in seconds with bash?

There is no simple command to get the time in seconds since a file was modified, but you can compute it from two pieces:

  • date +%s: the current time in seconds since the Epoch
  • date -r path/to/file +%s: the last modification time of the specified file in seconds since the Epoch

Use these values, you can apply simple Bash arithmetic:

lastModificationSeconds=$(date -r path/to/file +%s)
currentSeconds=$(date +%s)
((elapsedSeconds = currentSeconds - lastModificationSeconds))

You could also compute and print the elapsed seconds directly without temporary variables:

echo $(($(date +%s) - $(date -r path/to/file +%s)))

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)