Listing files in a directory including contents of subfolders with sorting

Solution 1:

You can use find:

find . -type f -printf "%s %P\n" | sort -n

Optional: To convert byte values to human-readable format, add this:

| numfmt --to=iec-i --field=1

Explanation:

 find in current directory (.) all files (-type f) 

 -printf: suppress normal output and print the following:
     %s - size in bytes
     %P - path to file
     \n - new line

 | sort -n: sort the result (-n = numeric)

Solution 2:

Since you didn't specify a particular shell, here's an alternative using zsh's glob qualifiers with

setopt extendedglob

for the recursion. Then for example:

  1. recursively list plain files:

    printf '%s\n' **/*(.)
    
  2. recursively list plain files, ordered by increasing Length (i.e. size):

    printf '%s\n' **/*(.oL)
    
  3. recursively list plain files, Ordered by decreasing size:

    printf '%s\n' **/*(.OL)
    
  4. recursively list plain files, ordered by decreasing size, and select the top 3 results:

    printf '%s\n' **/*(.OL[1,3])
    

If you want the file sizes as well, then you could use

du -hb **/*(.OL[1,3])