2

I want to pipe the output of time command rather than the output of the job it is measuring the time of. I tried

 /usr/bin/time -v java RandomTest > time.log

But that redirects java RandomTest's output to time.log

fo_x86
  • 2,583
  • 1
  • 30
  • 41
  • possible duplicate of [how can't I redirect the output of time command](http://stackoverflow.com/questions/2408981/how-cant-i-redirect-the-output-of-time-command) – Simon Dec 13 '12 at 04:01

3 Answers3

4

This one is a bit tricky, but you can do it as follows:

{ time java RandomTest ; } 2> time.log

This way the result of time goes into time.log, and stdout from java RandomTest stays in your tty.

Try it first with:

{ time ls ; } 2> tmp.txt 

cat tmp.txt
sampson-chen
  • 45,805
  • 12
  • 84
  • 81
3

At least on recent Linux distributions, the time command supports the -o FILE option:

/usr/bin/time -v -o time.log java RandomTest
kanaka
  • 70,845
  • 23
  • 144
  • 140
1

You could redirect also the stderr e.g. with >& in bash or zsh

(the >& is the same as 2>&1 since its redirects stderr to stdout)

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Ah I suspected time might be writing to stderr and tried `time -v java RandomTest 2 > time.log` with no avail. I'm not familiar with the `>&` construct. How is `>& time.log` different from `2 > time.log`? – fo_x86 Dec 12 '12 at 21:48
  • @fo_x86 : Not sure if you have a typo there, but its `2> time.log` (not space). Good luck. – shellter Dec 12 '12 at 22:09
  • Thanks for pointing that out. Yes it was a careless typo. – fo_x86 Dec 12 '12 at 22:17