sort -h on a Mac

Published: May 30, 2017

Tags:

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
428K	CHANGELOG.md
196K	app
 32K	LICENSE_EE.txt
 12K	phpserver
 12K	LICENSE_AFL.txt
 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	README_EE.md
4.0K	ISSUE_TEMPLATE.md
4.0K	Gruntfile.js.sample
4.0K	COPYING.txt
4.0K	CONTRIBUTING.md

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 loves dabbling in other languages like Go and Ruby. Technical topics that interest me are monitoring, security and performance. I'm also a stickler for good documentation and clear technical writing.

During the day I lead a team of developers and solve challenging technical problems at Rightpoint where I mainly work with the Magento platform. I've also spoken at a number of events.

In my spare time I blog about tech, work on open source and participate in bug bounty programs.

If you'd like to get in contact, you can find me on Twitter and LinkedIn.