Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

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
1  
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

 
discard

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.