Skip to content

Instantly share code, notes, and snippets.

@tamakiii
Last active July 1, 2024 08:55
Show Gist options
  • Save tamakiii/89650848a2121bb3c97bb12b569c2d0d to your computer and use it in GitHub Desktop.
Save tamakiii/89650848a2121bb3c97bb12b569c2d0d to your computer and use it in GitHub Desktop.
Use GNU commands on macOS

coreutils

The default installed commands, such as date, may not be compatible with general Linux systems.

$ date -d '+10 minutes' +%s
date: illegal option -- d
usage: date [-jnRu] [-I[date|hours|minutes|seconds]] [-f input_fmt]
            [-r filename|seconds] [-v[+|-]val[y|m|w|d|H|M|S]]
            [[[[mm]dd]HH]MM[[cc]yy][.SS] | new_date] [+output_fmt]

To resolve this, install coreutils:

brew install coreutils

Next, add the following line to either ~/.zprofile or ~/.bash_profile:

export PATH="/opt/homebrew/opt/coreutils/libexec/gnubin:$PATH"

Afterward, the command should work as expected:

$ date -d '+10 minutes' +%s
1683467946

gnu-sed

brew install gnu-sed
export PATH="/opt/homebrew/opt/gnu-sed/libexec/gnubin:$PATH"

grep

brew install grep
export PATH="/opt/homebrew/opt/grep/libexec/gnubin:$PATH"

find

brew install findutils
export PATH="/opt/homebrew/opt/findutils/libexec/gnubin:$PATH"
@tamakiii
Copy link
Author

tamakiii commented Jul 20, 2023

$ which sed
/opt/homebrew/opt/gnu-sed/libexec/gnubin/sed

$ which grep
/opt/homebrew/opt/grep/libexec/gnubin/grep

$ which date
/opt/homebrew/opt/coreutils/libexec/gnubin/date

$ which find
/opt/homebrew/opt/findutils/libexec/gnubin/find

@tamakiii
Copy link
Author

tamakiii commented Jul 1, 2024

$ echo grep sed find date | xargs which
/opt/homebrew/opt/grep/libexec/gnubin/grep
/opt/homebrew/opt/gnu-sed/libexec/gnubin/sed
/opt/homebrew/opt/findutils/libexec/gnubin/find
/opt/homebrew/opt/coreutils/libexec/gnubin/date

@tamakiii
Copy link
Author

tamakiii commented Jul 1, 2024

$ brew reinstall coreutils gnu-sed grep findutils | grep PATH= | awk '{ print $1 }' | sort | uniq
PATH="/opt/homebrew/opt/coreutils/libexec/gnubin:$PATH"
PATH="/opt/homebrew/opt/findutils/libexec/gnubin:$PATH"
PATH="/opt/homebrew/opt/gnu-sed/libexec/gnubin:$PATH"
PATH="/opt/homebrew/opt/grep/libexec/gnubin:$PATH"

@tamakiii
Copy link
Author

tamakiii commented Jul 1, 2024

$ ls /opt/homebrew/opt/coreutils/libexec/gnubin | xargs -I @ basename @ | sed -e 's/\n/ /g'
[
b2sum
base32
base64
basename
basenc
cat
chcon
chgrp
chmod
chown
chroot
cksum
comm
cp
csplit
cut
date
dd
df
dir
dircolors
dirname
du
echo
env
expand
expr
factor
false
fmt
fold
groups
head
hostid
id
install
join
kill
link
ln
logname
ls
md5sum
mkdir
mkfifo
mknod
mktemp
mv
nice
nl
nohup
nproc
numfmt
od
paste
pathchk
pinky
pr
printenv
printf
ptx
pwd
readlink
realpath
rm
rmdir
runcon
seq
sha1sum
sha224sum
sha256sum
sha384sum
sha512sum
shred
shuf
sleep
sort
split
stat
stdbuf
stty
sum
sync
tac
tail
tee
test
timeout
touch
tr
true
truncate
tsort
tty
uname
unexpand
uniq
unlink
uptime
users
vdir
wc
who
whoami
yes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment