Finding the difference in time between the first and last file in a folder using bash

I was working on running some statistics on log files and it required me to figure out the difference to increase the accuracy. I came up with the following bash script:

#!/bin/bash
# get the dates
start_date=$(date --utc --date "$(ls -Rt --full-time | tail -n1 | awk '{ print $6 }')" +%s)
end_date=$(date --utc --date "$(ls -Rt --full-time | head -n2 | tail -n1 | awk '{ print $6 }')" +%s)
 
# find the difference
difference=$((end_date-start_date))
 
# echo results
echo $end_date - $start_date = $difference seconds
echo $((difference/86400)) days

Which I originally wrote as a one liner:

start_date=$(date --utc --date "$(ls -Rt --full-time | tail -n1 | awk '{ print $6 }')" +%s); end_date=$(date --utc --date "$(ls -Rt --full-time | head -n2 | tail -n1 | awk '{ print $6 }')" +%s); difference=$((end_date-start_date)); echo $end_date - $start_date = $difference seconds; echo $((difference/86400)) days;

I got a little carried away and created this beast, which still isn’t as accurate as I need it to be but it did give me some information:

map_1=nordiccastle;map_2=dance;start_date=$(date --utc --date "$(ls -Rt --full-time | tail -n1 | awk '{ print $6 }')" +%s); end_date=$(date --utc --date "$(ls -Rt --full-time | head -n2 | tail -n1 | awk '{ print $6 }')" +%s); difference=$((end_date-start_date)); echo $... Read Moreend_date - $start_date = $difference seconds; echo logs for $((difference/86400)) days; map_1_ended=$(find -name *00*.log | xargs egrep -A 4 "endmatch|timelimit -1" |grep $map_1 |wc -l); map_1_played=$(find -name *00*.log | xargs egrep "gamestart" |grep $map_1 |wc -l); echo $map_1 endmatched $map_1_ended out of $map_1_played times played; map_2_ended=$(find -name *00*.log | xargs egrep -A 4 "endmatch|timelimit -1" |grep $map_2 |wc -l); map_2_played=$(find -name *00*.log | xargs egrep "gamestart" |grep $map_2 |wc -l); echo $map_2 endmatched $map_2_ended out of $map_2_played times played

It was used to see how many times a map was played and how many times it was voted to end the match.

It should really be a separate script to allow for more organization

Share and Enjoy:
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • ThisNext
  • MisterWong
  • Wists
  • blogmarks
  • BlogMemes
  • Furl
  • NewsVine
  • Reddit
  • SphereIt
  • Spurl
  • StumbleUpon
  • Technorati
  • YahooMyWeb

1 comment so far

  1. Mr. Bougo August 23, 2009 3:23 am

    start_date=$(date –utc –reference=”$(ls -t | head -n1)” +%s)
    end_date=$(date –utc –reference=”$(ls -rt | head -n1)” +%s)

    You’re forking too much.

Leave a comment

Please be polite and on topic. Your e-mail will never be published.