Why is there a difference in the duration of command execution with different redirection methods

io-redirectiontimetty

I am running a timed find command as normal user.

What I know is that redirection is to prevent stdout/stderr messages on the terminal. If that's the case, why do different redirections methods take different amounts of time? Is it somehow related to the write speed on the tty or is there any other reason behind it? Could someone point me in right direction in understanding this?

$ id
uid=1000(user1) gid=1000(user1) groups=1000(user1),1001(user2)

$time find /
<truncated output>
real    0m13.902s
user    0m0.197s
sys 0m0.448s

$ time find /  >/dev/null  
<truncated output>
real    0m0.298s
user    0m0.068s
sys 0m0.206s

$time find /  2> /dev/null 
<truncated output>
real    0m13.279s
user    0m0.181s
sys 0m0.405s

$ time find /  > /dev/null 2>&1
real    0m0.306s
user    0m0.109s
sys 0m0.174s

Best Answer

When your process (find) needs to actually write out the output, that obviously takes a lot longer than when you tell it to discard said output.

  • When you use find /, both the stdout and stderr are sent to your terminal, and it has to write out both of them (i.e., the actual results and all the permission errors and whatnot)

  • When you use time find / >/dev/null you are dropping the standard output of the command, but still printing out all the errors (if you have any). Judging by your results, you have lots of legitimate results and very few errors.

  • When you use time find / 2> /dev/null, the standard output of the command is still being sent to your terminal, but now you're simply dropping the stderr. If you were finding through a filesystem that you did not have permission to read, this would actually be pretty fast.

  • When you use time find / > /dev/null 2>&1, you are dropping the standard output, and then sending standard error to where standard output is being sent,... i.e., you are dropping both. This will not output anything, and thus will be the fastest of all commands.

Related Question