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?



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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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