Tips and Tricks About Computers, Web Development, Linux, the Internet and the Like
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
| Print article | This entry was posted by Tyler Mulligan on August 22, 2009 at 10:40 am, and is filed under Bash, Computers, Linux, Programming, Ubuntu. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
about 11 months ago
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.