#!/bin/bash
echo "hi"
path="/home/alert/VideoApplicationAPI.v1/logs"
dayDiff=365
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)"
diff="${now}-${lastUpdate}"
done
echo $DATE

- 138,973
-
Hi and welcome to U&L! would you please elaborate more on your question and add some context into your asking question by [edit]ing. please also see https://unix.stackexchange.com/help/how-to-ask. also check out this https://unix.stackexchange.com/q/187966/72456 which is what you need? – αғsнιη Mar 15 '19 at 05:58
-
https://unix.stackexchange.com/questions/24626/quickly-calculate-date-differences – kevlinux Mar 15 '19 at 06:06
-
You should reduce the code to the essential parts. – RalfFriedl Mar 15 '19 at 06:55
2 Answers
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.

- 162
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
86527
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"
24:02:07
Or use e.g. bc
to get the time in fractional days, as @Pheric's answer shows.

- 138,973