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

Max Chadwick Hi, I'm Max!

I'm a software developer who mainly works in PHP, but also dabbles in Ruby and Go. Technical topics that interest me are monitoring, security and performance.

During the day I solve challenging technical problems at Something Digital where I mainly work with the Magento platform. I also blog about tech, work on open source and hunt for bugs.

If you'd like to get in touch with me the best way is on Twitter.