Thursday, May 2, 2024
 Popular · Latest · Hot · Upcoming
1
rated 0 times [  1] [ 0]  / answers: 1 / hits: 1512  / 2 Years ago, thu, february 3, 2022, 12:44:09

I want to recursively sort all the file in the present directly.



I tried ls -lh -R |sort >> output.txt. But i think multi-threading problem is creating a problem, before complete output of ls -lh -R, the sort has started to do it's work.

Now i'm getting wrong answer. How can I do it properly .

I was trying to : ANSWER THIS QUESTION


More From » scripts

 Answers
5

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.


[#29466] Thursday, February 3, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
eving

Total Points: 162
Total Questions: 102
Total Answers: 112

Location: Trinidad and Tobago
Member since Thu, Dec 1, 2022
1 Year ago
eving questions
;