How to show a list of files from some directory root sorted in file size descending order from command line?
If you want to list everything in the directory recursively, use either
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 (
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
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.
It is sufficient to use
du --max-depth 1 * | sort -n -k1
Example of the output ,
$ du --max-depth 1 * | sort -n -k1 4 bin/CS-1400 4 bin/csrc 4 bin/DIR@YOLO 4 bin/EET2350 4 bin/HW6 4 RS232Functions.c 4 RS232Functions.c~ 4 Untitled Folder 8 1204686.docx_en-US_zh-CN.docx 8 bin/shell 16 MSUDenver_50th_Formal.png 20 bb0239ba-1718-4778-b19a-3826f36a95cd.png 20 mainLogo.png 24 bin/NAME WITH SPACES 32 bin/ala 32 bin/Online_book 60 bin/HORTON 72 bin/JAVA-OTHER