Small Sharp Software Tools

Harness the Combinatoric Power of Command-Line Tools and Utilities

GNU Utilities on macOS

Tagged with Bash macOS Homebrew

Published January 26, 2019

macOS systems are based on BSD, rather than on GNU/Linux like RedHat, Debian, and Ubuntu. As a result, a lot of the command line tools that ship with macOS aren’t 100% compatible.

However, using Homebrew, you can fix that.

Before You Begin

Installing coreutils

The coreutils package contains GNU versions of many tools, including date, cat, and many more.

$ brew install coreutils

To make these commands override their BSD counterparts, modify your .bash_profile file to include the path, and then apply the changes to your current environment.

$ echo 'export PATH="$(brew --prefix coreutils)/libexec/gnubin:$PATH"' >> ~/.bash_profile
$ source ~/.bash_profile

Verify that the changes are applied by using the which comand to verify that the date command comes from the coreutils package:

$ which date

You’ll see this output:

/usr/local/opt/coreutils/libexec/gnubin/date

Installing diffutils and findutils

The diffutils package includes programs that let you see differences between files, like diff, cmp, and diff3. These exist on your Mac,

Install them with:

$ brew install diffutils

These commands will be placed in /usr/local/bin, which should override the built-in tools due to how Homebrew modifies your PATH.

The findutils package includes find, locate, updatedb, and xargs.

$ brew install findutils

Installing GNU versions of awk, sed, and grep

The awk, grep, and sed programs included on macOS work very differently than their GNU counterparts.

Install the GNU version of awk with:

$ brew install awk

This installs to /usr/local/bin, overriding the built-in BSD version.

Install the GNU version of sed with this command:

$ brew install gnu-sed

This installs the command gsed. To use it in place of your existing sed command, add it to your PATH by modifying your .bash_profile file again, and applying the changes to your local environment:

$ echo 'export PATH="$(brew --prefix gnu-sed)/libexec/gnubin:$PATH"' >> ~/.bash_profile
$ source ~/.bash_profile

Install grep with:

$ brew install grep

Like sed, grep installs as ggrep, so if you want it to override your existing grep command, add it to your PATH in both your .bash_profile and your current session:

$ echo 'export PATH="$(brew --prefix grep)/libexec/gnubin:$PATH"' >> ~/.bash_profile
$ source ~/.bash_profile

Installing More Recent Tools

Your Mac comes with versions of Vim, Git, and Less that aren’t as up-to-date as you might like. Use Homebrew to install those too:

$ brew install vim git less

You now have the most essential GNU versions of popular utilities.