Implementation
- Install brew
Follow the instructions at https://brew.sh/
- Install the coreutils package
brew install coreutils
- Create an Alias or Symlink
3a. Create an an alias (per user)
You can place your alias in ~/.bashrc, ~/.bash_profile, or wherever you are used to keeping your bash aliases. I personally keep mine in ~/.bashrc
alias readlink=greadlink
3b. Create a symbolic link (system wide)
ln -s /usr/local/bin/greadlink /usr/local/bin/readlink
(credit: Izana)
This will create a symbolic link in /usr/local/bin while keeping the original readlink binary in tact. It works because the search for readlink will return 2 results. But the second in /usr/local/bin will take precedence.
e.g. which readlink
To undo this change simply unlink /usr/local/bin/readlink
Additional Tools
You can create similar aliases or symlinks for other coreutils such as gmv, gdu, gdf, and so on. But beware that the GNU behavior on a mac machine may be confusing to others used to working with native coreutils, or may behave in unexpected ways on your mac system.
Explanation
coreutils is a brew
package that installs GNU/Linux core utilities which correspond to the Mac OSX implementation of them so that you can use those
You may find programs or utilties on your mac osx system which seem similar to Linux coreutils ("Core Utilities") yet they differ in some ways (such as having different flags).
This is because the Mac OSX implementation of these tools are different. To get the original GNU/Linux-like behavior you can install the coreutils
package via the brew
package management system.
This will install corresponding core utilities, prefixed by g
. E.g. for readlink
, you will find a corresponding greadlink
program.
In order to make readlink
perform like the GNU readlink
(greadlink
) implementation, you can create a simple alias or symbolic link after you install coreutils.
readlink
can be a builtin or an external command. – Severalty/bin/sh
withoutbash
extensions). For many developers these days shell scripts = Linux, GNU utilities andbash
so portability issues appear. – Arquittouch myfile ; ln -s myfile otherfile ; perl -MCwd=abs_path -le 'print abs_path readlink(shift);' otherfile
... in my case I see: /Users/cito/myfile`. Added it to my response below. Cheers. – Arquitpwd -P
on OS X without installing anything – Fredkinreadlink
is GPLv3 and Apple won't touch anything GPLv3 because of the requirement to hand over cryptographic keys to a device in order to modify GPLv3 software on that device. gnu.org/licenses/… It will be one reason the default shell is no longer Bash but Zsh – Fredkinreadlink -f
since sometime between 2010 and 2012, depending on man page date vs. BSD release date, and that's not a GPL implementation. So Apple is just dragging its heels. And actually,readlink -f
originated in OpenBSD 2.1 in 1997, as far as I can tell. So the first implementation wasn't even GNU. I can understand it not being in Snow Leopard, but I can't understand it not being in Mavericks/Sierra. – Biflagellatepwd -P
without wishing Mac OS was GNU Linux by using Homebrew. Yes shell scripts are not portable. Not unique to Mac OS. – Fredkinreadlink -f
. – Ferial