echo "hi"

DATE=`date +%Y-%m-%d`
for filename in $path/*.*; do
    modDate=$(stat -c %y  "$filename") 
    #modDate=$(date -r   "$filename"+%s) 
    modDate=${modDate%%  *}
    echo $filename:$modDate

    #lastUpdate=$(stat -c %y "$filename")
    now="$(date +%s)"

echo $DATE 

2 Answers 2


This is close to a repost of this question. Adapting their script to your format gives us this set of commands:

echo "$(( $(date -d "$d2" +%s) - $(date -d "$d1" +%s) )) / 86400" | bc -l

where $d1 is the smaller (earlier date) and $d2 is the larger (later) date.

So, as far as I can tell, this should do the trick:

echo "$(( $(date -d "$modDate" +%s) - $(date -d "$now" +%s) )) / 86400" | bc -l

To clarify,

bc -l

Is, according to its manpage,

...a language that supports arbitrary precision numbers with interactive execution of statements. There are some similarities in the syntax to the C programming language.

It allows you to get decimal numbers in your answer, as most shells only support integer division.


I'm not sure what you're trying to modify here, as far as I know, stat -c only works on GNU stat, and stat -c %y gives the output in the format 2019-03-14 14:21:32.704211521 +0200. You're trying to remove anything after a double space, but there isn't one.

In any case, if you want to do arithmetic on the timestamp, it's better to just take it as seconds since the Epoch, i.e. -c %Y, not -c %y. Then, to get the difference (in seconds), you can use arithmetic expansion $(( .. )) in the shell:

$ diff=$(( "$(date +%s)" - "$(stat -c %Y "$file")" ))
$ echo $diff

To get that that as hours, minutes and seconds, just make the appropriate divisions and take the remainders:

$ s=$(( diff % 60 )); m=$(( diff / 60 % 60 )); h=$(( diff / 3600 ))
$ printf "%d:%02d:%02d\n" "$h" "$m" "$s"

Or use e.g. bc to get the time in fractional days, as @Pheric's answer shows.

Not the answer you're looking for? Browse other questions tagged .