I have a file say file.txt and contents looks like below with more than 10000 lines.

Wed May 27 15:41:29 PDT 2020  
Entering directory : /aaa/bbb/ccc  
Wed May 27 15:42:30 PDT 2020  
Entering directory : /aaa/bbb/ccc/ddd  
Wed May 27 15:43:19 PDT 2020  
Entering directory : /aaa/bbb/ccc/ddd/eee  
Wed May 27 15:44:25 PDT 2020  
Leaving directory : /aaa/bbb/ccc/ddd/eee  
Wed May 27 15:45:37 PDT 2020  
Leaving directory : /aaa/bbb/ccc/ddd'  
Wed May 27 15:46:42 PDT 2020  
Leaving directory : /aaa/bbb/ccc    

What need to done

Need to print out which directory taking how much time based on the timestamp before Entering and Leaving directory.

Output should be

_Directory_ : `/aaa/bbb/ccc`  
_Time taken_ : `00:05:13`

Entering time for this directory is : Wed May 27 15:41:29 PDT 2020 and Leaving time is : Wed May 27 15:46:42 PDT 2020 So the time difference is the output.
similarly need to print both directory path and the time difference for other instance as well.

Tried searching in the forum and got this, but it didn't solve my problem fully. Though I am not a pro in shell script. So if someone could help to me get the output will be really great.

Thank You

  • 1
    Is the tick at the end of /aaa/bbb/ccc/ddd' supposed to be there or is it a typo? Either explain it or fix it. Are there really blank chars at the end of each line in your input? If so how do we know when a directory name ends since a directory name could have blanks at the end? Again - explain or fix.
    – Ed Morton
    Jun 8, 2020 at 13:41

2 Answers 2


Not a complete answer, but based on the sed documentation if you do

cat file.txt | sed -n 'N;l;D' | sed -n '/.*\/aaa\/bbb\/ccc\s*\$/p'

you will get

Wed May 27 15:41:29 PDT 2020  \nEntering directory : /aaa/bbb/ccc  $
Wed May 27 15:46:42 PDT 2020  \nLeaving directory : /aaa/bbb/ccc$

which could simplify the problem, provided you know the full paths of all the directories in question, which you would have to loop over.

Not sure about the date arithmetic to get the elapsed time, but if you can extract the dates from the above, converting to unix time (seconds since Jan 1, 1970) might be a start. For example

date -d "Wed May 27 15:41:29 PDT 2020" +%s



So if in your script you can get the dates into variables (which I will do verbatim)

ENTERDATE="Wed May 27 15:41:29 PDT 2020"
LEAVEDATE="Wed May 27 15:46:42 PDT 2020"

and convert them into unixtime


you can get the duration in seconds as follows


which in this case is


There may be some packages available the would better support date arithmetic.

Hope this helps.

  • Thank You @orylis. I need to combine all of those and tweak a bit to get the exact output. I am preparing the script and hope that will help me to get what I wanted.
    – SRP
    Jun 5, 2020 at 8:41

Here's how to do what you want using GNU awk for mktime() and making some assumptions about your input:

$ cat tst.awk
{ gsub(/^[[:space:]]+|[[:space:]]+$/,"") }
sub(/^Entering directory : /,"") {
    enterSecs[$0] = epochSecs
sub(/^Leaving directory : /,"") {
    deltaSecs = epochSecs - enterSecs[$0]
    delete enterSecs[$0]

    hrs  = int(deltaSecs / (60*60))
    mins = int((deltaSecs % (60*60)) / 60)
    secs = deltaSecs % 60

    printf "_Directory_ : `%s`\n", $0
    printf "_Time taken_ : `%02d:%02d:%02d`\n", hrs, mins, secs
    gsub(/:/," ")
    mthNr = (index("JanFebMarAprmayJunJulAugSepOctNovDec", $2)+2) / 3
    epochSecs = mktime( sprintf("%04d %02d %02d %02d %02d %02d", $NF, mthNr, $3, $4, $5, $6) )


$ awk -f tst.awk file
_Directory_ : `/aaa/bbb/ccc/ddd/eee`
_Time taken_ : `00:01:06`
_Directory_ : `/aaa/bbb/ccc/ddd`
_Time taken_ : `00:03:07`
_Directory_ : `/aaa/bbb/ccc`
_Time taken_ : `00:05:13`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.