Why does this command (creating an empty folder, changing to it, touching a file, calling ls
and then cat
ing it), display two files?
root@ubuntu:~# mkdir a ; cd a ; touch b ; ls > c ; cat c
b
c
I believe that I should only get "b".
When you redirect the output of ls
to the file c
, it's a chicken and egg problem:
If c
would not be created upfront, that would mean the shell would need to store the output in a buffer and (in the end) write that buffer to the file.
Since this is not the best approach in many cases (because of memory management, management of failure for commands that get interrupted before completion, etc.), the file gets created upfront.
Thus, the standard output for the command can be replaced with the newly created file and the command output can be streamed to that file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With