I've read a lot about realpath
command and how it has been deprecated with the readlink -f
being now recommended. I have also seen in some places that the reason why realpath was introduced was for the lack of such functionality in readlink and that once it has been introduced, realpath was no longer needed and its support discontinued by most OS vendors.
The reason for my question is that I've also seen many people recommending readlink -f
as a command "pretty much similar" to realpath
, and that is what is bothering me, because no one elaborates on that "pretty much similar" part. What are the actual differences?
Best Answer
There are several
realpath
commands around.The
realpath
utility is a wrapper around therealpath
library functions and has been reinvented many times.Debian used to maintain a
realpath
package (separated fromdwww
since woody) which hasn't changed except regarding packaging and documentation since 2001, but has now been phased out. This utility was deprecated because there are now more standard alternatives (GNUreadlink
and soon GNUrealpath
), but at the time, GNU utilities didn't even havereadlink
at all. This implementation ofrealpath
supports a fewoptions
to prevent symbolic link resolution or produce null-terminated output. BusyBox also includes its ownrealpath
command (which takes no option).GNU coreutils introduced a
realpath
command in version 8.15 in January 2012. This is a compatible replacement for BusyBox's and Debian'srealpath
, and also has many options in common with GNUreadlink
.realpath
has the same effect asreadlink -f
with GNUreadlink
. What distinguishes the two commands (or rather the variousrealpath
commands fromreadlink -f
) is the extra options that they support.GNU
realpath
is not deprecated; it has the opposite problem: it's too new to be available everywhere. Debian used to omit GNUrealpath
from itscoreutils
package and stick with its ownrealpath
. I don't know why, since GNUrealpath
should be a drop-in replacement. As of Debian jessie and Ubuntu 16.04, however, GNUrealpath
is used.On Linux systems, at the moment, your best bet to canonicalize a path that may contain symbolic links is
readlink -f
.BSD systems have a
readlink
command, with different capabilities from GNUreadlink
. In particular, BSDreadlink
does not have an option to canonicalize paths, it only traverses the symlink passed to it.readlink
, incidentally, had the same problem — it was also invented many times (not adding this utility when symbolic links were added to Unix was a regrettable omission). It has now stabilized in several implementations with many incompatible flags (in particular BSD vs. GNU).