Jason Sares is a Giant Dork

Bash One Liner – Human Readable Sorted du

du -s * 2>/dev/null | sort -n | cut -f2 | xargs du -sh 2>/dev/null

Posted in Linux Quick Tip | 6 comments

6 Comments so far

  1. Alain Kelder June 12th, 2009 4:39 pm

    That’s a good one! It fails for files/dirs with spaces in the name. According to xargs man page, it’ll accept a delimiter such as the new line character. With that it can handle spaces:

    du -s * | sort -n | cut -f2 | xargs -d “\n” du -sh

    Sweet…

  2. Alain Kelder is a Giant Dork… January 8th, 2010 1:31 am

    […] liked Jason’s solution of passing a file list to du via xargs to produce results sorted by size in human readable format, […]

  3. redondos January 14th, 2010 2:47 pm

    GNU sort has a -h option that makes this trivial:

    du -hs * | sort -h

  4. Alain Kelder January 14th, 2010 5:34 pm

    Sounds promising, but sort complains that -h is an invalid option on most Linuxes I use..

    Confirmed on:

    CentOS 5.4 — sort (GNU coreutils) 5.97
    Debian 5.0.3 — sort (GNU coreutils) 6.10
    Ubuntu 8.10 — sort (GNU coreutils) 6.10
    Ubuntu 9.10 — sort (GNU coreutils) 7.4

  5. Jason Sares January 19th, 2010 11:59 am

    Is there a popular distro that carries the new version of sort (-h)?

  6. Alain Kelder January 19th, 2010 1:50 pm

    Looks like this option was added in 7.5 (1) in May 2009 (2) — so it’s quite new. So only distros which package new stuff very aggressively would have it (or distros without package management 🙂 — are there any?).

    Wonder what distro redondos runs..

    In Debian, looks like it just missed making it into the testing branch, so you’d have to grab 8.1 from unstable (3). This means it’ll probably show up in Ubuntu within the next couple of releases.

    Latest stable version of corutils from GNU is 8.4 (4). Perhaps there are other useful features that would make it worthwhile to install from source. Although according to “apt-cache showpkg coreutils”, a ton of other packages depend on coreutils so I’d be fearful of introducing an undiscovered bug and breaking a lot of stuff. I think Debian’s cautious approach to slowly promoting packages from experimental branch to unstable > testing > stable has a lot of merit.

    Looks like 7.5 is in Gentoo stable (5)..

    (1) http://savannah.gnu.org/patch/?2565
    (2) http://git.savannah.gnu.org/gitweb/?p=coreutils.git;a=commit;h=159faba1376ffd5a46fe4bbc780d85dd3e502cea
    (3) http://packages.debian.org/search?keywords=coreutils
    (4) http://directory.fsf.org/project/coreutils/
    (5) http://packages.gentoo.org/package/sys-apps/coreutils