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.
1Umm... sed is a "text editor", it can also modify binary files. What else would you want it to do? Please explain what exactly you need. I cannot understand your problem, could you give some more examples? – terdon – 2012-09-18T13:49:32.050
3
Actually, sed is a stream editor. Not to be pedantic, but it's actually not meant to modify files, unless those files are streams. That's precisely why the
– kojiro – 2012-09-18T19:22:05.217-i
flag is non-standard.ed
is for editing files.