Home » What’s the difference between “realpath” and “readlink -f”

What’s the difference between “realpath” and “readlink -f”

Solutons:


There are several realpath commands around.

The realpath utility is a wrapper around the realpath library functions and has been reinvented many times.

Debian used to maintain a realpath package (separated from dwww 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 (GNU readlink and soon GNU realpath), but at the time, GNU utilities didn’t even have readlink at all. This implementation of realpath supports a few options to prevent symbolic link resolution or produce null-terminated output.
BusyBox also includes its own realpath 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’s realpath, and also has many options in common with GNU readlink.

realpath has the same effect as readlink -f with GNU readlink. What distinguishes the two commands (or rather the various realpath commands from readlink -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 GNU realpath from its coreutils package and stick with its own realpath. I don’t know why, since GNU realpath should be a drop-in replacement. As of Debian jessie and Ubuntu 16.04, however, GNU realpath 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 GNU readlink. In particular, BSD readlink 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).

tl;dr readlink -f will return 0 for a non-existent file in an existing directory whereas realpath returns 1. However, readlink -e will behave like realpath and return 1 for a non-existent file (see Editors Notes at end).

readlink -f

$ readlink -f non-existent-file
/home/user/non-existent-file
$ echo $?
0

readlink -e

$ readlink -e non-existent-file
$ echo $?
1

realpath

$ realpath non-existent-file
non-existent-file: No such file or directory
$ echo $?
1

readlink -f with non-existent directory

readlink -f behavior varies depending upon which part of the path does not exist.

$ readlink -f /tmp/non-existent-dir/foo
$ echo $?
1

realpath with non-existent directory

$ realpath /tmp/non-existent-dir/foo
$ echo $?
1

Availablity

readlink is already installed within most Linux distributions. Whereas, realpath is also becoming more often already installed as realpath has been rolled into GNU Coreutils (in prior years, it was it’s own separate package).

In summary

If you want to replace calls to realpath ... then use readlink -e ....
However, realpath is becoming more widely distributed, so it’s not unreasonable to expect realpath to be installed.

MMV readlink implementations vary widely.
MMV realpath availability in a default Linux installation varies somewhat more than readlink.

Tested with readlink (GNU coreutils) 8.21 and realpath version 1.19 on Ubuntu 16.


Editors Notes

  • Ed.: @AnthonyGeoghegan wrote “this refers to the Debian version of realpath. The GNU version of realpath behaves the same as readlink -f“)

  • Ed.: @FilBot3 wrote “this doesn’t work on MacOS High Sierra

As stated in other responses, the functionality of readlink -f or readlink -e (depending on the distribution) can be equivalent to that of realpath. However, realpath the utility is not equivalent to readlink. E.g., readlink can read/retrieve the contents of (the literal text in) a symbolic link file:

> ln -s "I am cold" slfile
> readlink slfile
I am cold

which realpath cannot do.

A symbolic link file, after all, is a text file interpreted as a “recipe” to move about the directory/file hierarchy. But it is still a text file. For example, its contents (what readlink returns) should be possible to be manipulated with sed -i '...' slfile — in fact, option --follow-symlinks is to force sed -i to follow symlinks. I write “should” because in my current setup (OpenSuSe 15.3 default, GNU sed 4.4, GNU bash version 4.4.23(1)) this malfunctions: sed -i without the --follow-symlinks option still wants to follow as a path the contents of a symlink file.

Related Solutions

Joining bash arguments into single string with spaces

[*] I believe that this does what you want. It will put all the arguments in one string, separated by spaces, with single quotes around all: str="'$*'" $* produces all the scripts arguments separated by the first character of $IFS which, by default, is a space....

AddTransient, AddScoped and AddSingleton Services Differences

TL;DR Transient objects are always different; a new instance is provided to every controller and every service. Scoped objects are the same within a request, but different across different requests. Singleton objects are the same for every object and every...

