← All commands
THE COMMAND REFERENCE

realpath

Print the absolute, fully resolved form of a path.

Last updated

BusyBox

Implementation-level reference; examples are not certified for every installed version.

Overview

realpath turns any path into its absolute form: it adds the current folder to a relative path, removes . and .., and follows symbolic links. Use it to find out where a path really points, or to give another program a path that does not depend on the current folder.

Background

The realpath utility first appeared in FreeBSD 4.3, according to the macOS manual. Today the name covers GNU realpath, the realpath that macOS ships, and the realpath applet in BusyBox. They agree on resolving a path that exists; they differ on a path that does not exist yet.

Examples

Print the absolute path of a file reached through ... The output starts with a slash, so grep -c counts one match:

realpath logs/../notes.txt | grep -c '^/'

Show only the final part of the resolved path:

basename "$(realpath logs/../notes.txt)"

Resolve a symbolic link to the file it points at:

ln -s notes.txt link && basename "$(realpath link)"

Resolve the current folder:

cd logs && basename "$(realpath .)"

Common mistakes

realpath gives a different answer from the text you typed whenever a link or .. is involved, which is the point, but it can surprise you when a folder is a link into another disk. A path that does not exist is an error on macOS and is accepted by GNU, so a script that depends on either needs a check first with test -e. Quote the path and the command substitution if the name may contain spaces.

Portability

Only a path that exists is in the Common examples. GNU's --relative-to, -s, -m and -e are not on every system. The Common examples were executed on GNU coreutils 9.11, macOS system tools, and BusyBox 1.37.0 (see the development guide for the exact check).

About BusyBox realpath

BusyBox bundles realpath as an applet, and anything beyond the common options depends on how it was built. This page's examples ran on BusyBox 1.37.0.

BusyBox notes

BusyBox realpath prints the resolved path of each argument; the GNU options depend on the build.

References

Original explanations, with upstream documentation for further reading. FreeBSD references are comparison material, not a claim of Apple version equivalence.