There is nothing to do with multi-threading. After ls
completed its task, its all output will be send to sort
, never before.
An explanation more elevated:
A Unix pipe connects the STDOUT (standard output) file descriptor of
the first process to the STDIN (standard input) of the second. What
happens then is that when the first process writes to its STDOUT,
that output can be immediately read (from STDIN) by the second
process.
Using multiple pipes is no different than using a single pipe. Each
pipe is independent, and simply links the STDOUT and STDIN of the
adjacent processes.
[...] pipes, as such, are consistent everywhere in a bash script.
Source: What is a simple explanation for how pipes work in BASH?
So, I'm pretty sure that ls -lh -R |sort >> output.txt
it's working like a charm. Also, I tested on my system in /
directoryand and it has functioned properly with 1.597.396 files and directories (of course I had to wait a little).
If you wish an alphabetically order after file names only, don't use ls
with -l
argument. If you don't wish to see all blank lines, use sort with -u
argument. So, overall, you should use:
ls -h -R |sort -u >> output.txt
See man ls
and man sort
for more options or for for a better understanding.