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:
| Command | GNU (Linux) | BSD (macOS) |
|---|---|---|
| In-place edit with sed | sed -i 's/old/new/' file | sed -i '' 's/old/new/' file |
| Colorized ls output | ls --color=auto | ls -G |
| Date formatting | date -d '2 days ago' | date -v-2d |
| Read symlink target | readlink -f path | Not 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.
Open Terminal
Open Terminal.app from Applications > Utilities, or search for "Terminal" using Spotlight (Cmd + Space).
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.
Verify the installation
brew --versionIf you see a version number, Homebrew is ready.
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
Install the coreutils package
brew install coreutilsThis 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.
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 ~/.zshrcFor bash:
echo 'export PATH="$(brew --prefix coreutils)/libexec/gnubin:$PATH"' >> ~/.bash_profile
source ~/.bash_profileVerify it's working
ls --versionIf 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"
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.