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

ShellUnixTimeIo Redirection

Shell Problem Overview


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.

Shell Solutions


Solution 1 - Shell

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

Solution 2 - Shell

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.

Solution 3 - Shell

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.

Solution 4 - Shell

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.

Solution 5 - Shell

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):

> If the time reserved word precedes a pipeline, the elapsed as well as > user and system time consumed by its execution are reported when the > pipeline terminates.

So, to redirect output of time, either use curly braces:

{ time ls; } 2> filename

Or call /usr/bin/time:

/usr/bin/time ls 2> filename

Solution 6 - Shell

If you don't want to mix output from time and the command. With GNU time, you can use -o file like:

/usr/bin/time -o tim grep -e k /tmp 1>out 2>err

while tim is output of time, out and err are stdout and stderr from grep.

Solution 7 - Shell

I use the redirection of stdout and stderr method with braces for testing.
The &>>rpt represents this >>rpt 2>&1 but shorter.
The braces will execute a command(s) in the current shell.   See: man bash

{ time ls a*; } &>>rpt

Solution 8 - Shell

Not the canonical use case, but another way to go.

Longer running simple tasks can be launched in a detached "screen" terminal with logged output. You could even give the log a unique name.

Primarily this method is good for something that will take hours and is invoked over SSH with a need to "check up on" from time to time. In preference to backgrounding and disowning.

screen -dmL time -v ./crackpassword 

You get the same output a terminal would get, with the caveat that this is asynchronous. Of course it could be a script. The output may need tweaking.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionabubackerView Question on Stackoverflow
Solution 1 - Shellghostdog74View Answer on Stackoverflow
Solution 2 - ShellsganeshView Answer on Stackoverflow
Solution 3 - ShellMnementhView Answer on Stackoverflow
Solution 4 - ShellMichal ČihařView Answer on Stackoverflow
Solution 5 - Shelluser1338062View Answer on Stackoverflow
Solution 6 - ShellDing-Yi ChenView Answer on Stackoverflow
Solution 7 - ShellcognativeorcView Answer on Stackoverflow
Solution 8 - ShellmckenzmView Answer on Stackoverflow