pistach
pistach

Reputation: 401

calculate how long ago a file was modified

I need a script that calculates how long ago a file was modified. This can be days ago or just minutes ago. I already was able to get the modification date and time from the stat-command. I stored this data in two different variables. Now I need to calculate the difference between this date and time with "now". I can find lots of examples of getting the difference between two dates or time. But what if my time goes back more then a day. ex. modif date 2013-06-25 23:55:00 now 2013-06-26 00:10:08 If I calculate the difference between these two I need the answer 15 minutes and 8 seconds. Can anybody help me?

Upvotes: 1

Views: 1624

Answers (2)

ioscode
ioscode

Reputation: 821

Jens answer works, here's an example.

#!/bin/bash

MOD_TIME=`stat -c %Y thisfile.txt`
RIGHTNOW=`date +%s`
HOW_LONG=`expr $RIGHTNOW - $MOD_TIME`
NUM_MINS=`expr $HOW_LONG / 60`
NUM_SECS=`expr $HOW_LONG % 60`

echo "$NUM_MINS minutes, $NUM_SECS seconds since modified."

Upvotes: 3

Jens
Jens

Reputation: 72619

Make stat give you the times in seconds, then subtract the values. Divide by 60 if you want minutes, use modulo 60 to get the remainder in seconds.

Upvotes: 1

Related Questions