Replace OS X's shell commands with the linux versions?
In the general case, you can't (or shouldn't) replace the default commands at all. The reason is that many system administration scripts and third-party packages probably rely on these commands to behave the way they do out of the box on OS X.
So if you just wipe out the system commands and replace them with GNU equivalents that have incompatible behavior or command line arguments, it will probably break something. Especially if you use some software that was "ported" to Mac OS X after being originally designed to run on Linux or BSD, as these types of programs are more likely to rely on shell scripts and system commands as opposed to calling OS X APIs.
What you can do is install an environment that installs the GNU utilities in another directory without overwriting the defaults, and then adjust your PATH
environment variable so that it gives priority to commands found within the GNU directory before it even searches the system directories. You can wire this up so that it only sets your PATH that way if you are starting an interactive shell; you can google how to do this with bash
or ask another question on SU (or search for it, since it's probably been asked before) if you want to do that.
An example of such an environment is Homebrew which for example has GNU sed
among other things. Once you've installed Homebrew, you can type
brew install coreutils
and install the GNU Coreutils. These will provide you with sed
, date
, printf
, wc
and many other tools that ship with GNU/Linux, but not OS X. However, so as not to "override" default OS X binaries, they will be prefixed with g
by default. So, after installing the Coreutils, if you want to use GNU sed, type
gsed
If this is too much of a hassle to type every time, you can add a "gnubin" directory to your PATH and just call GNU sed with sed
. You will need to add the following to your ~/.bash_profile
:
PATH="$(brew --prefix coreutils)/libexec/gnubin:$PATH"
Of course, if you need a Linux environment from soup to nuts (kernel, X11, syscall compatibility, etc) you'll have to run Linux in a virtual machine, such as VirtualBox. This is a safe bet if you need to run software or scripts that are designed to run on Linux.
Homebrew will only afford you compatibility for certain classes of programs that do not require Linux-specific behavior. For example inotify
is only available on Linux. drm
(the Direct Rendering Manager) is only available on Linux. There are some other rather low-level system calls that are only available on Linux, and for which no equivalent exists on OS X, so porting certain programs from Linux to OS X can be impractical or impossible without significant code changes.
You could use a Gentoo Prefix which supports OS X, you can do this by bootstrapping it and then adding the relevant paths in the prefix directory to your PATH. It might be that this already does that for you. After you have done that you can use standard Gentoo commands for installing packages.
emerge coreutils
will get you the standard GNU utils, for instance.
Please note that Gentoo compiles by default, you might want to set up a binary host instead. This is just using one of the URLs on the second half of that article, and placing it in PORTAGE_BINHOST="... your url here ..."
in ./etc/make.conf
in your prefix.
As a follow up to @allquixotic's post, here are the official instructions per brew install coreutils
All commands have been installed with the prefix 'g'.
If you really need to use these commands with their normal name can add a "gnubin" directory to your PATH from your bashrc like
PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH"
Additionally, you can access their man pages with normal names the "gnuman" directory to your MANPATH from your bashrc as well
MANPATH="/usr/local/opt/coreutils/libexec/gnuman:$MANPATH"