TExcelTAD
Home › Terminal & Command Line › BSD vs GNU Tools

BSD vs GNU Tools

Linux and macOS both have sed, date, ls, grep, find and stat, but they are different programs that happen to share names. Linux distributions use the GNU versions (GNU coreutils, GNU sed, GNU grep, GNU find). macOS uses versions from BSD (FreeBSD heritage). Both follow the POSIX standard for the basics, so grep -i error log.txt or ls -la work everywhere. The trouble starts with the extra options each side added.

The classic symptom: a command copied from a Linux tutorial fails on a Mac with illegal option or a strange invalid command code error, or a script that works on your Mac fails in CI on Linux.

text
POSIX common ground         GNU extensions              BSD extensions
(works everywhere)          (Linux, WSL, Git Bash)      (macOS, FreeBSD)
grep -i, ls -la, sed s///   sed -i, date -d, stat -c    sed -i '', date -v, stat -f
cut -d, sort -n, find -name ls --color, find -printf    ls -G, tail -r

How to tell which one you have: GNU tools answer --version; BSD tools don't know that option.

bash · try it yourself
sed --version | head -n 1
date --version | head -n 1
sed (GNU sed) 4.9
date (GNU coreutils) 9.11
zsh
ada@macbook ~ % sed --version
sed: illegal option -- -
usage: sed script [-Ealnru] [-i extension] [file ...]

The GNU examples below run in the playground (Linux). The BSD side is shown as a macOS session.

sed -i: editing a file in place

The most common trap. Both versions support -i, but BSD sed requires an argument for the backup extension, and an empty one must be written as a separate '':

bash · try it yourself
printf 'color: red\nsize: 10\n' > config.txt
sed -i 's/red/blue/' config.txt
cat config.txt
sed -i.bak 's/10/12/' config.txt
cat config.txt config.txt.bak
color: blue
size: 10
color: blue
size: 12
color: blue
size: 10
zsh
ada@macbook ~ % sed -i '' 's/red/blue/' config.txt      # BSD: empty backup suffix, separate argument
ada@macbook ~ % sed -i 's/red/blue/' config.txt         # BSD reads 's/red/blue/' as the suffix!
sed: 1: "config.txt": invalid command code c

Portable form: sed -i.bak 's/x/y/' file && rm file.bak works with both (suffix attached, no space). Or use perl -pi -e 's/x/y/' file, which behaves the same everywhere.

date: parsing and arithmetic

GNU date -d understands a date description; BSD uses -v to adjust and -j -f to parse.

bash · try it yourself
date -u -d '2026-01-15 10:00' '+%Y-%m-%d %H:%M %A'
date -u -d '2026-01-15 +3 days' +%F
date -u -d '2026-01-15 -1 month' +%F
date -u -d @0 '+%F %T'
date -u -d '2026-03-01' +%s
2026-01-15 10:00 Thursday
2026-01-18
2025-12-15
1970-01-01 00:00:00
1772323200
zsh
ada@macbook ~ % date -u -j -f '%Y-%m-%d %H:%M' '2026-01-15 10:00' '+%Y-%m-%d %H:%M %A'
2026-01-15 10:00 Thursday
ada@macbook ~ % date -u -j -v+3d -f '%Y-%m-%d' '2026-01-15' +%F
2026-01-18
ada@macbook ~ % date -u -j -v-1m -f '%Y-%m-%d' '2026-01-15' +%F
2025-12-15
ada@macbook ~ % date -u -r 0 '+%F %T'
1970-01-01 00:00:00

On BSD, -j means "don't try to set the clock", -f gives the input format, -v+3d adds three days and -r takes seconds since 1970.

ls, stat, readlink

bash · try it yourself
printf 'hello\n' > note.txt
ln -s note.txt link.txt
ls --color=never -1
stat -c '%n is %s bytes' note.txt
readlink -f link.txt
realpath link.txt
link.txt
note.txt
note.txt is 6 bytes
/home/learner/note.txt
/home/learner/note.txt
zsh
ada@macbook ~ % ls -G                        # BSD: -G turns on colour (GNU: --color=auto)
link.txt note.txt
ada@macbook ~ % stat -f '%N is %z bytes' note.txt
note.txt is 6 bytes
ada@macbook ~ % readlink -f link.txt         # available since macOS 12.3
/Users/ada/note.txt

Watch out: on GNU ls, -G means something else entirely ("don't show group").

Quick reference

Task GNU (Linux) BSD (macOS)
In-place edit sed -i 's/a/b/' f sed -i '' 's/a/b/' f
Extended regex in sed sed -E (or -r) sed -E
Date from text date -d '2026-01-15' date -j -f %Y-%m-%d 2026-01-15
Date arithmetic date -d '+3 days' date -v+3d
Epoch to date date -d @1700000000 date -r 1700000000
File size stat -c %s f stat -f %z f
Coloured ls ls --color=auto ls -G
Reverse lines tac f tail -r f
All lines but the last head -n -1 f sed '$d' f (portable)
Perl regex in grep grep -P not available (use grep -E or perl)
Custom find output find . -printf '%s %p\n' find . -exec stat -f '%z %N' {} +
xargs: skip if empty xargs -r default behaviour (-r accepted on recent macOS)
Base64 decode base64 -d base64 -d (older macOS: -D)

Getting GNU tools on a Mac

If you'd rather have the Linux behaviour on macOS, Homebrew installs the GNU tools with a g prefix, so they don't break macOS scripts that expect BSD tools:

zsh
ada@macbook ~ % brew install coreutils gnu-sed grep findutils gawk
ada@macbook ~ % gdate -d '2026-01-15 +3 days' +%F
2026-01-18
ada@macbook ~ % gsed -i 's/red/blue/' config.txt

To use them without the prefix in your interactive shell, put their gnubin folders first on your PATH in ~/.zshrc:

zsh
export PATH="$(brew --prefix coreutils)/libexec/gnubin:$(brew --prefix gnu-sed)/libexec/gnubin:$PATH"

Writing scripts that run on both

  • Stick to POSIX options where you can (sed -E, grep -E, cut, sort -n), and test on both systems (or in CI on Linux and macOS).
  • Detect the platform when you must: case "$(uname -s)" in Darwin) ... ;; Linux) ... ;; esac.
  • Prefer a real language (Python, Node) for date arithmetic and complex text processing.
  • Windows has a third family: CMD and PowerShell commands; Git Bash and WSL bring the GNU tools. See Cross-Platform Scripts.
bash · try it yourself
case "$(uname -s)" in
  Darwin) echo "macOS: use sed -i ''" ;;
  Linux)  echo "Linux: use sed -i" ;;
  *)      echo "something else" ;;
esac
Linux: use sed -i

Learn more: The Terminal on macOS, Bash sed, Cross-Platform Scripts.

Test yourself

What is the macOS equivalent of GNU sed -i 's/a/b/' file?

  1. sed -i 's/a/b/' file
  2. sed -i '' 's/a/b/' file
  3. sed --in-place file
  4. gsed is required

Which command adds three days to a date on macOS's BSD date?

  1. date -d '+3 days'
  2. date -v+3d
  3. date --add 3d
  4. date +3d

After brew install coreutils, how do you call GNU date on a Mac?

  1. date
  2. gdate
  3. gnu-date
  4. date --gnu