Bash – How to get the difference between two timestamps

bashscriptingtimestamps

I am creating a simple backup script. I would like to have a message saying "You last backed up x days ago" on login. I can create a file when I do the backup "backup/.last_backup_timestamp" but what format should I use and how would I find the difference in days between then and now in bash?

Thank-you

Best Answer

You can use the date utility to write the timestamp of a file, and the current time, as seconds elapsed from the Epoch, then format a string to convert the seconds of the difference in days with bc:

echo "scale=2; ($(date +%s)-$(date -r file +%s)) / (3600 * 24)" \
  | bc
Related Question