sort -h on a Mac

Published: May 30, 2017


du -sh * | sort -hr is my favorite command for quickly looking at how much space directories are taking up on a machine. For example, in the root of a Magento 2 installation you’ll see the following…

$ du -sh * | sort -hr
317M	vendor
100M	pub
 35M	dev
 25M	var
 25M	lib
 19M	update
7.5M	setup
460K	composer.lock
196K	app
 12K	phpserver
 12K	LICENSE.txt
8.0K	nginx.conf.sample
8.0K	bin
4.0K	php.ini.sample
4.0K	package.json.sample
4.0K	index.php
4.0K	composer.json
4.0K	Gruntfile.js.sample
4.0K	COPYING.txt

However, if you run this command on a Mac here’s what you’ll get…

$ du -sh * | sort -hr
sort: invalid option -- h
Try `sort --help' for more information.

Fortunately, per this StackExchange answer you can work around this by brew install-ing coreutils.

Doing so, will give you a new executable, gsort, which supports the -h flag.

I’ve added the following alias to my .zshrc so I don’t even have to think about it…

alias sort=gsort

:bulb:Did you enjoy this blog post?

If so, please consider checking out my side project Domain Clamp. It's a SaaS which monitors domains and SSL certificates and sends notifications before anything expires. If you work at an agency, then you're probably not the registrant for your client's domains or the SSL certificate owner. This means you won't get expiration notifications. You don't want a client's domain or SSL certificate to expire under your watch. Believe me, I've been there.

Domain Clamp solves this problem by letting you monitor the SSL certificate and registration for any domain you'd damn please. Free accounts are available so please head on over »