I'm searching PDF files in all directories except those in .texpadtmp by code originally based on this discussion.

function findpdfs() {
find . -iname '*.pdf' -not -path "*.texpadtmp*" | while read filename
  echo -e "\033[34;1m// === PDF Document:\033[33;1m $filename\033[0m"
  pdftotext -enc Latin1 "$filename" - | grep --color=always -i $1
  time         # TODO problem here!

where, in the last line, I try to print the execution time of the script for each file unsuccessfully.

How can you print the time correctly?

My next goal is then to have all execution times and present them as percentages in the output as whole numbers so I can approximate where most of time resources are going.


Method #1 - Using begin/end time

You'll typically see it done where you do:

begin=$(date +%s) 
... do something ...
end=$(date +%s)

And then subtract the $begin time from the $end time to get the number of seconds that have elapsed.

tottime=$(expr $end - $begin)

Method #2 - Using the time commands

The other method would be to use the time command that's built into Bash or the time program that's included with most Linux/Unix systems here,/bin/time`.

You can check like so:

$ type -a time
time is a shell keyword
time is /usr/bin/time
time is /bin/time

You use it like so:

$ time sleep 5

real    0m5.004s
user    0m0.001s
sys 0m0.001s
standalone executable
$ /bin/time sleep 5
0.00user 0.00system 0:05.00elapsed 0%CPU (0avgtext+0avgdata 628maxresident)k
0inputs+0outputs (0major+191minor)pagefaults 0swaps

They provide different outputs but you can use which ever suites your needs. Both will print the elapse time that a command takes to run:

$ time <cmd>

So you could prefix your pdf2text command.

  • Yes, I remember there is some parameters in some time command about to start and to end timing. Something like tit and tot, cannot remember exact names. Yes, I think the last approach is in Matlab and its timing facilities. – Léo Léopold Hertz 준영 Oct 17 '14 at 10:13
  • 1
    There isn't anything like tit/tot in Unix. I've only seen the 2 approaches that I've shown. In programming languages such as Perl/Ruby there are libraries that'll present you with similar methods but are typically more featureful. I seem to recall doing a similar thing in Matlab, but it's been ~10+ years since I've touched Matlab. – slm Oct 17 '14 at 11:07

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.