How to list the size of each file and directory and sort by descending size in Bash?

Kit Ho picture Kit Ho · Sep 18, 2011 · Viewed 176.5k times · Source

I found that there is no easy to get way the size of a directory in Bash?

I want that when I type ls -<some options>, it can list of all the sum of the file size of directory recursively and files at the same time and sort by size order.

Is that possible?

Answer

Developer picture Developer · Feb 7, 2013

Simply navigate to directory and run following command:

du -a --max-depth=1 | sort -n

OR add -h for human readable sizes and -r to print bigger directories/files first.

du -a -h --max-depth=1 | sort -hr