Ubuntu: Problem IN piping the output of one command to other



Question:

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


Solution:1

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.


Solution:2

Try using find instead of ls -R, e.g. to find all the files > 100MB in the current directory and its subdirectories and then sort them by (human readable) size and list the largest 10

find . -size +100M -exec du -hd 1 {} \; | sort -hr | head -10  

or (slightly more robust)

 while read -rd $'\0' file; do du -h "$file"; done < <(find . -size +100M -print0) | sort -hr | head -10  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »