tags:

views:

68

answers:

4

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 get the output of the ls command not the time Please explain me , wny I couldn't and how can I ?

+3  A: 

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.

sganesh
yeah relly fine , but the question is how they did this ?
abubacker
time command will take the arguments as a command. But parenthesis will group that as a one command. Ex:time ls > file1.txtIn arguments, 0 = time 1 = "ls > file1.txt"
sganesh
And time command prints the output in stderr. So you can use(time ls) 2> file
sganesh
+1  A: 

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.

Michal Čihař
/usr/bin/time --output filename ls
chub
The builtin time works well with output-redirection. It only outputs on STDERR, not on STDOUT.
Mnementh
A: 

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 goers normal to the console. If you want to capture both in one file, type:

(time ls) &> filename

2> redirects STDERR, &> redirects both.

Mnementh
+1  A: 

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
ghostdog74
+1, Best answer
Tim Post