How can I redirect the output of the "time" command?

abubacker picture abubacker · Mar 9, 2010 · Viewed 58.8k times · Source

I tried to redirect the output of the time command, but I couldn't:

$time ls > filename
real    0m0.000s
user    0m0.000s
sys     0m0.000s

In the file I can see the output of the ls command, not that of time. Please explain, why I couldn't and how to do this.

Answer

ghostdog74 picture ghostdog74 · Mar 9, 2010

no need to launch sub shell. Use a code block will do as well.

{ time ls; } 2> out.txt

or

{ time ls > /dev/null 2>&1 ; } 2> out.txt