0

I just started learning Linux commands. I was experimenting with the > command, that, as far as I understand, makes the command before it write it's output to the file after the sign. My perception however, seems to be different from the behavior of time > time.txt:

nagy@nagy-VirtualBox ~/Dokumentumok/random $ date
2016. márc. 20., vasárnap, 18.14.58 CET
nagy@nagy-VirtualBox ~/Dokumentumok/random $ time

real    0m0.000s
user    0m0.000s
sys 0m0.000s
nagy@nagy-VirtualBox ~/Dokumentumok/random $ date > date.txt
nagy@nagy-VirtualBox ~/Dokumentumok/random $ time > time.txt

real    0m0.000s
user    0m0.000s
sys 0m0.000s
nagy@nagy-VirtualBox ~/Dokumentumok/random $ cat date.txt
2016. márc. 20., vasárnap, 18.15.21 CET
nagy@nagy-VirtualBox ~/Dokumentumok/random $ cat time.txt
nagy@nagy-VirtualBox ~/Dokumentumok/random $

So, as it seems, while date > date.txt works as expected, the >, oddly enough for me, fails with time > time.txt. Can anybody explain this?

This happened on a Linux Mint 14.3 virtual machine hosted by Windows 10.

2

The time command does not do what you seem to think it does. In fact, since its purpose is to time the running of another command, running it without arguments (without a command to time) doesn't make much sense. (Apparently it still runs without complaint though!)

The specific effect you are seeing here is that time outputs its statistics on standard error, not on standard output (in order to avoid interfering with the output of whatever it is that you are timing). Redirecting the standard output has no effect on the standard error channel. Try this instead:

time ls 2>time.txt

...where you are redirecting the standard error, not the standard output. The output of ls is displayed as usual (standard output is not redirected), but the output of time itself on standard error goes to the file, which is what I think you were trying to achieve.

  • Thanks! Noobs make funny errors, as it seems. But this makes me wondering: how would you put that damm output of time into the file? – Neinstein Mar 20 '16 at 18:58
  • Aha! Try /usr/bin/time instead of just time. This forces you to use the external version of the time command, which behaves as expected. Turns out time is a little too much of a special case because it is intercepted by the shell and a built-in version which doesn't follow the rule gets run. I don't know whether that's mandated by the standards of not but it's definitely unusual. – Celada Mar 20 '16 at 19:05
  • @Celada Both behaviors are allowed. See the recent spate of time-utility questions. – Gilles Mar 20 '16 at 19:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.