Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?


share|improve this question
up vote 6 down vote accepted

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
share|improve this answer
You don't need bc for that: echo $(( ( $(date +%s) - $(date -r file +%s) ) / 3600 / 24 )) – Mat Dec 30 '11 at 6:55
@Mat: I know, but a fraction of day could be important. – enzotib Dec 30 '11 at 7:06

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.