1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Rep Power

    How to find diff in time stamp

    ls -lrt /usr/local/intranet/areas/prod/output/SRGW_0?/
    O/P of above command.
    drwxr-xr-x 2 mtsadm mts 4096 Mar 13 10:44 153913
    drwxr-xr-x 2 mtsadm mts 4096 Mar 13 10:48 153914
    drwxr-xr-x 2 mtsadm mts 4096 Mar 13 10:53 153915
    drwxr-xr-x 2 mtsadm mts 4096 Mar 13 10:57 153916
    drwxr-xr-x 2 mtsadm mts 4096 Mar 13 11:01 153917
    drwxr-xr-x 2 mtsadm mts 4096 Mar 13 11:05 153918
    drwxr-xr-x 2 mtsadm mts 4096 Mar 13 11:10 153919
    drwxr-xr-x 2 mtsadm mts 4096 Mar 13 11:14 153921
    drwxr-xr-x 2 mtsadm mts 4096 Mar 13 11:14 153920
    Load time of 153913 = 4 minutes.
    I need to in corporate a logic similar to this but need to do it in a loop as there are many directories for load time calculation.
    Store in a variable = ls -lrt /usr/local/intranet/areas/prod/output/SRGW_05/ | cut -f24 -d" "
    h1=`echo $T1|cut -d: -f1`
    m1=`echo $T1|cut -d: -f2`
    x1=`echo "$h1*60 + $m1"|bc -l`
    h2=`echo $T2|cut -d: -f1`
    m2=`echo $T2|cut -d: -f2`
    x2=`echo "$h2*60 + $m2"|bc -l`
    if test $x1 -lt $x2
    diff=`echo "$x2 - $x1"|bc -l`
    diff=`echo "$x1 - $x2"|bc -l`
    echo "Load time is $diff"

    Can some one give me the answer
  2. #2
  3. Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2006
    Rep Power
    Ok, now we have an idea of what you are after (having had it clarified in another thread):

    I suspect throwing some form of the basic data (the directory listing) at awk will be the way to go for this. Things may go 'creaky' if a time period spans midnight, so be warned! Also, there seems to be no way of determining the time difference for the earliest entry - you'll have the end time, but no start time. I'll be rash and assume that it will be available at run time, and you can pass it to the command.

    We, for this, really only care about the directory name and the time stamp, so we can drop the user and group names from the output:
    ls -logtr /usr/local/intranet/areas/prod/output/SRGW_0?/
    will produce a basic-ish list which we can use awk to 'chomp' at:

    ls -logtr | awk -vSTART="hh:mm" 
      ' function tdiff(d, s, e) {
          split(s,t1,":"); split(e,t2,":");
          t1m=t1[1]*60+t1[2]; t2m=t2[1]*60+t2[2];
          print d, td;
      BEGIN { x=0; n=""; ts=""; te="" }
         if (NR==1} {
            n=$6; te=$5;
         else {
           ts=$5; tdiff(n, ts, te); n=$6; te=ts; ts=$5;
       END { tdiff(n, START, te) }
    The above should (if the brackets all match up!) do pretty much what you need.
    We take the ls output, pipe it into awk. You will need to supple the correct value for the -vSTART="hh:mm", replacing the hh:mm with the correct time.
    We set some defaults inthe BEGIN clause, then in the main code we check to see if this is the first time through - by using the internal NR variable which will be 1 or the first row of the input. If it is we remember the directory name and the time.
    Next, and subsequent times round the loop we will have two sets of data - from the previous row and from the current row.
    It is then just a matter of juggling the data, getting the difference in times (using a function as we will be doing the same thing in two places), and printing that out. After that we rotate the current row data into the old row variables and get ready to do it again.
    To process the final row we need the input from our 'dummy' START variable to provide what we would have used by getting the next row, and call out function again.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  4. #3
  5. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Rep Power


    Now for the recursive/many directories part:

    /local/intranet/areas/prod/output/ -type d -exec script.sh {} \;

    script.sh holds (or whichever ls options work with your awk program)

    echo IN DIRECTORY "$path"
    ls --sort=time --full-time $path | gawk -f diff_in_time_stamp.awk
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo