How sort find result by file sizes

Parsing ls or variations thereof will add unnecesary complexity.

Sorted file paths by file size:

find src -type f -printf '%s\t%p\n' | sort -n | cut -f2-

Notes:

  • Change sort -n to sort -nr to get reverse order
  • The question had -print0 but catering for file names containing newlines seem pedantic.

  • The question mentioned relative paths, and changing %p to %P will get relative paths under src/


find -type f -exec du -sm {} \; | sort -nk1

Size in MiB, path is relative.


Here is how to do using find command:

find . -type f -exec ls -al {} \; | sort -k 5 -n | sed 's/ \+/\t/g' | cut -f 9

Here is how to do using recursive ls command:

ls -lSR | sort -k 5 -n

Or, if you want to display only file names:

ls -lSR | sort -k 5 -n | sed 's/ \+/\t/g' | cut -f 9