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.
you can redirect the time output using,
Because you need to take (time ls) as a single command so you can use braces.
The reason why redirection does not seem to work with
time
is that it's a bash reserved word (not a builtin!) when used in front of a pipeline. bash(1):So, to redirect output of
time
, either use curly braces:Or call
/usr/bin/time
: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:
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:
2> redirects STDERR, &> redirects both.
If you don't want to mix output from
time
and the command. With GNU time, you can use-o file
like:while
tim
is output of time,out
anderr
are stdout and stderr fromgrep
.time is shell builtin and I'm not sure if there is way to redirect it. However you can use
/usr/bin/time
instead, which definitely accept any output redirections.no need to launch sub shell. Use a code block will do as well.
or