Linux – Sorting human readable file sizes

linuxmacsortingunix

How can I sort a list using a human-readable file-size sort, numerical sort that takes size identifier (G,M,K) into account? Can I sort "du -sh" output for example?

Problem:
Consider the problem of listing files/folders and sorting them by their size. You can achieve that by running:

du -s * | sort -n

This lists the files/folders sorted by their sizes. However the printed size value is in bytes (or megabytes, or gigabytes if you choose).

It would be desirable to be able to sort based on the human-readable values, so I can run something analogous to

du -sh * | <human-readable file sort>

And have 1.5GB folder shows up after 2.0M.

Best Answer

Afaik, there's no standard command to do this.

There are various workarounds, which were discussed when the same question was asked over at Stack Overflow: How can I sort du -h output by size

Related Question