Harness the Combinatoric Power of Command-Line Tools and Utilities

../Tutorials

GNU Utilities on macOS

Bash macOS Homebrew tools

Published January 26, 2019 and last verified on January 16, 2024

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.

What You Need

To complete this tutorial, you’ll need Homebrew installed, which you can do by following the Install Homebrew tutorial.

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 output similar to the following:

/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

Like coreutils, you’ll need to add these to your $PATH variable. Execute the following line:

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

Reload your configuration:

source ~/.bash_profile

Verify that the changes worked by checking the xargs command:

which xargs

You’ll see output similar to the following:

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

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

bphcommand

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.