Ubuntu – How to list files from n biggest directories

command linefilesystem

I am interested in finding n biggest subdirectories (eg. 3) in my current directory AND THEN listing all the files from them. I am not interested in finding biggest files. I know there are a lot of solutions for finding biggest files and that's not what I want.

I have found my 3 biggest subdirectories with line:

$ du -hs */ | sort -rh | head -3

with result:

212K    04/
52K     02/
20K     03/

but somehow I am not able to list files from these directories (piping with ls did't work). Any suggestions how to do that?

Best Answer

You can use the results of your piped commands with a while loop:

du -hs */ | sort -rh | head -3 | while read -r size dir 
do
   ls -l "$dir"
done

As a one liner:

du -hs */ | sort -rh | head -3 | while read -r size dir ; do ls -l "$dir"; done

Thanks to steeldriver for suggesting the use of read in a while loop over a for loop using awk, to handle filenames with spaces and special characters in a more robust manner.