SetupBeginner10 min read

Install GNU Coreutils on macOS with Homebrew

macOS ships with BSD command-line tools that differ from the GNU versions found on Linux. Learn how to install GNU coreutils via Homebrew for a consistent terminal experience.

Why macOS Commands Behave Differently

macOS is built on a BSD Unix foundation, and its command-line tools come from the BSD project — not from GNU, which is what Linux uses. Most of the time, basic usage is identical. But when you start using flags and options, the differences show up.

Here are some common examples:

CommandGNU (Linux)BSD (macOS)
In-place edit with sedsed -i 's/old/new/' filesed -i '' 's/old/new/' file
Colorized ls outputls --color=autols -G
Date formattingdate -d '2 days ago'date -v-2d
Read symlink targetreadlink -f pathNot supported (need grealpath)

If you're learning terminal commands from any Linux-based tutorial or course, having GNU tools on your Mac ensures everything works the same way.

Installing Homebrew

Homebrew is the standard package manager for macOS. If you already have it installed, skip to the next section.

1

Open Terminal

Open Terminal.app from Applications > Utilities, or search for "Terminal" using Spotlight (Cmd + Space).

2

Run the Homebrew installer

Paste the following command and press Enter:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Follow the on-screen instructions. You may need to enter your macOS password.

3

Verify the installation

brew --version

If you see a version number, Homebrew is ready.

NOTE

On Apple Silicon Macs (M1/M2/M3/M4), Homebrew installs to /opt/homebrew/. On Intel Macs, it installs to /usr/local/. The installer will tell you which path to use.

Installing GNU Coreutils

1

Install the coreutils package

brew install coreutils

This installs GNU versions of core commands like ls, cat, cp, mv, sort, date, and many more. By default, they're prefixed with g to avoid conflicts with the built-in BSD tools — for example, gls instead of ls.

2

Add GNU tools to your PATH

To use the GNU versions as the default (without the g prefix), add the gnubin directory to the beginning of your PATH.

For zsh (the default shell on modern macOS):

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

For bash:

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

Verify it's working

ls --version

If you see output mentioning GNU coreutils, the GNU version is now your default. If you still see an error like "illegal option", the PATH change hasn't taken effect — try opening a new terminal window.

Installing Additional GNU Tools

Coreutils covers the basics, but several other commonly used tools also have BSD/GNU differences. Here are the ones worth installing:

brew install gnu-sed    # GNU sed (in-place editing without '')
brew install grep       # GNU grep (--color, -P for Perl regex)
brew install findutils  # GNU find, xargs, locate
brew install gawk       # GNU awk
brew install gnu-tar    # GNU tar

To use these without the g prefix, add their gnubin directories to your PATH as well:

# Add to ~/.zshrc or ~/.bash_profile
export PATH="$(brew --prefix gnu-sed)/libexec/gnubin:$PATH"
export PATH="$(brew --prefix grep)/libexec/gnubin:$PATH"
export PATH="$(brew --prefix findutils)/libexec/gnubin:$PATH"
export PATH="$(brew --prefix gawk)/libexec/gnubin:$PATH"
export PATH="$(brew --prefix gnu-tar)/libexec/gnubin:$PATH"
NOTE

You can add all of these PATH lines to your shell config file at once. After editing, run source ~/.zshrc (or open a new terminal) for the changes to take effect.

Key Differences You'll No Longer Worry About

With GNU tools installed, these common frustrations disappear:

sed in-place editing

# Works on both Linux and macOS (with GNU sed)
sed -i 's/foo/bar/' file.txt

No more remembering to add '' after -i on macOS.

Date arithmetic

# GNU date — works on both Linux and macOS
date -d '2 days ago' +%Y-%m-%d
date -d 'next friday' +%Y-%m-%d

Extended regex in grep

# GNU grep supports -P (Perl-compatible regex)
grep -P '\d{3}-\d{4}' contacts.txt

Reverting to BSD Tools

If you ever need to use the original BSD version of a command, you can access it by its full path:

/usr/bin/sed    # BSD sed
/usr/bin/grep   # BSD grep

Or remove the gnubin directories from your PATH to revert completely.

Next Steps

With GNU tools installed, your macOS terminal now behaves just like a Linux terminal. Any tutorial, guide, or course designed for Linux will work seamlessly on your Mac — including WebTerm Learn's interactive courses.

Related Courses

Ready to practice? Try these interactive courses.

Related Articles

Back to Articles