Sorting human readable file sizes

17,300

Solution 1

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

Solution 2

Use GNU coreutils >= 7.5:

du -hs * | sort -h

(Taken from this serverfault question)

Man page

Edit: You can check your versions using du --version and sort --version if you are using the GNU versions. If you're using homebrew you may need to use gdu and gsort.

Solution 3

If you are just worried about files larger than 1MB, as it seems you are, you can use this command to sort them and use awk to convert the size to MB:

du -s * | sort -n | awk '{print int($1 / 1024)"M\t"$2}'

Again, this rounds the sizes to the nearest MB. You can modify it converting to the unit of your choice.

Solution 4

This one handles filenames with whitespace or apostrophes, and works on systems which do not support xargs -d or sort -h:

du -s * | sort -n | cut -f2 | tr '\n' '\0' | xargs -0 -I {} du -sh "{}"

which results in:

368K    diskmanagementd
392K    racoon
468K    coreaudiod
472K    securityd
660K    sshd
3.6M    php-fpm

Solution 5

du -sk * | sort -n | awk '{ print $2 }' | while read f ; do du -sh "$f" ; done

Share:
17,300

Related videos on Youtube

notnoop
Author by

notnoop

Love macs and linux!

Updated on September 17, 2022

Comments

  • notnoop
    notnoop almost 2 years

    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.

  • notnoop
    notnoop almost 15 years
    This is similar to: du -sm * | sort -n. -s/-g makes du output sizes in megabytes/gigabytes.
  • Læti
    Læti over 11 years
    That is already what the user is doing actually, he/she just didn't gave the example with MiB but mentioned about it. What he/she is looking for is to be able to sort when using the -h flag to du.
  • Pratik Khadloya
    Pratik Khadloya almost 10 years
    For MB you have to divide by 1024 more. So it will be int($1 / (1024 * 1024))
  • arayinfree
    arayinfree almost 10 years
    OSX doesn't have this option. You can use homebrew to brew install coreutils (which prepends all the coreutils commands with a 'g'). You can then do gdu -hs * | gsort -h.
  • jvriesem
    jvriesem over 9 years
    Just to clarify @dsummersl's point: the du -hs * works fine on Mac OS X, but sort -h returns sort: invalid option -- h. One can also install the coreutils package via MacPorts as described here.