Showing file sizes in sorted order

Solution 1:

From your folder:

find . -maxdepth 1 -type f -exec du -h {} + | sort --human-numeric-sort --reverse

You can set how deep it can look for files with -maxdepth parameter or without it to be recursive.

Solution 2:

If you want to list everything in the directory recursively, use either find or du with sort:

find /some/path -type f -printf '%s %p\n' | sort -rn
du -h /some/path | sort -rh

The former will show only files, and size in bytes. The latter will show both file and cumulative directory sizes, in human-readable (using KB, MB, etc.) format. sort accordingly uses numeric for the former (-n) and human-readable for the latter (-h).


With more complexity, the best option would be:

find /some/path -type f -print0 | du --files0-from=- -0h | sort -rzh | tr '\0' '\n'

du can read a NUL-delimited list of files from input, and find can print NUL-delimited filenames using -print0. sort can then take the NUL-delimited list of sizes and filenames and sort them, and finally you replace NULs with newlines for convenient display.

Since filenames and paths can contain anything except the ASCII NUL character, using NUL-delimited lines will processing them is the safest way.

You can also get find to print the size as seen in the first command, but with -printf '%s %p\0' to still use NUL-delimited lines, and skip using du as the middle man.

Solution 3:

As @Terrance said, ls -lS sorts files in descending order. For all of the files, ls -lSa works.