Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I want to measure the elapsed time of a command, and show it without fractions of seconds no matter how little time it takes.

For instance,

/usr/bin/time -f "%E (real)" SOMECOMMAND

might return 4:36:05 for a long running process, or 7:27.32 for a short one.

In the second instance, how can I format it as 7:27 or 00:07:27 (leading zeros not important), so that it is obvious at a glance that it's 7 minutes (and not 7 hours as I initially thought)?

The seconds fractions are just not necessary for my measurements.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

If you're using Bash, you could use the built-in time command. It outputs "m" for minutes and "s" for seconds instead of ambiguous colons. You can control the format of its output with the TIMEFORMAT variable:

$ time sleep 1.99

real    0m1.999s
user    0m0.004s
sys     0m0.004s
$ TIMEFORMAT=%0lR    # truncate (not round) the decimal seconds
$ time sleep 1.99
0m1s

The default value of TIMEFORMAT:

TIMEFORMAT=$'\nreal\t%3lR\nuser\t%3lU\nsys%3lS'

Edit:

You can also use sed to modify the output of /usr/bin/time to remove the ambiguity in a similar way:

$ /usr/bin/time -f "%E" sleep 1.99 2>&1 | sed 's/:\([^:]\+\)$/m\1/;s/:/h/;s/\..*//;s/$/s/'

Demo using simulated time output:

$ echo -e '0:01.99\n10:11:12' | sed 's/:\([^:]\+\)$/m\1/;s/:/h/;s/\..*//;s/$/s/'
0m01s
10h11m12s
share|improve this answer
    
I only needed the last line to be formatted, so I added "$" to the beginning of each sed directive. The command changes to sed '$s/:([^:]\+)$/m\1/;$s/:/h/;s/\..*//;$s/$/s/' –  tephyr Jan 28 '11 at 21:58

Try piping it through this cut command. I'm not in front of an Linux shell to try it for sure so your mileage may vary. For example, I'm not sure if the first field is zero or one.

cut -d . -f 1

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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