Code, Computers & Random Junk

Installing GNU Coreutils in OS X

Do I need it? No… but you’re better off. With that I mean, you already have most of its tools in OS X. There are few ones missing and there are a few that are better than the version in OS X. And we’ll install the GNU version of sed as well. There are some differences between the OS X and GNU version, and you’re better off with the new one.


Jumping right into it… and I explain later/below.

You can find the downloads here. At the moment the latest version is 8.24. Download and verify, then unpack it.

This installation will be put in a folder outside PATH, then we pick out a few ones with symlinks. I use the folder: /usr/local/xbin where I also put my own script and stuff - keeping /usr/local/bin a bit cleaner. You can use that one instead, or any other folder, of course. The important thing is that it’s ahead of the OS X version later in PATH.

In Terminal - cd to the source folder and…

cd coreutils-8.24
mkdir __build && cd __build
sudo mkdir /usr/local/{xbin,xgnu/coreutils}
../configure --prefix=/usr/local/xgnu/coreutils --program-prefix=gnu_

make -j5
sudo make install

All programs have been installed in /usr/local/xgnu/coreutils/bin, with the prefix gnu_. That is, dd will be installed as gnu_dd instead. So, no risk (what so ever) to mistake them for being an OS X file.

The programs to use…

OS X have a lot of GNU software already, inlcuding many/most of the ones in GNU Coreutils. Take a look in /bin (and there are a few ones in /usr/bin) and see what’s in there.

ls -Ahl /bin

The reason to get the GNU versions is that some of them are missing, and some of them are better. OS X have sometimes done something for the better of OS X, but also sometimes …just different.

The missing ones I’m talking about is mainly the md5sum, sha1sum, sha256sum etc… Of course we have md5 instead and shasum which are in many ways better than all the GNU versions since shasum handles all the sha{1,224,256,384,512}sum all together. But, when scripting or perhaps using other scripts written on another platform using these… then you’re better off - you have them. So, there are better portability.

First - fix your PATH. In ~/.bashrc or ~/.bash_profile (if you’re using that one)

If you don’t have already, it’s good to have the /usr/local-folder in front of PATH. In that way anything you install will always come first …which is probably the reason you installed it in the first place.

You can use /usr/local/bin if you prefer that, or put these “xtras” in your own folder to keep better track of them.


export PATH="$_xbin:/usr/local/bin:/usr/local/sbin:$PATH"

OK, here we go…

cd /usr/local/xbin

md5sum and shaNNNsum

sudo ln -s /usr/local/xgnu/coreutils/bin/gnu_md5sum md5sum
sudo ln -s /usr/local/xgnu/coreutils/bin/gnu_sha1sum sha1sum
sudo ln -s /usr/local/xgnu/coreutils/bin/gnu_sha224sum sha224sum
sudo ln -s /usr/local/xgnu/coreutils/bin/gnu_sha256sum sha256sum
sudo ln -s /usr/local/xgnu/coreutils/bin/gnu_sha384sum sha384sum
sudo ln -s /usr/local/xgnu/coreutils/bin/gnu_sha512sum sha512sum

That will add them as symlinks, with their original name.


Another one great to have is date. There’s nothing wrong with date in OS X, just that I miss a few options like date -I. Something that’s not in OS X.

$ date -I

# os x equivalent
$ date "+%F"

Great when scripting and when automating file names with a date stamp.

$ echo 'FooBar' > foobar_$(date -I).txt


dd is a really great tool. The syntax differs a little bit between the OS X and GNU versions though.

OS X uses small letters like “m” instead of “M” for MegaByte. Another thing is also that OS X display the result in bytes instead om MB.

# OS X
$ dd if=/dev/random count=1 bs=1m of=./fooBar.txt
1+0 records in
1+0 records out
1048576 bytes transferred in 0.076491 secs (13708468 bytes/sec)

$ dd if=/dev/random count=1 bs=1M of=./fooBar.txt
1+0 records in
1+0 records out
1048576 bytes (1.0 MB) copied, 0.081676 s, 12.8 MB/s

Immediately you get “12.8MB/s” instead of all the bytes. Perhaps the best usage to explain the benefit is when you use dd to test the disk speed.


# write 1G
$ dd if=/dev/random count=1 bs=1G of=./speed_test

# purge the disk cache
$ purge

# read 1G
$ dd if=./speed_test of=/dev/null

Simple, yet efficiant way to check the disk speed. No need for any “app”.

Even with the GNU version installed it’s still easy to use the old one with the full path…

$ /bin/dd if=/dev/random count=1 bs=1m of=./fooBar.txt

Add the symlink with:

cd /usr/local/xbin
sudo ln -s /usr/local/xgnu/coreutils/bin/gnu_dd dd

More programs

Another program that is great to replace is sed. Both are working fine in OS X, but there is a small “WTF?”-thing going on with the OS X version. And that is when you’re replacing text in a file.


sed -i 's/foo/bar/g' fooBar.txt

On OS X you get:

sed: 1: "fooBar.txt": invalid command code f

So, you need to add an empty '' to supress the error.

# OS X
sed -i '' 's/foo/bar/g' fooBar.txt

You can always get the original (OS X) version later with:

/usr/bin/sed -i '' 's/foo/bar/g' fooBar.txt

Installing a lot of software manually (compiling), I find it more “secure” knowing that sed is behaving like it’s suppose to.



First you need a better iconv (libiconv). If using the iconv bundled in OS X, use this instead when configuring sed.


You can find the downloads here. At the moment the latest version is 1.14. Download and verify, then unpack it.

In Terminal - cd to the source folder and…

cd libiconv-1.14
mkdir __build && cd __build
make -j5
sudo make install


You can find the downloads here. At the moment the latest version is 4.2.2. Download and verify, then unpack it.

In Terminal - cd to the source folder and…

cd sed-4.2.2
mkdir __build && cd __build
../configure --prefix=/usr/local --with-libiconv-prefix=/usr/local
make -j5
sudo make install


$ which iconv sed


When you have multiple versions of a software and forgot the path to the original one, you can always check with type.

Example with sed:

$ which sed

$ type sed
sed is: /usr/local/bin/sed

Using type with option -a:

$ type -a sed
sed is /usr/local/bin/sed
sed is /usr/bin/sed 

Yaay… There it is. :)

Ok, you have now added some more “GNUliciousness” to OS X.

Happy hacking…