ed82 ed82 - 5 months ago 43
Linux Question

Is there a way to redirect time output to file in Linux

Just a little question about timing programs on Linux: the time command allows to
measure the execution time of a program:

[ed@lbox200 ~]$ time sleep 1

real 0m1.004s
user 0m0.000s
sys 0m0.004s


Which works fine. But if I try to redirect the output to a file, it fails.

[ed@lbox200 ~]$ time sleep 1 > time.txt

real 0m1.004s
user 0m0.001s
sys 0m0.004s

[ed@lbox200 ~]$ cat time.txt
[ed@lbox200 ~]$


I know there are other implementations of time with the option -o to write a file but
my question is about the command without those options.

Any suggestions ?

Answer

Try

{ time sleep 1 ; } 2> time.txt

which combines the STDERR of "time" and your command into time.txt

Or use

{ time sleep 1 2> sleep.stderr ; } 2> time.txt

which puts STDERR from "sleep" into the file "sleep.stderr" and only STDERR from "time" goes into "time.txt"