Dired: human-readable sizes and sort by size

We’ve looked a few times at dired, emacs’ powerful file browser. This time I want to look at file sizes in dired. By default, dired lists file sizes in bytes, but this is easy to change by customising the switches that dired uses when it calls your system’s ls command to generate the directory listing.

Add the following to your emacs config file to have file sizes given in “human-readable” format (i.e. in units of B, K, M, G as appropriate).

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; dired                                                                  ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(setq dired-listing-switches "-alh")

We already saw that hitting s in a dired window changes the sorting between file name and modification date. It is also nice to sort by file size sometimes. To do this, we can use the prefix argument in front of the s command, so use C-u s in the dired buffer. This prompts us for the ls switches, which we can change from -alh to -alhS to sort by size. Xah Lee covers this in more detail in his extensive emacs pages.