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

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


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


you can redirect the time output using,

(time ls) &> file

Because you need to take (time ls) as a single command so you can use braces.


The command time sends it's output to STDERR (instead of STDOUT). That's because the command executed with time normally (in this case ls) outputs to STDOUT.

If you want to capture the output of time, then type:

(time ls) 2> filename

That captures only the output of time, but the output of ls goes normal to the console. If you want to capture both in one file, type:

(time ls) &> filename

2> redirects STDERR, &> redirects both.