HTF
HTF

Reputation: 7260

Measure Elapsed Time from logs in BASH

I would like to check the elapse time for the rsnapshot, directory deletion process

Rsnapshot log format (I filtered this with grep):

cat /var/log/rsnapshot | grep -A 1 "/bin/rm"

[21/Nov/2012:07:41:24] /bin/rm -rf /mnt/sdb/rsnapshot/daily.delete/
[21/Nov/2012:09:47:50] WARNING: /usr/local/bin/rsnapshot -v daily: completed, but with some warnings

then the time only:

cat /var/log/rsnapshot | grep -A 1 "/bin/rm" | awk '{ print $1 }' | cut -b 14-21 | uniq
07:41:24
09:47:50

However I'm not sure how to use date command to subtract the time.

Please let me know if there is a better/more efficient way of doing this in BASH as I'm still on a learning curve.

Upvotes: 0

Views: 277

Answers (1)

anishsane
anishsane

Reputation: 20970

Use date command with below syntax:

NEW=`date --date "$NEW_DATE" +%s`
OLD=`date --date "$OLD_DATE" +%s`
diff=$((NEW-OLD))

This will give you difference in seconds... Then you can easily take modulo 60 to separate seconds, hours, mins etc.

Upvotes: 2

Related Questions