How to download package not install it with apt-get command?

Use --download-only: sudo apt-get install --download-only pppoe This will download pppoe and any dependencies you need, and place them in /var/cache/apt/archives. That way a subsequent apt-get install pppoe will be able to complete without any extra downloads....

What defines the maximum size for a command single argument?

Answers Definitely not a bug. The parameter which defines the maximum size for one argument is MAX_ARG_STRLEN. There is no documentation for this parameter other than the comments in binfmts.h: /* * These are the maximum length and maximum number of strings...

Bulk rename, change prefix

I'd say the simplest it to just use the rename command which is common on many Linux distributions. There are two common versions of this command so check its man page to find which one you have: ## rename from Perl (common in Debian systems -- Ubuntu, Mint,...

Output from ls has newlines but displays on a single line. Why?

When you pipe the output, ls acts differently. This fact is hidden away in the info documentation: If standard output is a terminal, the output is in columns (sorted vertically) and control characters are output as question marks; otherwise, the output is...

mv: Move file only if destination does not exist

mv -vn file1 file2. This command will do what you want. You can skip -v if you want. -v makes it verbose - mv will tell you that it moved file if it moves it(useful, since there is possibility that file will not be moved) -n moves only if file2 does not exist....

Is it possible to store and query JSON in SQLite?

SQLite 3.9 introduced a new extension (JSON1) that allows you to easily work with JSON data . Also, it introduced support for indexes on expressions, which (in my understanding) should allow you to define indexes on your JSON data as well. PostgreSQL has some...

Combining tail && journalctl

You could use: journalctl -u service-name -f -f, --follow Show only the most recent journal entries, and continuously print new entries as they are appended to the journal. Here I've added "service-name" to distinguish this answer from others; you substitute...

how can shellshock be exploited over SSH?

One example where this can be exploited is on servers with an authorized_keys forced command. When adding an entry to ~/.ssh/authorized_keys, you can prefix the line with command="foo" to force foo to be run any time that ssh public key is used. With this...

Why doesn’t the tilde (~) expand inside double quotes?

The reason, because inside double quotes, tilde ~ has no special meaning, it's treated as literal. POSIX defines Double-Quotes as: Enclosing characters in double-quotes ( "" ) shall preserve the literal value of all characters within the double-quotes, with the...

What is GNU Info for?

GNU Info was designed to offer documentation that was comprehensive, hyperlinked, and possible to output to multiple formats. Man pages were available, and they were great at providing printed output. However, they were designed such that each man page had a...

Set systemd service to execute after fstab mount

a CIFS network location is mounted via /etc/fstab to /mnt/ on boot-up. No, it is not. Get this right, and the rest falls into place naturally. The mount is handled by a (generated) systemd mount unit that will be named something like mnt-wibble.mount. You can...

Merge two video clips into one, placing them next to each other

To be honest, using the accepted answer resulted in a lot of dropped frames for me. However, using the hstack filter_complex produced perfectly fluid output: ffmpeg -i left.mp4 -i right.mp4 -filter_complex hstack output.mp4 ffmpeg -i input1.mp4 -i input2.mp4...

How portable are /dev/stdin, /dev/stdout and /dev/stderr?

It's been available on Linux back into its prehistory. It is not POSIX, although many actual shells (including AT&T ksh and bash) will simulate it if it's not present in the OS; note that this simulation only works at the shell level (i.e. redirection or...

How can I increase the number of inodes in an ext4 filesystem?

It seems that you have a lot more files than normal expectation. I don't know whether there is a solution to change the inode table size dynamically. I'm afraid that you need to back-up your data, and create new filesystem, and restore your data. To create new...

Why doesn’t cp have a progress bar like wget?

The tradition in unix tools is to display messages only if something goes wrong. I think this is both for design and practical reasons. The design is intended to make it obvious when something goes wrong: you get an error message, and it's not drowned in...